Esempio n. 1
0
        internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt)
        {
            var dateTimeParser = new DateTimeParser();

            if (!dateTimeParser.Parse(text))
            {
                dt = null;
                return(false);
            }

            var 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:
                {
                    var 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:
                {
                    var 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)
		{
			var dateTimeParser = new DateTimeParser();
			if (!dateTimeParser.Parse(text))
			{
				dt = null;
				return false;
			}

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