Ejemplo n.º 1
0
        public static bool TryParse(string value, DateTimeZone timeZone, out ZonedDateTime instant)
        {
            if (DateTimeOffset.TryParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"
                                             , CultureInfo.InvariantCulture, DateTimeStyles.None, out var offset) &&
                !DateTime.TryParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFF"
                                        , CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var buffer))
            {
                instant = new ZonedDateTime(offset.UtcDateTime, timeZone);
                return(true);
            }
            else if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var date))
            {
                if (timeZone == DateTimeZone.Utc)
                {
                    instant = new ZonedDateTime(DateTime.SpecifyKind(date, DateTimeKind.Utc), timeZone);
                }
                else
                {
                    date    = DateTime.SpecifyKind(date, DateTimeKind.Unspecified);
                    date    = DateTime.SpecifyKind(DateTimeZone.ConvertTime(date, timeZone, DateTimeZone.Utc), DateTimeKind.Utc);
                    instant = new ZonedDateTime(date, timeZone);
                }
                return(true);
            }

            instant = new ZonedDateTime();
            return(false);
        }
Ejemplo n.º 2
0
        public void TimeZoneTests()
        {
            var zonesToIgnore = new HashSet <string>();

            foreach (var zone in TimeZoneInfo.GetSystemTimeZones().Where(t => !zonesToIgnore.Contains(t.Id)))
            {
                for (var i = 0; i < 4; i++)
                {
                    var summer   = new DateTime(2017 + i, 7, 1, 12, 0, 0, DateTimeKind.Unspecified);
                    var winter   = new DateTime(2017 + i, 1, 1, 12, 0, 0, DateTimeKind.Unspecified);
                    var custZone = DateTimeZone.ById(zone.Id);

                    Assert.AreEqual(TimeZoneInfo.ConvertTime(summer, TimeZoneInfo.Utc, zone)
                                    , DateTimeZone.ConvertTime(summer, DateTimeZone.Utc, custZone));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(summer, zone, TimeZoneInfo.Utc)
                                    , DateTimeZone.ConvertTime(summer, custZone, DateTimeZone.Utc));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(winter, TimeZoneInfo.Utc, zone)
                                    , DateTimeZone.ConvertTime(winter, DateTimeZone.Utc, custZone));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(winter, zone, TimeZoneInfo.Utc)
                                    , DateTimeZone.ConvertTime(winter, custZone, DateTimeZone.Utc));

                    Assert.AreEqual(zone.GetUtcOffset(summer), custZone.GetUtcOffset(summer));
                    Assert.AreEqual(zone.GetUtcOffset(winter), custZone.GetUtcOffset(winter));

                    Assert.AreEqual(TimeZoneInfo.ConvertTime(new DateTimeOffset(summer, TimeSpan.FromHours(3)), zone)
                                    , DateTimeZone.ConvertTime(new DateTimeOffset(summer, TimeSpan.FromHours(3)), custZone));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(new DateTimeOffset(winter, TimeSpan.FromHours(3)), zone)
                                    , DateTimeZone.ConvertTime(new DateTimeOffset(winter, TimeSpan.FromHours(3)), custZone));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(new DateTimeOffset(summer, TimeSpan.FromHours(-7.5)), zone)
                                    , DateTimeZone.ConvertTime(new DateTimeOffset(summer, TimeSpan.FromHours(-7.5)), custZone));
                    Assert.AreEqual(TimeZoneInfo.ConvertTime(new DateTimeOffset(winter, TimeSpan.FromHours(-7.5)), zone)
                                    , DateTimeZone.ConvertTime(new DateTimeOffset(winter, TimeSpan.FromHours(-7.5)), custZone));
                }
            }
        }
Ejemplo n.º 3
0
        private DateTime ZoneToUtc(DateTime value)
        {
            if (Zone == DateTimeZone.Utc)
            {
                return(DateTime.SpecifyKind(value, DateTimeKind.Utc));
            }

            value = DateTime.SpecifyKind(value, DateTimeKind.Unspecified);
            return(DateTime.SpecifyKind(DateTimeZone.ConvertTime(value, Zone, DateTimeZone.Utc), DateTimeKind.Utc));
        }
Ejemplo n.º 4
0
 public ZonedDateTime(DateTime value, DateTimeZone timeZone)
 {
     if (value.Kind == DateTimeKind.Utc)
     {
         UtcDateTime = DateTime.SpecifyKind(value, DateTimeKind.Utc);
     }
     else
     {
         UtcDateTime = DateTime.SpecifyKind(DateTimeZone.ConvertTime(value, DateTimeZone.Local, DateTimeZone.Utc), DateTimeKind.Utc);
     }
     Zone = timeZone;
 }
Ejemplo n.º 5
0
        private string Render(DateTime value)
        {
            var converted = value;

            if (value.Kind == DateTimeKind.Utc)
            {
                if (_timeZone != DateTimeZone.Utc)
                {
                    converted = DateTimeZone.ConvertTime(value, DateTimeZone.Utc, _timeZone);
                }
            }
            else if (_timeZone != DateTimeZone.Local) // Assume local
            {
                converted = DateTimeZone.ConvertTime(value, DateTimeZone.Local, _timeZone);
            }
            return(converted.ToString("s"));
        }
Ejemplo n.º 6
0
        public static bool TryParseExact(string value, string format, IFormatProvider provider, DateTimeStyles styles, DateTimeZone timeZone, out ZonedDateTime instant)
        {
            if ((format.IndexOf('K') >= 0 || format.IndexOf('K') >= 0) &&
                DateTimeOffset.TryParseExact(value, format, provider, styles, out var offset))
            {
                instant = new ZonedDateTime(offset.UtcDateTime, timeZone);
                return(true);
            }
            else if (DateTime.TryParseExact(value, format, provider, styles, out var date))
            {
                date    = DateTime.SpecifyKind(date, DateTimeKind.Unspecified);
                date    = DateTime.SpecifyKind(DateTimeZone.ConvertTime(date, timeZone, DateTimeZone.Utc), DateTimeKind.Utc);
                instant = new ZonedDateTime(date, timeZone);
                return(true);
            }

            instant = new ZonedDateTime();
            return(false);
        }
Ejemplo n.º 7
0
        public int CompareTo(object obj)
        {
            if (obj is ZonedDateTime zonded)
            {
                return(CompareTo(zonded));
            }
            if (obj is DateTimeOffset offset)
            {
                return(UtcDateTime.CompareTo(offset.UtcDateTime));
            }
            if (!(obj is DateTime date))
            {
                throw new NotSupportedException();
            }

            if (date.Kind == DateTimeKind.Utc)
            {
                return(UtcDateTime.CompareTo(date));
            }

            return(UtcDateTime.CompareTo(DateTimeZone.ConvertTime(date, DateTimeZone.Local, DateTimeZone.Utc)));
        }