Example #1
0
        public void ElidedTransitions()
        {
            var zone1 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[3], 0, 0, "a" },
                { Instants[4], 1, 2, "b" },
                { Instants[5], 2, 1, "b" },
                { Instants[6], 1, 0, "d" },
                { Instants[7], 1, 0, "e" },
                { Instants[8], 0, 0, "x" },
            }.Build();
            var zone2 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[3], 0, 0, "a" },
                { Instants[4], 3, 0, "b" },
                // Instants[5] isn't included here: wall offset is the same; components change in zone1
                { Instants[6], 1, 0, "d" },
                // Instants[7] isn't included here: offset components are the same; names change in zone1
                { Instants[8], 0, 0, "x" },
            }.Build();

            AssertEqual(zone1, zone2, Instant.MinValue, Instant.MaxValue, Options.OnlyMatchWallOffset);
            // BOT-Instants[6] will elide transitions when ignoring components, even if we match names
            AssertEqual(zone1, zone2, Instant.MinValue, Instants[6], Options.MatchNames);
            AssertNotEqual(zone1, zone2, Instant.MinValue, Instants[6], Options.MatchOffsetComponents);
            // Instants[6]-EOT will elide transitions when ignoring names, even if we match components
            AssertEqual(zone1, zone2, Instants[6], Instant.MaxValue, Options.MatchOffsetComponents);
            AssertNotEqual(zone1, zone2, Instants[6], Instant.MaxValue, Options.MatchNames);

            // But if we require the exact transitions, both fail
            AssertNotEqual(zone1, zone2, Instant.MinValue, Instants[6], Options.MatchAllTransitions);
            AssertNotEqual(zone1, zone2, Instants[6], Instant.MaxValue, Options.MatchAllTransitions);
        }
        public void GetZoneIntervals_WithOptions_NoCoalescing()
        {
            // We'll ask for 1999-2003, so there are three transitions within that.
            var transition1 = Instant.FromUtc(2000, 1, 1, 0, 0);
            var transition2 = Instant.FromUtc(2001, 1, 1, 0, 0);
            var transition3 = Instant.FromUtc(2002, 1, 1, 0, 0);
            // And one transition afterwards.
            var transition4 = Instant.FromUtc(2004, 1, 1, 0, 0);
            var zone        = new MultiTransitionDateTimeZone.Builder(0, "0+0")
            {
                { transition1, 1, 1, "1+1" },
                { transition2, 0, 2, "0+2" },
                { transition3, 0, 1, "0+1" },
                { transition4, 0, 0, "0+0" }
            }.Build();
            var interval = new Interval(
                Instant.FromUtc(1999, 1, 1, 0, 0),
                Instant.FromUtc(2003, 1, 1, 0, 0));
            // No coalescing required, as the names are different.
            var zoneIntervals = zone.GetZoneIntervals(interval, ZoneEqualityComparer.Options.MatchNames).ToList();

            Assert.AreEqual(4, zoneIntervals.Count);
            CollectionAssert.AreEqual(new[] { transition1, transition2, transition3, transition4 },
                                      zoneIntervals.Select(zi => zi.End));
        }
        public void GetZoneIntervals_WithOptions_Coalescing()
        {
            // We'll ask for 1999-2003, so there are three transitions within that.
            var transition1 = Instant.FromUtc(2000, 1, 1, 0, 0);
            var transition2 = Instant.FromUtc(2001, 1, 1, 0, 0);
            var transition3 = Instant.FromUtc(2002, 1, 1, 0, 0);
            // And one transition afterwards.
            var transition4 = Instant.FromUtc(2004, 1, 1, 0, 0);
            var zone        = new MultiTransitionDateTimeZone.Builder(0, "0+0")
            {
                { transition1, 1, 1, "1+1" },
                { transition2, 0, 2, "0+2" },
                { transition3, 0, 1, "0+1" },
                { transition4, 0, 0, "0+0" }
            }.Build();
            var interval = new Interval(
                Instant.FromUtc(1999, 1, 1, 0, 0),
                Instant.FromUtc(2003, 1, 1, 0, 0));
            // The zone intervals abutting at transition2 are coalesced,
            // because that only changes the name and standard/daylight split.
            var zoneIntervals = zone.GetZoneIntervals(interval, ZoneEqualityComparer.Options.OnlyMatchWallOffset).ToList();

            Assert.AreEqual(3, zoneIntervals.Count);
            CollectionAssert.AreEqual(new[] { transition1, transition3, transition4 },
                                      zoneIntervals.Select(zi => zi.End));
            CollectionAssert.AreEqual(new[] { Instant.BeforeMinValue, transition1, transition3 },
                                      zoneIntervals.Select(zi => zi.RawStart));
            CollectionAssert.AreEqual(new[] { "0+0", "1+1", "0+1" },
                                      zoneIntervals.Select(zi => zi.Name));
        }
Example #4
0
        public void BuildTwice()
        {
            var builder = new MultiTransitionDateTimeZone.Builder();

            builder.Build();
            Assert.Throws <InvalidOperationException>(() => builder.Build());
        }
