private static DateTime CreateDateTime(DateTimeParser dateTimeParser) { bool is24Hour; if (dateTimeParser.Hour == 24) { is24Hour = true; dateTimeParser.Hour = 0; } else { is24Hour = false; } DateTime d = new DateTime(dateTimeParser.Year, dateTimeParser.Month, dateTimeParser.Day, dateTimeParser.Hour, dateTimeParser.Minute, dateTimeParser.Second); d = d.AddTicks(dateTimeParser.Fraction); if (is24Hour) { d = d.AddDays(1); } return(d); }
private static DateTime CreateDateTime(DateTimeParser dateTimeParser) { bool is24Hour; if (dateTimeParser.Hour == 24) { is24Hour = true; dateTimeParser.Hour = 0; } else { is24Hour = false; } DateTime d = new DateTime(dateTimeParser.Year, dateTimeParser.Month, dateTimeParser.Day, dateTimeParser.Hour, dateTimeParser.Minute, dateTimeParser.Second); d = d.AddTicks(dateTimeParser.Fraction); if (is24Hour) { d = d.AddDays(1); } return d; }
internal static bool TryParseDateTimeOffsetIso(StringReference text, out DateTimeOffset dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTimeOffset); return false; } DateTime d = CreateDateTime(dateTimeParser); TimeSpan offset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: offset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: offset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: offset = TimeZoneInfo.Local.GetUtcOffset(d); break; } long ticks = d.Ticks - offset.Ticks; if (ticks < 0 || ticks > 3155378975999999999) { dt = default(DateTimeOffset); return false; } dt = new DateTimeOffset(d, offset); return true; }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTime); return false; } DateTime d = CreateDateTime(dateTimeParser); long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return true; }
internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt) { DateTimeParser dateTimeParser = default(DateTimeParser); if (!dateTimeParser.Parse(text)) { dt = null; return(false); } DateTime dateTime = new DateTime(dateTimeParser.Year, dateTimeParser.Month, dateTimeParser.Day, dateTimeParser.Hour, dateTimeParser.Minute, dateTimeParser.Second); dateTime = dateTime.AddTicks((long)dateTimeParser.Fraction); if (dateParseHandling != DateParseHandling.DateTimeOffset) { switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan timeSpan = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); long num = dateTime.Ticks + timeSpan.Ticks; if (num <= DateTime.MaxValue.Ticks) { dateTime = new DateTime(num, DateTimeKind.Utc).ToLocalTime(); } else { num += dateTime.GetUtcOffset().Ticks; if (num > DateTime.MaxValue.Ticks) { num = DateTime.MaxValue.Ticks; } dateTime = new DateTime(num, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan timeSpan2 = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); long num = dateTime.Ticks - timeSpan2.Ticks; if (num >= DateTime.MinValue.Ticks) { dateTime = new DateTime(num, DateTimeKind.Utc).ToLocalTime(); } else { num += dateTime.GetUtcOffset().Ticks; if (num < DateTime.MinValue.Ticks) { num = DateTime.MinValue.Ticks; } dateTime = new DateTime(num, DateTimeKind.Local); } break; } } dt = DateTimeUtils.EnsureDateTime(dateTime, dateTimeZoneHandling); return(true); } TimeSpan utcOffset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: utcOffset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: utcOffset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: utcOffset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime); break; } long num2 = dateTime.Ticks - utcOffset.Ticks; if (num2 < 0L || num2 > 3155378975999999999L) { dt = null; return(false); } dt = new DateTimeOffset(dateTime, utcOffset); return(true); }
internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text)) { dt = null; return false; } DateTime d = new DateTime(dateTimeParser.Year, dateTimeParser.Month, dateTimeParser.Day, dateTimeParser.Hour, dateTimeParser.Minute, dateTimeParser.Second); d = d.AddTicks(dateTimeParser.Fraction); if (dateParseHandling == DateParseHandling.DateTimeOffset) { TimeSpan offset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: offset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: offset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: offset = TimeZoneInfo.Local.GetUtcOffset(d); break; } long ticks = d.Ticks - offset.Ticks; if (ticks < 0 || ticks > 3155378975999999999) { dt = null; return false; } dt = new DateTimeOffset(d, offset); return true; } else { long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) ticks = DateTime.MaxValue.Ticks; d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) ticks = DateTime.MinValue.Ticks; d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return true; } }
internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text)) { dt = null; return(false); } DateTime d = new DateTime(dateTimeParser.Year, dateTimeParser.Month, dateTimeParser.Day, dateTimeParser.Hour, dateTimeParser.Minute, dateTimeParser.Second); d = d.AddTicks(dateTimeParser.Fraction); #if !NET20 if (dateParseHandling == DateParseHandling.DateTimeOffset) { TimeSpan offset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: offset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: offset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: offset = TimeZoneInfo.Local.GetUtcOffset(d); break; } long ticks = d.Ticks - offset.Ticks; if (ticks < 0 || ticks > 3155378975999999999) { dt = null; return(false); } dt = new DateTimeOffset(d, offset); return(true); } else #endif { long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return(true); } }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default; return(false); } DateTime d = CreateDateTime(dateTimeParser); long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return(true); }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { long ticks; TimeSpan utcOffset; DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = new DateTime(); return(false); } DateTime dateTime = DateTimeUtils.CreateDateTime(dateTimeParser); switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: { dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Utc); break; } case ParserTimeZone.LocalWestOfUtc: { TimeSpan timeSpan = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = dateTime.Ticks + timeSpan.Ticks; if (ticks > DateTime.MaxValue.Ticks) { utcOffset = dateTime.GetUtcOffset(); ticks += utcOffset.Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } dateTime = new DateTime(ticks, DateTimeKind.Local); break; } else { dateTime = (new DateTime(ticks, DateTimeKind.Utc)).ToLocalTime(); break; } } case ParserTimeZone.LocalEastOfUtc: { TimeSpan timeSpan1 = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = dateTime.Ticks - timeSpan1.Ticks; if (ticks < DateTime.MinValue.Ticks) { utcOffset = dateTime.GetUtcOffset(); ticks += utcOffset.Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } dateTime = new DateTime(ticks, DateTimeKind.Local); break; } else { dateTime = (new DateTime(ticks, DateTimeKind.Utc)).ToLocalTime(); break; } } } dt = DateTimeUtils.EnsureDateTime(dateTime, dateTimeZoneHandling); return(true); }