public void XmlSerialization_Iso() { var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.FromHours(1)); TestHelper.AssertXmlRoundtrip(value, "<value>2013-04-12T17:53:23+01:00</value>"); }
public void Subtraction_OffsetDateTime() { // Test all three approaches... not bothering to check a different calendar, // but we'll use two different offsets. OffsetDateTime start = new LocalDateTime(2014, 08, 14, 6, 51).WithOffset(Offset.FromHours(1)); OffsetDateTime end = new LocalDateTime(2014, 08, 14, 18, 0).WithOffset(Offset.FromHours(4)); Duration expected = Duration.FromHours(8) + Duration.FromMinutes(9); Assert.AreEqual(expected, end - start); Assert.AreEqual(expected, end.Minus(start)); Assert.AreEqual(expected, OffsetDateTime.Subtract(end, start)); }
public void BinarySerialization() { TestHelper.AssertBinaryRoundtrip( new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23).PlusNanoseconds(1), Offset.FromHours(1))); TestHelper.AssertBinaryRoundtrip( new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23, CalendarSystem.Julian).PlusNanoseconds(1), Offset.FromHours(1))); }
public void InstantComparer() { var localControl = new LocalDateTime(2013, 4, 2, 19, 54); var control = new OffsetDateTime(localControl, Offset.Zero); var equalAndOppositeChanges = control.LocalDateTime.PlusHours(1).WithOffset(Offset.FromHours(1)); var differentCalendar = control.LocalDateTime.WithCalendar(CalendarSystem.Coptic).WithOffset(Offset.Zero); // Negative offset means later instant var negativeOffset = control.LocalDateTime.WithOffset(Offset.FromHours(-1)); // Positive offset means earlier instant var positiveOffset = control.LocalDateTime.WithOffset(Offset.FromHours(1)); // Later instant, earlier local var earlierLocal = control.LocalDateTime.PlusHours(-2).WithOffset(Offset.FromHours(-10)); // Earlier instant, later local var laterLocal = control.LocalDateTime.PlusHours(2).WithOffset(Offset.FromHours(10)); var comparer = OffsetDateTime.Comparer.Instant; Assert.AreEqual(0, comparer.Compare(control, differentCalendar)); Assert.AreEqual(0, comparer.Compare(control, equalAndOppositeChanges)); Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, negativeOffset))); Assert.AreEqual(1, Math.Sign(comparer.Compare(negativeOffset, control))); Assert.AreEqual(1, comparer.Compare(control, positiveOffset)); Assert.AreEqual(-1, Math.Sign(comparer.Compare(positiveOffset, control))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, earlierLocal))); Assert.AreEqual(1, Math.Sign(comparer.Compare(earlierLocal, control))); Assert.AreEqual(1, Math.Sign(comparer.Compare(control, laterLocal))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(laterLocal, control))); Assert.IsTrue(comparer.Equals(control, differentCalendar)); Assert.IsFalse(comparer.Equals(control, earlierLocal)); Assert.IsTrue(comparer.Equals(control, equalAndOppositeChanges)); Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(differentCalendar)); Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(equalAndOppositeChanges)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(earlierLocal)); }
public void Addition_Duration() { const int minutes = 23; const int hours = 3; const int milliseconds = 40000; const long seconds = 321; const long nanoseconds = 12345; const long ticks = 5432112345; // Test all three approaches... not bothering to check a different calendar, // but we'll use two different offsets. OffsetDateTime start = new LocalDateTime(2014, 08, 14, 6, 51).WithOffset(Offset.FromHours(1)); Duration duration = Duration.FromHours(8) + Duration.FromMinutes(9); OffsetDateTime expected = new LocalDateTime(2014, 08, 14, 15, 0).WithOffset(Offset.FromHours(1)); Assert.AreEqual(expected, start + duration); Assert.AreEqual(expected, start.Plus(duration)); Assert.AreEqual(expected, OffsetDateTime.Add(start, duration)); Assert.AreEqual(start + Duration.FromHours(hours), start.PlusHours(hours)); Assert.AreEqual(start + Duration.FromHours(-hours), start.PlusHours(-hours)); Assert.AreEqual(start + Duration.FromMinutes(minutes), start.PlusMinutes(minutes)); Assert.AreEqual(start + Duration.FromMinutes(-minutes), start.PlusMinutes(-minutes)); Assert.AreEqual(start + Duration.FromSeconds(seconds), start.PlusSeconds(seconds)); Assert.AreEqual(start + Duration.FromSeconds(-seconds), start.PlusSeconds(-seconds)); Assert.AreEqual(start + Duration.FromMilliseconds(milliseconds), start.PlusMilliseconds(milliseconds)); Assert.AreEqual(start + Duration.FromMilliseconds(-milliseconds), start.PlusMilliseconds(-milliseconds)); Assert.AreEqual(start + Duration.FromTicks(ticks), start.PlusTicks(ticks)); Assert.AreEqual(start + Duration.FromTicks(-ticks), start.PlusTicks(-ticks)); Assert.AreEqual(start + Duration.FromNanoseconds(nanoseconds), start.PlusNanoseconds(nanoseconds)); Assert.AreEqual(start + Duration.FromNanoseconds(-nanoseconds), start.PlusNanoseconds(-nanoseconds)); }
public void WithCalendar() { CalendarSystem julianCalendar = CalendarSystem.Julian; OffsetDateTime gregorianEpoch = NodaConstants.UnixEpoch.WithOffset(Offset.Zero); OffsetDateTime expected = new LocalDate(1969, 12, 19, julianCalendar).AtMidnight().WithOffset(Offset.FromHours(0)); OffsetDateTime actual = gregorianEpoch.WithCalendar(CalendarSystem.Julian); Assert.AreEqual(expected, actual); }
public void SkippedStartOfDay_TransitionBeforeMidnight() { // 12.20am because of the skip var expected = new ZonedDateTime(new LocalDateTime(2000, 6, 1, 0, 20).WithOffset(Offset.FromHours(-1)), TransitionForwardBeforeMidnightZone); var actual = TransitionForwardBeforeMidnightZone.AtStartOfDay(TransitionDate); Assert.AreEqual(expected, actual); }
public void FindLongestZoneId() { var source = new FakeDateTimeZoneSource.Builder { CreateZone("ABC"), CreateZone("ABCA"), CreateZone("ABCB"), CreateZone("ABCBX"), CreateZone("ABCD") }.Build(); var provider = new DateTimeZoneCache(source); var pattern = ZonedDateTimePattern.Create("z 'x'", CultureInfo.InvariantCulture, Resolvers.StrictResolver, provider, NodaConstants.UnixEpoch.InUtc()); foreach (var id in provider.Ids) { var value = pattern.Parse($"{id} x").Value; Assert.AreEqual(id, value.Zone.Id); } DateTimeZone CreateZone(string id) => new SingleTransitionDateTimeZone(NodaConstants.UnixEpoch - Duration.FromDays(1), Offset.FromHours(-1), Offset.FromHours(0), id); }
public void FromTimeSpan_Simple() { TimeSpan ts = TimeSpan.FromHours(2); Assert.AreEqual(Offset.FromHours(2), Offset.FromTimeSpan(ts)); }
public void Customize(IFixture fixture) { fixture.Register(() => new NodaTimeOffsetTime { OffsetTime = new OffsetTime(new LocalTime(7, 8, 9), Offset.FromHours(1)), NullableOffsetTime = new OffsetTime(new LocalTime(8, 9, 10), Offset.FromHours(2)), NullableOffsetTimeWithNull = null }); }
public void ZonedDateTimeConverter() { // Deliberately give it an ambiguous local time, in both ways. var zone = DateTimeZoneProviders.Tzdb["Europe/London"]; var earlierValue = new ZonedDateTime(new LocalDateTime(2012, 10, 28, 1, 30), zone, Offset.FromHours(1)); var laterValue = new ZonedDateTime(new LocalDateTime(2012, 10, 28, 1, 30), zone, Offset.FromHours(0)); string earlierJson = "2012-10-28T01:30:00+01 Europe/London"; string laterJson = "2012-10-28T01:30:00Z Europe/London"; var converter = NodaSerializerDefinitions.CreateZonedDateTimeSerializer(DateTimeZoneProviders.Tzdb); AssertConversions(earlierValue, earlierJson, converter); AssertConversions(laterValue, laterJson, converter); }
// We don't care about the details of the time zones, just the IDs private static DateTimeZone CreateZone(string id) { return(new SingleTransitionDateTimeZone(NodaConstants.UnixEpoch, Offset.FromHours(1), Offset.FromHours(2), id)); }
public void IsTransitionFrom_laterInstantAndEqualButOppositeStandardAndSavings_true() { var newValue = new ZoneTransition(NodaConstants.UnixEpoch + Duration.Epsilon, "abc", Offset.FromHours(1), Offset.Zero); var oldValue = new ZoneTransition(NodaConstants.UnixEpoch, "abc", Offset.Zero, Offset.FromHours(1)); Assert.True(newValue.IsTransitionFrom(oldValue)); }
public void UnambiguousStartOfDay() { // Just a simple midnight in March. var expected = new ZonedDateTime(new LocalDateTime(2000, 3, 1, 0, 0).WithOffset(Offset.FromHours(-2)), TransitionForwardAtMidnightZone); var actual = TransitionForwardAtMidnightZone.AtStartOfDay(new LocalDate(2000, 3, 1)); Assert.AreEqual(expected, actual); }
public void WithOffset_CrossDates() { OffsetDateTime noon = new OffsetDateTime(new LocalDateTime(2017, 8, 22, 12, 0, 0), Offset.FromHours(0)); OffsetDateTime previousNight = noon.WithOffset(Offset.FromHours(-14)); OffsetDateTime nextMorning = noon.WithOffset(Offset.FromHours(14)); Assert.AreEqual(new LocalDateTime(2017, 8, 21, 22, 0, 0), previousNight.LocalDateTime); Assert.AreEqual(new LocalDateTime(2017, 8, 23, 2, 0, 0), nextMorning.LocalDateTime); }
public void SafePlus_NormalTime() { var localInstant = NodaConstants.UnixEpoch.SafePlus(Offset.FromHours(1)); Assert.AreEqual(Duration.FromHours(1), localInstant.TimeSinceLocalEpoch); }
public void WithOffset_TwoDaysForwardAndBack() { // Go from UTC-18 to UTC+18 OffsetDateTime night = new OffsetDateTime(new LocalDateTime(2017, 8, 21, 18, 0, 0), Offset.FromHours(-18)); OffsetDateTime morning = night.WithOffset(Offset.FromHours(18)); Assert.AreEqual(new LocalDateTime(2017, 8, 23, 6, 0, 0), morning.LocalDateTime); OffsetDateTime backAgain = morning.WithOffset(Offset.FromHours(-18)); Assert.AreEqual(night, backAgain); }
public void LocalComparer() { var localControl = new LocalDateTime(2013, 4, 2, 19, 54); var control = new OffsetDateTime(localControl, Offset.Zero); var negativeOffset = control.LocalDateTime.WithOffset(Offset.FromHours(-1)); var positiveOffset = control.LocalDateTime.WithOffset(Offset.FromHours(1)); var differentCalendar = control.LocalDateTime.WithCalendar(CalendarSystem.Coptic).WithOffset(Offset.FromHours(5)); // Later instant, earlier local var earlierLocal = control.LocalDateTime.PlusHours(-2).WithOffset(Offset.FromHours(-10)); // Same offset, previous day var muchEarlierLocal = control.PlusHours(-24); // Earlier instant, later local var laterLocal = control.LocalDateTime.PlusHours(2).WithOffset(Offset.FromHours(10)); // Same offset, next day var muchLaterLocal = control.PlusHours(24); var comparer = OffsetDateTime.Comparer.Local; Assert.AreEqual(0, comparer.Compare(control, negativeOffset)); Assert.AreEqual(0, comparer.Compare(control, positiveOffset)); Assert.Throws <ArgumentException>(() => comparer.Compare(control, differentCalendar)); Assert.AreEqual(1, Math.Sign(comparer.Compare(control, earlierLocal))); Assert.AreEqual(1, Math.Sign(comparer.Compare(control, muchEarlierLocal))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(earlierLocal, control))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(muchEarlierLocal, control))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, laterLocal))); Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, muchLaterLocal))); Assert.AreEqual(1, Math.Sign(comparer.Compare(laterLocal, control))); Assert.AreEqual(1, Math.Sign(comparer.Compare(muchLaterLocal, control))); Assert.IsFalse(comparer.Equals(control, differentCalendar)); Assert.IsFalse(comparer.Equals(control, earlierLocal)); Assert.IsFalse(comparer.Equals(control, muchEarlierLocal)); Assert.IsFalse(comparer.Equals(control, laterLocal)); Assert.IsFalse(comparer.Equals(control, muchLaterLocal)); Assert.IsTrue(comparer.Equals(control, control)); Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(negativeOffset)); Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(positiveOffset)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(earlierLocal)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(muchEarlierLocal)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(laterLocal)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(muchLaterLocal)); Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(differentCalendar)); }
public void XmlSerialization_Iso() { DateTimeZoneProviders.Serialization = DateTimeZoneProviders.Tzdb; var zone = DateTimeZoneProviders.Tzdb["America/New_York"]; var value = new ZonedDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23).WithOffset(Offset.FromHours(-4)), zone); TestHelper.AssertXmlRoundtrip(value, "<value zone=\"America/New_York\">2013-04-12T17:53:23-04</value>"); }
public void AmbiguousStartOfDay_TransitionAfterMidnight() { // Occurrence before transition var expected = new ZonedDateTime(new LocalDateTime(2000, 6, 1, 0, 0).WithOffset(Offset.FromHours(-2)), TransitionBackwardAfterMidnightZone); var actual = TransitionBackwardAfterMidnightZone.AtStartOfDay(TransitionDate); Assert.AreEqual(expected, actual); }