public void ZoneWithAmbiguity_MidTransition() { var actual = ZoneWithAmbiguity.GetZoneIntervalPair(MidTransition); var expected = ZoneIntervalPair.Ambiguous(IntervalBeforeAmbiguity, IntervalAfterAmbiguity); Assert.AreEqual(expected, actual); }
public void ZoneWithAmbiguity_LastTickOfTransition() { var actual = ZoneWithAmbiguity.GetZoneIntervalPair(TransitionPlus10 - Duration.Epsilon); var expected = ZoneIntervalPair.Ambiguous(IntervalBeforeAmbiguity, IntervalAfterAmbiguity); Assert.AreEqual(expected, actual); }
public void MatchingIntervals_TwoIntervals() { ZoneIntervalPair pair = ZoneIntervalPair.Ambiguous( new ZoneInterval("Foo", new Instant(0), new Instant(10), Offset.Zero, Offset.Zero), new ZoneInterval("Bar", new Instant(10), new Instant(20), Offset.Zero, Offset.Zero)); Assert.AreEqual(2, pair.MatchingIntervals); }
/// <summary> /// Finds all zone intervals for the given local instant. Usually there's one (i.e. only a single /// instant is mapped to the given local instant within the time zone) but during DST transitions /// there can be either 0 (the given local instant doesn't exist, e.g. local time skipped from 1am to /// 2am, but you gave us 1.30am) or 2 (the given local instant is ambiguous, e.g. local time skipped /// from 2am to 1am, but you gave us 1.30am). /// </summary> /// <remarks> /// This method is implemented in terms of GetZoneInterval(Instant) within DateTimeZone, /// and should work for any zone. However, internal derived classes may override this method /// for optimization purposes, e.g. if the zone interval is always ambiguous with /// a fixed value. /// </remarks> /// <param name="localInstant">The local instant to find matching zone intervals for</param> /// <returns>The struct containing up to two ZoneInterval references.</returns> internal virtual ZoneIntervalPair GetZoneIntervalPair(LocalInstant localInstant) { Instant firstGuess = new Instant(localInstant.Ticks); ZoneInterval interval = GetZoneInterval(firstGuess); // Most of the time we'll go into here... the local instant and the instant // are close enough that we've found the right instant. if (interval.Contains(localInstant)) { ZoneInterval earlier = GetEarlierMatchingInterval(interval, localInstant); if (earlier != null) { return(ZoneIntervalPair.Ambiguous(earlier, interval)); } ZoneInterval later = GetLaterMatchingInterval(interval, localInstant); if (later != null) { return(ZoneIntervalPair.Ambiguous(interval, later)); } return(ZoneIntervalPair.Unambiguous(interval)); } else { // Our first guess was wrong. Either we need to change interval by one (either direction) // or we're in a gap. ZoneInterval earlier = GetEarlierMatchingInterval(interval, localInstant); if (earlier != null) { return(ZoneIntervalPair.Unambiguous(earlier)); } ZoneInterval later = GetLaterMatchingInterval(interval, localInstant); if (later != null) { return(ZoneIntervalPair.Unambiguous(later)); } return(ZoneIntervalPair.NoMatch); } }