public 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) { var 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; }
public 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); }