internal static new JsonDate Parse(string text) { if (text == null) { throw new ArgumentNullException(nameof(text)); } // TODO support: unixtimeseconds.partialseconds if (text.Length > 4 && _IsNumber(text)) // UnixTime { var date = DateTimeOffset.FromUnixTimeSeconds(long.Parse(text)); return(new JsonDate(date)); } else if (text.Length <= 4 || text[4] == '-') // ISO: 2012- { return(new JsonDate(IsoDate.Parse(text).ToDateTimeOffset())); } else { // NOT ISO ENCODED // "Thu, 5 Apr 2012 16:59:01 +0200", return(new JsonDate(DateTimeOffset.Parse(text))); } }
internal static IsoDate Parse(string text) { var tzIndex = -1; var timeIndex = text.IndexOf('T'); var builder = new IsoDate { Day = 1, Month = 1 }; // TODO: strip the time zone offset off the end string dateTime = text; string timeZone = null; if (dateTime.IndexOf('Z') > -1) { tzIndex = dateTime.LastIndexOf('Z'); builder.Kind = DateTimeKind.Utc; } else if (dateTime.LastIndexOf('+') > 10) { tzIndex = dateTime.LastIndexOf('+'); } else if (dateTime.LastIndexOf('-') > 10) { tzIndex = dateTime.LastIndexOf('-'); } if (tzIndex > -1) { timeZone = dateTime.Substring(tzIndex); dateTime = dateTime.Substring(0, tzIndex); } string date = (timeIndex == -1) ? dateTime : dateTime.Substring(0, timeIndex); var dateParts = date.Split(Seperator.Dash); // '-' for (int i = 0; i < dateParts.Length; i++) { var part = dateParts[i]; switch (i) { case 0: builder.Year = int.Parse(part); break; case 1: builder.Month = int.Parse(part); break; case 2: builder.Day = int.Parse(part); break; } } if (timeIndex > -1) { string[] timeParts = dateTime.Substring(timeIndex + 1).Split(timeSeperators); for (int i = 0; i < timeParts.Length; i++) { var part = timeParts[i]; switch (i) { case 0: builder.Hour = int.Parse(part); break; case 1: builder.Minute = int.Parse(part); break; case 2: builder.Second = int.Parse(part); break; case 3: builder.Millisecond = double.Parse("0." + part) * 1000; break; } } } if (timeZone != null && timeZone != "Z") { var hours = int.Parse(timeZone.Substring(1, 2)); var minutes = int.Parse(timeZone.Substring(4, 2)); if (timeZone[0] == '-') { hours = -hours; minutes = -minutes; } builder.Offset = new TimeSpan(hours, minutes, 0); } return(builder); }
internal string ToIsoString() { return(IsoDate.FromDateTimeOffset(value).ToString()); }