Beispiel #1
0
    static void Main(string[] args)
    {
        var pattern = LocalDateTimePattern.CreateWithInvariantInfo
                          ("MMM dd, yyyy HH:mm:ss tt");
        LocalDateTime start = pattern.Parse("Nov 06, 2011 01:59:58 AM").Value;
        LocalDateTime end   = pattern.Parse("Nov 06, 2011 01:00:00 AM").Value;

        DateTimeZone zone = DateTimeZone.ForId("America/Chicago");

        // Where this is ambiguous, pick the earlier option
        ZonedDateTime zonedStart = zone.AtEarlier(start);
        // Where this is ambiguous, pick the later option
        ZonedDateTime zonedEnd = zone.AtLater(start);

        Duration duration = zonedEnd.ToInstant() - zonedStart.ToInstant();

        // Prints 01:00:00
        Console.WriteLine(duration.ToTimeSpan());
    }
        private static void AssertAmbiguous(LocalDateTime localTime, DateTimeZone zone)
        {
            ZonedDateTime earlier = zone.AtEarlier(localTime);
            ZonedDateTime later = zone.AtLater(localTime);
            Assert.AreEqual(localTime, earlier.LocalDateTime);
            Assert.AreEqual(localTime, later.LocalDateTime);
            Assert.That(earlier.ToInstant(), Is.LessThan(later.ToInstant()));

            try
            {
                zone.AtExactly(localTime);
                Assert.Fail("Expected exception");
            }
            catch (AmbiguousTimeException e)
            {
                Assert.AreEqual(localTime, e.LocalDateTime);
                Assert.AreEqual(zone, e.Zone);
                Assert.AreEqual(earlier, e.EarlierMapping);
                Assert.AreEqual(later, e.LaterMapping);
            }
        }