public void Base15LeapYear() { CalendarSystem calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Assert.AreEqual(false, calendar.IsLeapYear(1)); Assert.AreEqual(true, calendar.IsLeapYear(2)); Assert.AreEqual(false, calendar.IsLeapYear(3)); Assert.AreEqual(false, calendar.IsLeapYear(4)); Assert.AreEqual(true, calendar.IsLeapYear(5)); Assert.AreEqual(false, calendar.IsLeapYear(6)); Assert.AreEqual(true, calendar.IsLeapYear(7)); Assert.AreEqual(false, calendar.IsLeapYear(8)); Assert.AreEqual(false, calendar.IsLeapYear(9)); Assert.AreEqual(true, calendar.IsLeapYear(10)); Assert.AreEqual(false, calendar.IsLeapYear(11)); Assert.AreEqual(false, calendar.IsLeapYear(12)); Assert.AreEqual(true, calendar.IsLeapYear(13)); Assert.AreEqual(false, calendar.IsLeapYear(14)); Assert.AreEqual(true, calendar.IsLeapYear(15)); Assert.AreEqual(false, calendar.IsLeapYear(16)); Assert.AreEqual(false, calendar.IsLeapYear(17)); Assert.AreEqual(true, calendar.IsLeapYear(18)); Assert.AreEqual(false, calendar.IsLeapYear(19)); Assert.AreEqual(false, calendar.IsLeapYear(20)); Assert.AreEqual(true, calendar.IsLeapYear(21)); Assert.AreEqual(false, calendar.IsLeapYear(22)); Assert.AreEqual(false, calendar.IsLeapYear(23)); Assert.AreEqual(true, calendar.IsLeapYear(24)); Assert.AreEqual(false, calendar.IsLeapYear(25)); Assert.AreEqual(true, calendar.IsLeapYear(26)); Assert.AreEqual(false, calendar.IsLeapYear(27)); Assert.AreEqual(false, calendar.IsLeapYear(28)); Assert.AreEqual(true, calendar.IsLeapYear(29)); Assert.AreEqual(false, calendar.IsLeapYear(30)); }
public void GetInstance_Caching() { var queue = new Queue <CalendarSystem>(); var set = new HashSet <CalendarSystem>(); var ids = new HashSet <string>(); foreach (IslamicLeapYearPattern leapYearPattern in Enum.GetValues(typeof(IslamicLeapYearPattern))) { foreach (IslamicEpoch epoch in Enum.GetValues(typeof(IslamicEpoch))) { var calendar = CalendarSystem.GetIslamicCalendar(leapYearPattern, epoch); queue.Enqueue(calendar); Assert.IsTrue(set.Add(calendar)); // Check we haven't already seen it... Assert.IsTrue(ids.Add(calendar.Id)); } } // Now check we get the same references again... foreach (IslamicLeapYearPattern leapYearPattern in Enum.GetValues(typeof(IslamicLeapYearPattern))) { foreach (IslamicEpoch epoch in Enum.GetValues(typeof(IslamicEpoch))) { var oldCalendar = queue.Dequeue(); var newCalendar = CalendarSystem.GetIslamicCalendar(leapYearPattern, epoch); Assert.AreSame(oldCalendar, newCalendar); } } }
public void GetInstance_ArgumentValidation() { Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(LeapYearPatterns.Min() - 1, Epochs.Min())); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(LeapYearPatterns.Min(), Epochs.Min() - 1)); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(LeapYearPatterns.Max() + 1, Epochs.Min())); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(LeapYearPatterns.Min(), Epochs.Max() + 1)); }
/// <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 PlusYears_Simple() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); LocalDateTime start = new LocalDateTime(5, 8, 20, 2, 0, calendar); LocalDateTime expectedEnd = new LocalDateTime(10, 8, 20, 2, 0, calendar); Assert.AreEqual(expectedEnd, start.PlusYears(5)); }
public void CompareTo_DifferentCalendars_Throws() { CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical); YearMonth yearMonth1 = new YearMonth(2011, 1); YearMonth yearMonth2 = new YearMonth(1500, 1, islamic); Assert.Throws <ArgumentException>(() => yearMonth1.CompareTo(yearMonth2)); Assert.Throws <ArgumentException>(() => ((IComparable)yearMonth1).CompareTo(yearMonth2)); }
public void CompareTo_DifferentCalendars_Throws() { CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical); LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30); LocalDateTime value2 = new LocalDateTime(1500, 1, 1, 10, 30, islamic); Assert.Throws <ArgumentException>(() => value1.CompareTo(value2)); Assert.Throws <ArgumentException>(() => ((IComparable)value1).CompareTo(value2)); }
public void GetInstance_ArgumentValidation() { var epochs = Enum.GetValues(typeof(IslamicEpoch)).Cast <IslamicEpoch>(); var leapYearPatterns = Enum.GetValues(typeof(IslamicLeapYearPattern)).Cast <IslamicLeapYearPattern>(); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(leapYearPatterns.Min() - 1, epochs.Min())); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(leapYearPatterns.Min(), epochs.Min() - 1)); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(leapYearPatterns.Max() + 1, epochs.Min())); Assert.Throws <ArgumentOutOfRangeException>(() => CalendarSystem.GetIslamicCalendar(leapYearPatterns.Min(), epochs.Max() + 1)); }
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 SampleDateBclCompatibility() { Calendar hijri = new HijriCalendar(); DateTime bclDirect = new DateTime(1302, 10, 15, 0, 0, 0, 0, hijri, DateTimeKind.Unspecified); CalendarSystem islamicCalendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base16, IslamicEpoch.Astronomical); LocalDate iso = new LocalDate(1302, 10, 15, islamicCalendar); DateTime bclFromNoda = iso.AtMidnight().ToDateTimeUnspecified(); Assert.AreEqual(bclDirect, bclFromNoda); }
public void CompareTo_DifferentCalendars_OnlyInstantMatters() { CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical); ZonedDateTime value1 = SampleZone.AtStrictly(new LocalDateTime(2011, 1, 2, 10, 30)); ZonedDateTime value2 = SampleZone.AtStrictly(new LocalDateTime(1500, 1, 1, 10, 30, islamic)); ZonedDateTime value3 = SampleZone.AtStrictly(value1.LocalDateTime.WithCalendar(islamic)); Assert.That(value1.CompareTo(value2), Is.LessThan(0)); Assert.That(value2.CompareTo(value1), Is.GreaterThan(0)); Assert.That(value1.CompareTo(value3), Is.EqualTo(0)); }
public void CompareTo_DifferentCalendars_OnlyLocalInstantMatters() { CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical); LocalDate date1 = new LocalDate(2011, 1, 2); LocalDate date2 = new LocalDate(1500, 1, 1, islamic); LocalDate date3 = date1.WithCalendar(islamic); Assert.That(date1.CompareTo(date2), Is.LessThan(0)); Assert.That(date2.CompareTo(date1), Is.GreaterThan(0)); Assert.That(date1.CompareTo(date3), Is.EqualTo(0)); }
public void WithOffset_NonIsoCalendar() { // October 12th 2013 ISO is 1434-12-07 Islamic CalendarSystem calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Instant instant = Instant.FromUtc(2013, 10, 12, 11, 15); Offset offset = Offset.FromHours(2); OffsetDateTime actual = instant.WithOffset(offset, calendar); OffsetDateTime expected = new OffsetDateTime(new LocalDateTime(1434, 12, 7, 13, 15, calendar), offset); Assert.AreEqual(expected, actual); }
public void PlusYears_DoesNotTruncateFromOneLeapYearToAnother() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Assert.IsTrue(calendar.IsLeapYear(2)); Assert.IsTrue(calendar.IsLeapYear(5)); LocalDateTime start = new LocalDateTime(2, 12, 30, 2, 0, calendar); LocalDateTime expectedEnd = new LocalDateTime(5, 12, 30, 2, 0, calendar); Assert.AreEqual(expectedEnd, start.PlusYears(3)); }
public void PlusYears_TruncatesAtLeapYear() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Assert.IsTrue(calendar.IsLeapYear(2)); Assert.IsFalse(calendar.IsLeapYear(3)); LocalDateTime start = new LocalDateTime(2, 12, 30, 2, 0, calendar); LocalDateTime expectedEnd = new LocalDateTime(3, 12, 29, 2, 0, calendar); Assert.AreEqual(expectedEnd, start.PlusYears(1)); }
public void PlusMonths_Simple() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Assert.IsTrue(calendar.IsLeapYear(2)); LocalDateTime start = new LocalDateTime(2, 12, 30, 2, 0, calendar); LocalDateTime expectedEnd = new LocalDateTime(3, 11, 30, 2, 0, calendar); Assert.AreEqual(11, expectedEnd.Month); Assert.AreEqual(30, expectedEnd.Day); Assert.AreEqual(expectedEnd, start.PlusMonths(11)); }
public void IComparableCompareTo_DifferentCalendars_OnlyLocalInstantMatters() { CalendarSystem islamic = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Astronomical); LocalDateTime value1 = new LocalDateTime(2011, 1, 2, 10, 30); LocalDateTime value2 = new LocalDateTime(1500, 1, 1, 10, 30, islamic); LocalDateTime value3 = value1.WithCalendar(islamic); IComparable i_value1 = (IComparable)value1; IComparable i_value2 = (IComparable)value2; Assert.That(i_value1.CompareTo(value2), Is.LessThan(0)); Assert.That(i_value2.CompareTo(value1), Is.GreaterThan(0)); Assert.That(i_value1.CompareTo(value3), Is.EqualTo(0)); }
public void InternalConsistency() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); // Check construction and then deconstruction for every day of every year in one 30-year cycle. for (int year = 1; year <= 30; year++) { for (int month = 1; month <= 12; month++) { int monthLength = calendar.GetDaysInMonth(year, month); for (int day = 1; day < monthLength; day++) { LocalDate date = new LocalDate(year, month, day, calendar); Assert.AreEqual(year, date.Year, "Year of {0}-{1}-{2}", year, month, day); Assert.AreEqual(month, date.Month, "Month of {0}-{1}-{2}", year, month, day); Assert.AreEqual(day, date.Day, "Day of {0}-{1}-{2}", year, month, day); } } } }
[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 GetDaysInMonth() { // Just check that we've got the long/short the right way round... CalendarSystem calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.HabashAlHasib, IslamicEpoch.Civil); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 1)); Assert.AreEqual(29, calendar.GetDaysInMonth(7, 2)); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 3)); Assert.AreEqual(29, calendar.GetDaysInMonth(7, 4)); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 5)); Assert.AreEqual(29, calendar.GetDaysInMonth(7, 6)); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 7)); Assert.AreEqual(29, calendar.GetDaysInMonth(7, 8)); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 9)); Assert.AreEqual(29, calendar.GetDaysInMonth(7, 10)); Assert.AreEqual(30, calendar.GetDaysInMonth(7, 11)); // As noted before, 7 isn't a leap year in this calendar Assert.AreEqual(29, calendar.GetDaysInMonth(7, 12)); // As noted before, 8 is a leap year in this calendar Assert.AreEqual(30, calendar.GetDaysInMonth(8, 12)); }
public void BetweenLocalDateTimes_OnLeapYearIslamic() { var calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil); Assert.IsTrue(calendar.IsLeapYear(2)); Assert.IsFalse(calendar.IsLeapYear(3)); LocalDateTime dt1 = new LocalDateTime(2, 12, 30, 2, 0, calendar); LocalDateTime dt2 = new LocalDateTime(2, 12, 30, 4, 0, calendar); LocalDateTime dt3 = new LocalDateTime(3, 12, 29, 3, 0, calendar); // Adding a year truncates to 0003-12-28T02:00:00, then add an hour. Assert.AreEqual(Parse("P1YT1H"), Period.Between(dt1, dt3)); // Adding a year would overshoot. Adding 11 months takes us to month 03-11-30T04:00. // Adding another 28 days takes us to 03-12-28T04:00, then add another 23 hours to finish. Assert.AreEqual(Parse("P11M28DT23H"), Period.Between(dt2, dt3)); // Subtracting 11 months takes us to 03-01-29T03:00. Subtracting another 29 days // takes us to 02-12-30T03:00, and another hour to get to the target. Assert.AreEqual(Parse("P-11M-29DT-1H"), Period.Between(dt3, dt1)); Assert.AreEqual(Parse("P-11M-28DT-23H"), Period.Between(dt3, dt2)); }