Example #5
0
        public void Transitions()
        {
            var transition1 = Instant.FromUnixTimeTicks(0L);
            var transition2 = Instant.FromUnixTimeTicks(100000L);
            var zone        = new MultiTransitionDateTimeZone.Builder
            {
                { transition1, 5 },
                { transition2, 3 }
            }.Build();

            CollectionAssert.AreEqual(new[] { transition1, transition2 }, zone.Transitions);
        }
        public void ComplexBuilding()
        {
            var transition1 = Instant.FromUnixTimeTicks(0L);
            var transition2 = Instant.FromUnixTimeTicks(100000L);
            var zone        = new MultiTransitionDateTimeZone.Builder(2, 1, "X")
            {
                { transition1, 2, 0, "Y" },
                { transition2, 1, 1, "Z" }
            }.Build();
            var actual = zone.GetZoneIntervals(transition1 - Duration.Epsilon, transition2 + Duration.Epsilon).ToList();
            // ZoneInterval uses wall offset and savings...
            var expected = new[]
            {
                new ZoneInterval("X", Instant.BeforeMinValue, transition1, Offset.FromHours(3), Offset.FromHours(1)),
                new ZoneInterval("Y", transition1, transition2, Offset.FromHours(2), Offset.FromHours(0)),
                new ZoneInterval("Z", transition2, Instant.AfterMaxValue, Offset.FromHours(2), Offset.FromHours(1)),
            };

            CollectionAssert.AreEqual(expected, actual);
        }
Example #7
0
        public void ElidedTransitions_Degenerate()
        {
            // Transitions with *nothing* that we care about. (Normally
            // these wouldn't even be generated, but we could imagine some
            // sort of zone interval in the future which had another property...)
            var zone1 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[3], 1, 0, "a" },
                { Instants[4], 1, 0, "a" },
                { Instants[5], 1, 0, "a" },
                { Instants[6], 0 }
            }.Build();
            var zone2 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[3], 1, 0, "a" },
                { Instants[6], 0 }
            }.Build();

            // We can match *everything* except exact transitions...
            AssertEqual(zone1, zone2, Instant.MinValue, Instant.MaxValue, Options.MatchNames | Options.MatchOffsetComponents | Options.MatchStartAndEndTransitions);
            // But not the exact transitions...
            AssertNotEqual(zone1, zone2, Instant.MinValue, Instant.MaxValue, Options.MatchAllTransitions);
        }
Example #8
0
        public void Various()
        {
            // Names, some offsets, and first transition are all different.
            var zone1 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[0], 1, 0, "xx" },
                { Instants[2], 3, 0, "1b" },
                { Instants[4], 2, 1, "1c" },
                { Instants[6], 4, 0, "1d" },
            }.Build();
            var zone2 = new MultiTransitionDateTimeZone.Builder
            {
                { Instants[1], 1, 0, "xx" },
                { Instants[2], 3, 0, "2b" },
                { Instants[4], 1, 2, "2c" },
                { Instants[6], 5, 0, "2d" },
            }.Build();

            // Even though the first transition point is different, by default that's fine if
            // the start point is "inside" both.
            AssertEqual(zone1, zone2, Instants[1], Instants[5], Options.OnlyMatchWallOffset);
            // When we extend backwards a bit, we can see the difference between the two.
            AssertNotEqual(zone1, zone2, Instants[1] - Duration.Epsilon, Instants[5], Options.OnlyMatchWallOffset);
            // Or if we force the start and end transitions to be exact...
            AssertNotEqual(zone1, zone2, Instants[1], Instants[5], Options.MatchStartAndEndTransitions);

            // The first two transitions have the same split between standard and saving...
            AssertEqual(zone1, zone2, Instants[1], Instants[4], Options.MatchOffsetComponents);
            // The third one (at Instants[4]) doesn't...
            AssertNotEqual(zone1, zone2, Instants[1], Instants[5], Options.MatchOffsetComponents);

            // The first transition has the same name for the zone interval...
            AssertEqual(zone1, zone2, Instants[1], Instants[2], Options.MatchNames);
            // The second transition (at Instants[2]) doesn't...
            AssertNotEqual(zone1, zone2, Instants[1], Instants[3], Options.MatchNames);
        }
        public void SimpleBuilding()
        {
            var transition1 = Instant.FromUnixTimeTicks(0L);
            var transition2 = Instant.FromUnixTimeTicks(100000L);
            var zone        = new MultiTransitionDateTimeZone.Builder
            {
                { transition1, 5 },
                { transition2, 3 }
            }.Build();
            var intervals = zone.GetZoneIntervals(transition1 - Duration.Epsilon, transition2 + Duration.Epsilon).ToList();

            Assert.AreEqual(3, intervals.Count);
            Assert.AreEqual(Offset.Zero, intervals[0].WallOffset);
            Assert.AreEqual(Instant.BeforeMinValue, intervals[0].RawStart);
            Assert.AreEqual(transition1, intervals[0].End);

            Assert.AreEqual(Offset.FromHours(5), intervals[1].WallOffset);
            Assert.AreEqual(transition1, intervals[1].Start);
            Assert.AreEqual(transition2, intervals[1].End);

            Assert.AreEqual(Offset.FromHours(3), intervals[2].WallOffset);
            Assert.AreEqual(transition2, intervals[2].Start);
            Assert.AreEqual(Instant.AfterMaxValue, intervals[2].RawEnd);
        }
Example #10
0
        public void BuilderGetEnumerator_Throws()
        {
            var builder = new MultiTransitionDateTimeZone.Builder();

            Assert.Throws <NotImplementedException>(() => ((IEnumerable)builder).GetEnumerator());
        }