Exemplo n.º 1
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));
                }
            }
        }