public static DateTime ConvertUtcToLocation(DateTimeZone dtz, DateTime utc) { DateTimeZone utcTimeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("UTC"); ZoneLocalMappingResolver customResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter); var local = LocalDateTime.FromDateTime(utc); var utcZoned = utcTimeZone.ResolveLocal(local, customResolver); var locationZoned = utcZoned.ToInstant().InZone(dtz).ToDateTimeUnspecified(); return(locationZoned); }
static void ResolveLocal(LocalDateTime input, DateTimeZone zone) { // This can be cached in a static field; it's thread-safe. var resolver = Resolvers.CreateMappingResolver( Resolvers.ReturnEarlier, ShiftForward); var result = input.InZone(zone, resolver); Console.WriteLine("{0} => {1}", input, result); }
public void CreateResolver_Skipped() { ZonedDateTime zoned = new ZonedDateTime(TimeInTransition.PlusDays(1).WithOffset(GapZone.EarlyInterval.WallOffset), GapZone); AmbiguousTimeResolver ambiguityResolver = (earlier, later) => { Assert.Fail("Shouldn't be called"); return(default(ZonedDateTime)); }; SkippedTimeResolver skippedTimeResolver = (local, zone, before, after) => zoned; var resolver = Resolvers.CreateMappingResolver(ambiguityResolver, skippedTimeResolver); var resolved = resolver(GapZone.MapLocal(TimeInTransition)); Assert.AreEqual(zoned, resolved); }
public void CreateResolver_Unambiguous() { AmbiguousTimeResolver ambiguityResolver = (earlier, later) => { Assert.Fail("Shouldn't be called"); return(default(ZonedDateTime)); }; SkippedTimeResolver skippedTimeResolver = (local, zone, before, after) => { Assert.Fail("Shouldn't be called"); return(default(ZonedDateTime)); }; var resolver = Resolvers.CreateMappingResolver(ambiguityResolver, skippedTimeResolver); LocalDateTime localTime = new LocalDateTime(1900, 1, 1, 0, 0); var resolved = resolver(GapZone.MapLocal(localTime)); Assert.AreEqual(new ZonedDateTime(localTime.WithOffset(GapZone.EarlyInterval.WallOffset), GapZone), resolved); }
public void Construct_FromLocal_ValidLaterOffset() { SingleTransitionDateTimeZone zone = new SingleTransitionDateTimeZone(Instant.FromUtc(2011, 6, 12, 22, 0), 4, 3); LocalDateTime local = new LocalDateTime(2011, 6, 13, 1, 30); ZonedDateTime zoned = new ZonedDateTime(local, zone, zone.LateInterval.WallOffset); // Map the local time to the later of the offsets in a way which is tested elsewhere. var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ThrowWhenSkipped); Assert.AreEqual(zoned, local.InZone(zone, resolver)); }
static void Main() { var local = new LocalDateTime(2014, 3, 30, 2, 30, 0); var zone = DateTimeZoneProviders.Tzdb["Europe/Paris"]; var resolver = Resolvers.CreateMappingResolver( ambiguousTimeResolver: Resolvers.ReturnEarlier, skippedTimeResolver: Resolvers.ReturnEndOfIntervalBefore); var zoned = local.InZone(zone, resolver); Console.WriteLine(zoned); // 2014-03-30T01:59:59 Europe/Paris (+01) }
public void CuriousSubtraction() { var shanghai = DateTimeZoneProviders.Tzdb["Asia/Shanghai"]; var localBefore = new LocalDateTime(1927, 12, 31, 23, 54, 02); var localAfter = localBefore.PlusSeconds(1); var instantBefore = localBefore.InZoneLeniently(shanghai).ToInstant(); var instantAfter = localAfter.InZoneLeniently(shanghai).ToInstant(); Assert.AreEqual(Duration.FromSeconds(358), instantAfter - instantBefore); // Now let's resolve them differently... var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, Resolvers.ThrowWhenSkipped); instantBefore = localBefore.InZone(shanghai, resolver).ToInstant(); instantAfter = localAfter.InZone(shanghai, resolver).ToInstant(); Assert.AreEqual(Duration.FromSeconds(1), instantAfter - instantBefore); }
public void CuriousSubtraction() { var shanghai = DateTimeZoneProviders.Tzdb["Asia/Shanghai"]; var localBefore = new LocalDateTime(1900, 12, 31, 23, 54, 16); var localAfter = localBefore.PlusSeconds(1); // Note: The behavior of NodaTime's Lenient resolver changed in 2.0, which deviates from the problem described in the original post. var oldLenientResolver = Resolvers.CreateMappingResolver(Resolvers.ReturnLater, Resolvers.ReturnStartOfIntervalAfter); var instantBefore = localBefore.InZone(shanghai, oldLenientResolver).ToInstant(); var instantAfter = localAfter.InZone(shanghai, oldLenientResolver).ToInstant(); Assert.AreEqual(Duration.FromSeconds(344), instantAfter - instantBefore); // Now let's resolve them differently... var resolver = Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, Resolvers.ThrowWhenSkipped); instantBefore = localBefore.InZone(shanghai, resolver).ToInstant(); instantAfter = localAfter.InZone(shanghai, resolver).ToInstant(); Assert.AreEqual(Duration.FromSeconds(1), instantAfter - instantBefore); }