/// <summary> /// Tries to work out a roughly-matching calendar system for the given BCL calendar. /// This is needed where we're testing whether days of the week match - even if we can /// get day/month/year values to match without getting the calendar right, the calendar /// affects the day of week. /// </summary> internal static CalendarSystem CalendarSystemForCalendar(Calendar bcl) { if (bcl is GregorianCalendar) { return(CalendarSystem.Iso); } if (bcl is HijriCalendar) { return(CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Astronomical)); } // This is *not* a general rule. Noda Time simply doesn't support this calendar, which requires // table-based data. However, using the Islamic calendar with the civil epoch gives the same date for // our sample, which is good enough for now... if (bcl is UmAlQuraCalendar) { // ... On Mono, this actually might not be good enough. So let's just punt on it - the Mono // implementation of UmAlQuraCalendar currently differs from the Windows one, but may get fixed // at some point. Let's just abort the test. if (TestHelper.IsRunningOnMono) { return(null); } return(CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Civil)); } if (bcl is JulianCalendar) { return(CalendarSystem.GetJulianCalendar(4)); } // No idea - we can't test with this calendar... return(null); }
public void BinarySerialization() { TestHelper.AssertBinaryRoundtrip( new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.FromHours(1))); TestHelper.AssertBinaryRoundtrip( new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23, CalendarSystem.GetJulianCalendar(3)), Offset.FromHours(1))); }
public void Constructor_PropertiesRoundTrip_CustomCalendar() { LocalDate date = new LocalDate(2023, 7, 27, CalendarSystem.GetJulianCalendar(4)); Assert.AreEqual(2023, date.Year); Assert.AreEqual(7, date.Month); Assert.AreEqual(27, date.Day); }
public void EraProperty() { CalendarSystem calendar = CalendarSystem.GetJulianCalendar(4); LocalDateTime startOfEra = new LocalDateTime(1, 1, 1, 0, 0, 0, calendar); Assert.AreEqual(Era.Common, startOfEra.Era); Assert.AreEqual(Era.BeforeCommon, startOfEra.PlusTicks(-1).Era); }
public void FormatToday_Julian_Utcx() { StubClock clock = new StubClock(Instant.FromUtc(1970, 1, 1, 0, 0)); Diary diary = new Diary(clock, CalendarSystem.GetJulianCalendar(7), DateTimeZone.Utc); string today = diary.FormatToday(); Assert.AreEqual("1969-12-19", today); }
public void LocalDateTimeProperty() { LocalDateTime local = new LocalDateTime(2012, 6, 19, 1, 2, 3, 4, 5, CalendarSystem.GetJulianCalendar(5)); Offset offset = Offset.FromHours(5); OffsetDateTime odt = new OffsetDateTime(local, offset); Assert.AreEqual(local, odt.LocalDateTime); }
public void Comparison_IgnoresOriginalCalendar() { LocalDateTime dateTime1 = new LocalDateTime(1900, 1, 1, 10, 30, 0); LocalDateTime dateTime2 = dateTime1.WithCalendar(CalendarSystem.GetJulianCalendar(3)); // Calendar information is propagated into LocalDate, but not into LocalTime Assert.IsFalse(dateTime1.Date == dateTime2.Date); Assert.IsTrue(dateTime1.TimeOfDay == dateTime2.TimeOfDay); }
public void BinarySerialization_NonIso() { DateTimeZoneProviders.Serialization = DateTimeZoneProviders.Tzdb; var zone = DateTimeZoneProviders.Tzdb["America/New_York"]; var value = new ZonedDateTime(new LocalDateTime(2013, 6, 12, 17, 53, 23, CalendarSystem.GetJulianCalendar(3)), Offset.FromHours(-4), zone); TestHelper.AssertBinaryRoundtrip(value); }
public void WithCalendar() { LocalDate isoEpoch = new LocalDate(1970, 1, 1); LocalDate julianEpoch = isoEpoch.WithCalendar(CalendarSystem.GetJulianCalendar(4)); Assert.AreEqual(1969, julianEpoch.Year); Assert.AreEqual(12, julianEpoch.Month); Assert.AreEqual(19, julianEpoch.Day); }
public void FridayEpoch() { CalendarSystem fridayEpochCalendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Civil); CalendarSystem julianCalendar = CalendarSystem.GetJulianCalendar(4); LocalDate fridayEpoch = new LocalDate(1, 1, 1, fridayEpochCalendar); LocalDate fridayEpochJulian = new LocalDate(622, 7, 16, julianCalendar); Assert.AreEqual(fridayEpochJulian, fridayEpoch.WithCalendar(julianCalendar)); }
public void ThursdayEpoch() { CalendarSystem thursdayEpochCalendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Astronomical); CalendarSystem julianCalendar = CalendarSystem.GetJulianCalendar(4); LocalDate thursdayEpoch = new LocalDate(1, 1, 1, thursdayEpochCalendar); LocalDate thursdayEpochJulian = new LocalDate(622, 7, 15, julianCalendar); Assert.AreEqual(thursdayEpochJulian, thursdayEpoch.WithCalendar(julianCalendar)); }
public void XmlSerialization_NonIso() { DateTimeZoneProviders.Serialization = DateTimeZoneProviders.Tzdb; var zone = DateTimeZoneProviders.Tzdb["America/New_York"]; var value = new ZonedDateTime(new LocalDateTime(2013, 6, 12, 17, 53, 23, CalendarSystem.GetJulianCalendar(3)), Offset.FromHours(-4), zone); TestHelper.AssertXmlRoundtrip(value, "<value zone=\"America/New_York\" calendar=\"Julian 3\">2013-06-12T17:53:23-04</value>"); }
public void ComparisonOperators_DifferentCalendars_AlwaysReturnsFalse() { LocalDate date1 = new LocalDate(2011, 1, 2); LocalDate date2 = new LocalDate(2011, 1, 3, CalendarSystem.GetJulianCalendar(4)); // All inequality comparisons return false Assert.IsFalse(date1 < date2); Assert.IsFalse(date1 <= date2); Assert.IsFalse(date1 > date2); Assert.IsFalse(date1 >= date2); }
public void ComparisonOperators_DifferentCalendars_AlwaysReturnsFalse() { LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30); LocalDateTime value2 = new LocalDateTime(2011, 1, 3, 10, 30, CalendarSystem.GetJulianCalendar(4)); // All inequality comparisons return false Assert.IsFalse(value1 < value2); Assert.IsFalse(value1 <= value2); Assert.IsFalse(value1 > value2); Assert.IsFalse(value1 >= value2); }
public void CopticEpoch() { CalendarSystem coptic = CalendarSystem.GetCopticCalendar(4); LocalDateTime copticEpoch = new LocalDateTime(1, 1, 1, 0, 0, coptic); CalendarSystem julian = CalendarSystem.GetJulianCalendar(4); LocalDateTime converted = new LocalDateTime(copticEpoch.LocalInstant, julian); LocalDateTime expected = new LocalDateTime(284, 8, 29, 0, 0, julian); Assert.AreEqual(expected, converted); }
public void Equals_DifferentCalendars() { CalendarSystem calendar = CalendarSystem.GetJulianCalendar(4); LocalDate date1 = new LocalDate(2011, 1, 2, calendar); LocalDate date2 = new LocalDate(2011, 1, 2, CalendarSystem.Iso); Assert.AreNotEqual(date1, date2); Assert.AreNotEqual(date1.GetHashCode(), date2.GetHashCode()); Assert.IsFalse(date1 == date2); Assert.IsTrue(date1 != date2); Assert.IsFalse(date1.Equals(date2)); // IEquatable implementation }
public void CombinationWithTime() { // Test all three approaches in the same test - they're logically equivalent. var calendar = CalendarSystem.GetJulianCalendar(4); LocalDate date = new LocalDate(2014, 3, 28, calendar); LocalTime time = new LocalTime(20, 17, 30); LocalDateTime expected = new LocalDateTime(2014, 3, 28, 20, 17, 30, calendar); Assert.AreEqual(expected, date + time); Assert.AreEqual(expected, date.At(time)); Assert.AreEqual(expected, time.On(date)); }
public void BclUsesAstronomicalEpoch() { Calendar hijri = new HijriCalendar(); DateTime bclDirect = new DateTime(1, 1, 1, 0, 0, 0, 0, hijri, DateTimeKind.Unspecified); CalendarSystem julianCalendar = CalendarSystem.GetJulianCalendar(4); LocalDate julianIslamicEpoch = new LocalDate(622, 7, 15, julianCalendar); LocalDate isoIslamicEpoch = julianIslamicEpoch.WithCalendar(CalendarSystem.Iso); DateTime bclFromNoda = isoIslamicEpoch.AtMidnight().ToDateTimeUnspecified(); Assert.AreEqual(bclDirect, bclFromNoda); }
public void GetInstance_MinDaysInFirstWeekIsRespected() { // Seems the simplest way to test this... yes, it seems somewhat wasteful, but hey... for (int i = 1; i < 7; i++) { CalendarSystem calendar = CalendarSystem.GetJulianCalendar(i); int actualMin = Enumerable.Range(1900, 400) .Select(year => GetDaysInFirstWeek(year, calendar)) .Min(); Assert.AreEqual(i, actualMin); } }
public void MysteryTimeZones() { var julianCalendar = CalendarSystem.GetJulianCalendar(4); var julianEpoch = new LocalDate(Era.BeforeCommon, 4713, 1, 1, julianCalendar); var sampleDate = julianEpoch.PlusDays(2475213); Console.WriteLine("Sample date in ISO calendar: {0}", sampleDate.WithCalendar(CalendarSystem.Iso)); var zoneProvider = DateTimeZoneProviders.Tzdb; foreach (var id in zoneProvider.Ids) { var zone = zoneProvider[id]; if (zone.AtStartOfDay(sampleDate).LocalDateTime.TimeOfDay != LocalTime.Midnight) { Console.WriteLine(id); } } }
public void LocalDateTimeProperties() { LocalDateTime local = new LocalDateTime(2012, 6, 19, 1, 2, 3, 4, 5, CalendarSystem.GetJulianCalendar(5)); Offset offset = Offset.FromHours(5); OffsetDateTime odt = new OffsetDateTime(local, offset); var localDateTimePropertyNames = typeof(LocalDateTime).GetProperties() .Select(p => p.Name) .ToList(); var commonProperties = typeof(OffsetDateTime).GetProperties() .Where(p => localDateTimePropertyNames.Contains(p.Name)); foreach (var property in commonProperties) { Assert.AreEqual(typeof(LocalDateTime).GetProperty(property.Name).GetValue(local, null), property.GetValue(odt, null)); } }
[Test, Timeout(180000)] // Can take a long time under NCrunch. public void BclThroughHistory() { Calendar hijri = new HijriCalendar(); DateTime bclDirect = new DateTime(1, 1, 1, 0, 0, 0, 0, hijri, DateTimeKind.Unspecified); CalendarSystem islamicCalendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Astronomical); CalendarSystem julianCalendar = CalendarSystem.GetJulianCalendar(4); LocalDate julianIslamicEpoch = new LocalDate(622, 7, 15, julianCalendar); LocalDate islamicDate = julianIslamicEpoch.WithCalendar(islamicCalendar); for (int i = 0; i < 9000 * 365; i++) { Assert.AreEqual(bclDirect, islamicDate.AtMidnight().ToDateTimeUnspecified()); Assert.AreEqual(hijri.GetYear(bclDirect), islamicDate.Year, i.ToString()); Assert.AreEqual(hijri.GetMonth(bclDirect), islamicDate.Month); Assert.AreEqual(hijri.GetDayOfMonth(bclDirect), islamicDate.Day); bclDirect = hijri.AddDays(bclDirect, 1); islamicDate = islamicDate.PlusDays(1); } }
public void BinarySerialization() { TestHelper.AssertBinaryRoundtrip(new LocalDateTime(2013, 4, 12, 17, 53, 23, CalendarSystem.GetJulianCalendar(3))); TestHelper.AssertBinaryRoundtrip(new LocalDateTime(2013, 4, 12, 17, 53, 23, 123, 4567)); }
public void XmlSerialization_NonIso() { var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23, CalendarSystem.GetJulianCalendar(3)), Offset.FromHours(1)); TestHelper.AssertXmlRoundtrip(value, "<value calendar=\"Julian 3\">2013-04-12T17:53:23+01:00</value>"); }
public void GetInstance_InvalidMinDaysInFirstWeek() { Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetJulianCalendar(0)); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetJulianCalendar(8)); }
public void XmlSerialization_NonIso() { var value = new LocalDate(2013, 4, 12, CalendarSystem.GetJulianCalendar(3)); TestHelper.AssertXmlRoundtrip(value, "<value calendar=\"Julian 3\">2013-04-12</value>"); }
public void GetInstance_UniqueIds() { Assert.AreEqual(7, Enumerable.Range(1, 7).Select(x => CalendarSystem.GetJulianCalendar(x).Id).Distinct().Count()); }