private static DateTime ParseFormattedDate(string input, CultureInfo culture) { var formats = new[] { "u", "s", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", "yyyy-MM-ddTHH:mm:ssZ", "yyyy-MM-dd HH:mm:ssZ", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-ddTHH:mm:sszzzzzz", "M/d/yyyy h:mm:ss tt" // default format for invariant culture }; DateTime date; #if !PocketPC if (DateTime.TryParseExact(input, formats, culture, DateTimeStyles.None, out date)) #else if (ParseAssistant.TryParseExact(input, formats, culture, DateTimeStyles.None, out date)) #endif { return(date); } #if !PocketPC if (DateTime.TryParse(input, culture, DateTimeStyles.None, out date)) #else if (ParseAssistant.TryParse(input, culture, DateTimeStyles.None, out date)) #endif { return(date); } return(default(DateTime)); }
/// <summary> /// Parses most common JSON date formats /// </summary> /// <param name="input">JSON value to parse</param> /// <returns>DateTime</returns> public static DateTime ParseJsonDate(this string input, CultureInfo culture) { input = input.Replace("\n", ""); input = input.Replace("\r", ""); input = input.RemoveSurroundingQuotes(); long unix; #if !PocketPC if (Int64.TryParse(input, out unix)) #else if (ParseAssistant.TryParse(input, out unix)) #endif { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return(epoch.AddSeconds(unix)); } if (input.Contains("/Date(")) { return(ExtractDate(input, @"\\?/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\?/", culture)); } if (input.Contains("new Date(")) { input = input.Replace(" ", ""); // because all whitespace is removed, match against newDate( instead of new Date( return(ExtractDate(input, @"newDate\((-?\d+)*\)", culture)); } return(ParseFormattedDate(input, culture)); }
public static object ParsePrimitive(string value) { if (string.IsNullOrEmpty(value)) { return(null); } bool boolValue; #if !NETCF if (bool.TryParse(value, out boolValue)) { return(boolValue); } #else if (ParseAssistant.TryParse(value, out boolValue)) { return(boolValue); } #endif decimal decimalValue; #if !NETCF if (decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out decimalValue)) #else // TODO NETCF check NumberStyles.Number for what if (ParseAssistant.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out decimalValue)) #endif { if (decimalValue == decimal.Truncate(decimalValue)) { if (decimalValue <= ulong.MaxValue && decimalValue >= 0) { return((ulong)decimalValue); } if (decimalValue <= long.MaxValue && decimalValue >= long.MinValue) { var longValue = (long)decimalValue; if (longValue <= sbyte.MaxValue && longValue >= sbyte.MinValue) { return((sbyte)longValue); } if (longValue <= byte.MaxValue && longValue >= byte.MinValue) { return((byte)longValue); } if (longValue <= short.MaxValue && longValue >= short.MinValue) { return((short)longValue); } if (longValue <= ushort.MaxValue && longValue >= ushort.MinValue) { return((ushort)longValue); } if (longValue <= int.MaxValue && longValue >= int.MinValue) { return((int)longValue); } if (longValue <= uint.MaxValue && longValue >= uint.MinValue) { return((uint)longValue); } } } return(decimalValue); } float floatValue; #if !NETCF if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatValue)) { return(floatValue); } #else // TODO NETCF check NumberStyles.Number for what if (ParseAssistant.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatValue)) { return(floatValue); } #endif double doubleValue; #if !NETCF if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue)) { return(doubleValue); } #else // TODO NETCF check NumberStyles.Number for what if (ParseAssistant.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue)) { return(doubleValue); } #endif return(null); }
private static ParseStringDelegate GetParseFn() { //Note the generic typeof(T) is faster than using var type = typeof(T) if (typeof(T) == typeof(bool)) { return(value => bool.Parse(value)); } if (typeof(T) == typeof(byte)) { return(value => byte.Parse(value)); } if (typeof(T) == typeof(sbyte)) { return(value => sbyte.Parse(value)); } if (typeof(T) == typeof(short)) { return(value => short.Parse(value)); } if (typeof(T) == typeof(int)) { return(value => int.Parse(value)); } if (typeof(T) == typeof(long)) { return(value => long.Parse(value)); } if (typeof(T) == typeof(float)) { return(value => float.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(double)) { return(value => double.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(decimal)) { return(value => decimal.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(Guid)) { return(value => new Guid(value)); } if (typeof(T) == typeof(DateTime?)) { return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value)); } if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTime?)) { return(value => DateTimeSerializer.ParseShortestXsdDateTime(value)); } #if !NETCF if (typeof(T) == typeof(DateTimeOffset) || typeof(T) == typeof(DateTimeOffset?)) { return(value => DateTimeSerializer.ParseDateTimeOffset(value)); } #endif if (typeof(T) == typeof(TimeSpan)) { return(value => DateTimeSerializer.ParseTimeSpan(value)); } if (typeof(T) == typeof(TimeSpan?)) { return(value => DateTimeSerializer.ParseNullableTimeSpan(value)); } #if !MONOTOUCH && !SILVERLIGHT && !XBOX && !ANDROID && !NETCF if (typeof(T) == typeof(System.Data.Linq.Binary)) { return(value => new System.Data.Linq.Binary(Convert.FromBase64String(value))); } #endif if (typeof(T) == typeof(char)) { char cValue; #if !NETCF return(value => char.TryParse(value, out cValue) ? cValue : '\0'); #else return(value => ParseAssistant.TryParse(value, out cValue) ? cValue : '\0'); #endif } if (typeof(T) == typeof(ushort)) { return(value => ushort.Parse(value)); } if (typeof(T) == typeof(uint)) { return(value => uint.Parse(value)); } if (typeof(T) == typeof(ulong)) { return(value => ulong.Parse(value)); } if (typeof(T) == typeof(bool?)) { return(value => string.IsNullOrEmpty(value) ? (bool?)null : bool.Parse(value)); } if (typeof(T) == typeof(byte?)) { return(value => string.IsNullOrEmpty(value) ? (byte?)null : byte.Parse(value)); } if (typeof(T) == typeof(sbyte?)) { return(value => string.IsNullOrEmpty(value) ? (sbyte?)null : sbyte.Parse(value)); } if (typeof(T) == typeof(short?)) { return(value => string.IsNullOrEmpty(value) ? (short?)null : short.Parse(value)); } if (typeof(T) == typeof(int?)) { return(value => string.IsNullOrEmpty(value) ? (int?)null : int.Parse(value)); } if (typeof(T) == typeof(long?)) { return(value => string.IsNullOrEmpty(value) ? (long?)null : long.Parse(value)); } if (typeof(T) == typeof(float?)) { return(value => string.IsNullOrEmpty(value) ? (float?)null : float.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(double?)) { return(value => string.IsNullOrEmpty(value) ? (double?)null : double.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(decimal?)) { return(value => string.IsNullOrEmpty(value) ? (decimal?)null : decimal.Parse(value, CultureInfo.InvariantCulture)); } if (typeof(T) == typeof(TimeSpan?)) { return(value => string.IsNullOrEmpty(value) ? (TimeSpan?)null : TimeSpan.Parse(value)); } if (typeof(T) == typeof(Guid?)) { return(value => string.IsNullOrEmpty(value) ? (Guid?)null : new Guid(value)); } if (typeof(T) == typeof(ushort?)) { return(value => string.IsNullOrEmpty(value) ? (ushort?)null : ushort.Parse(value)); } if (typeof(T) == typeof(uint?)) { return(value => string.IsNullOrEmpty(value) ? (uint?)null : uint.Parse(value)); } if (typeof(T) == typeof(ulong?)) { return(value => string.IsNullOrEmpty(value) ? (ulong?)null : ulong.Parse(value)); } if (typeof(T) == typeof(char?)) { char cValue; #if !NETCF return(value => string.IsNullOrEmpty(value) ? (char?)null : char.TryParse(value, out cValue) ? cValue : '\0'); #else return(value => string.IsNullOrEmpty(value) ? (char?)null : ParseAssistant.TryParse(value, out cValue) ? cValue : '\0'); #endif } return(null); }
public static DateTime?ParseManual(string dateTimeStr) { if (dateTimeStr == null || dateTimeStr.Length < "YYYY-MM-DD".Length) { return(null); } var dateKind = DateTimeKind.Utc; if (dateTimeStr.EndsWith("Z")) { dateTimeStr = dateTimeStr.Substring(0, dateTimeStr.Length - 1); } var parts = dateTimeStr.Split('T'); if (parts.Length == 1) { parts = dateTimeStr.SplitOnFirst(' '); } var dateParts = parts[0].Split('-'); int hh = 0, min = 0, ss = 0, ms = 0; double subMs = 0; int offsetMultiplier = 0; if (parts.Length == 2) { var timeStringParts = parts[1].Split('+'); if (timeStringParts.Length == 2) { offsetMultiplier = -1; } else { timeStringParts = parts[1].Split('-'); if (timeStringParts.Length == 2) { offsetMultiplier = 1; } } var timeOffset = timeStringParts.Length == 2 ? timeStringParts[1] : null; var timeParts = timeStringParts[0].Split(':'); if (timeParts.Length == 3) { #if !NETCF int.TryParse(timeParts[0], out hh); int.TryParse(timeParts[1], out min); #else ParseAssistant.TryParse(timeParts[0], out hh); ParseAssistant.TryParse(timeParts[1], out min); #endif var secParts = timeParts[2].Split('.'); #if !NETCF int.TryParse(secParts[0], out ss); #else ParseAssistant.TryParse(secParts[0], out ss); #endif if (secParts.Length == 2) { var msStr = secParts[1].PadRight(3, '0'); ms = int.Parse(msStr.Substring(0, 3)); if (msStr.Length > 3) { var subMsStr = msStr.Substring(3); subMs = double.Parse(subMsStr) / Math.Pow(10, subMsStr.Length); } } } var dateTime = new DateTime(int.Parse(dateParts[0]), int.Parse(dateParts[1]), int.Parse(dateParts[2]), hh, min, ss, ms, dateKind); if (subMs != 0) { dateTime.AddMilliseconds(subMs); } if (offsetMultiplier != 0 && timeOffset != null) { timeParts = timeOffset.Split(':'); if (timeParts.Length == 2) { hh = int.Parse(timeParts[0]); min = int.Parse(timeParts[1]); } else { hh = int.Parse(timeOffset.Substring(0, 2)); min = int.Parse(timeOffset.Substring(2)); } dateTime = dateTime.AddHours(offsetMultiplier * hh); dateTime = dateTime.AddMinutes(offsetMultiplier * min); } return(dateTime.ToLocalTime().Prepare()); } return(null); }