/// <summary> /// Checks that each day from the given start year to the end year (inclusive) is equal /// between the BCL and the Noda Time calendar. Additionally, the number of days in each month and year /// and the number of months (and leap year status) in each year is checked. /// </summary> internal static void AssertEquivalent(Calendar bcl, CalendarSystem noda, int fromYear, int toYear) { // We avoid asking the BCL to create a DateTime on each iteration, simply // because the BCL implementation is so slow. Instead, we just check at the start of each month that // we're at the date we expect. DateTime bclDate = bcl.ToDateTime(fromYear, 1, 1, 0, 0, 0, 0); for (int year = fromYear; year <= toYear; year++) { Assert.AreEqual(bcl.GetDaysInYear(year), noda.GetDaysInYear(year), "Year: {0}", year); Assert.AreEqual(bcl.GetMonthsInYear(year), noda.GetMonthsInYear(year), "Year: {0}", year); for (int month = 1; month <= noda.GetMonthsInYear(year); month++) { // Sanity check at the start of each month. Even this is surprisingly slow. // (These three tests make up about 20% of the total execution time for the test.) Assert.AreEqual(year, bcl.GetYear(bclDate)); Assert.AreEqual(month, bcl.GetMonth(bclDate)); Assert.AreEqual(1, bcl.GetDayOfMonth(bclDate)); Assert.AreEqual(bcl.GetDaysInMonth(year, month), noda.GetDaysInMonth(year, month), "Year: {0}; Month: {1}", year, month); Assert.AreEqual(bcl.IsLeapYear(year), noda.IsLeapYear(year), "Year: {0}", year); for (int day = 1; day <= noda.GetDaysInMonth(year, month); day++) { LocalDate nodaDate = new LocalDate(year, month, day, noda); Assert.AreEqual(bclDate, nodaDate.ToDateTimeUnspecified(), "Original calendar system date: {0:yyyy-MM-dd}", nodaDate); Assert.AreEqual(nodaDate, LocalDate.FromDateTime(bclDate, noda)); Assert.AreEqual(year, nodaDate.Year); Assert.AreEqual(month, nodaDate.Month); Assert.AreEqual(day, nodaDate.Day); bclDate = bclDate.AddDays(1); } } } }
private ParseResult <LocalDate> DetermineMonth(PatternFields usedFields, string text) { switch (usedFields & (PatternFields.MonthOfYearNumeric | PatternFields.MonthOfYearText)) { case PatternFields.MonthOfYearNumeric: // No-op break; case PatternFields.MonthOfYearText: MonthOfYearNumeric = MonthOfYearText; break; case PatternFields.MonthOfYearNumeric | PatternFields.MonthOfYearText: if (MonthOfYearNumeric != MonthOfYearText) { return(ParseResult <LocalDate> .InconsistentMonthValues(text)); } // No need to change MonthOfYearNumeric - this was just a check break; case 0: MonthOfYearNumeric = TemplateValue.Month; break; } if (MonthOfYearNumeric > Calendar.GetMonthsInYear(Year)) { return(ParseResult <LocalDate> .MonthOutOfRange(text, MonthOfYearNumeric, Year)); } return(null); }
/// <summary> /// Checks that each day from the given start year to the end year (inclusive) is equal /// between the BCL and the Noda Time calendar. Additionally, the number of days in each month and year /// and the number of months (and leap year status) in each year is checked. /// </summary> internal static void AssertEquivalent(Calendar bcl, CalendarSystem noda, int fromYear, int toYear) { // We avoid asking the BCL to create a DateTime on each iteration, simply // because the BCL implementation is so slow. Instead, we just check at the start of each month that // we're at the date we expect. DateTime bclDate = bcl.ToDateTime(fromYear, 1, 1, 0, 0, 0, 0); for (int year = fromYear; year <= toYear; year++) { Assert.AreEqual(bcl.GetDaysInYear(year), noda.GetDaysInYear(year), "Year: {0}", year); Assert.AreEqual(bcl.GetMonthsInYear(year), noda.GetMonthsInYear(year), "Year: {0}", year); for (int month = 1; month <= noda.GetMonthsInYear(year); month++) { // Sanity check at the start of each month. Even this is surprisingly slow. // (These three tests make up about 20% of the total execution time for the test.) Assert.AreEqual(year, bcl.GetYear(bclDate)); Assert.AreEqual(month, bcl.GetMonth(bclDate)); Assert.AreEqual(1, bcl.GetDayOfMonth(bclDate)); Assert.AreEqual(bcl.GetDaysInMonth(year, month), noda.GetDaysInMonth(year, month), "Year: {0}; Month: {1}", year, month); Assert.AreEqual(bcl.IsLeapYear(year), noda.IsLeapYear(year), "Year: {0}", year); for (int day = 1; day <= noda.GetDaysInMonth(year, month); day++) { LocalDate nodaDate = new LocalDate(year, month, day, noda); Assert.AreEqual(bclDate, nodaDate.ToDateTimeUnspecified(), "Original calendar system date: {0:yyyy-MM-dd}", nodaDate); Assert.AreEqual(nodaDate, LocalDate.FromDateTime(bclDate, noda)); Assert.AreEqual(year, nodaDate.Year); Assert.AreEqual(month, nodaDate.Month); Assert.AreEqual(day, nodaDate.Day); bclDate = bclDate.AddDays(1); } } } }
/// <summary> /// Converts each day in a full leap cycle (for coverage of different scenarios) to the ISO /// calendar and back. This exercises fetching the number of days since the epoch and getting /// a year/month/day *from* a number of days. /// </summary> private static void TestLeapCycle(CalendarSystem calendar) { for (int year = 5400; year < 5419; year++) { #if !V1 int maxMonth = calendar.GetMonthsInYear(year); #else int maxMonth = calendar.GetMaxMonth(year); #endif for (int month = 1; month <= maxMonth; month++) { int maxDay = calendar.GetDaysInMonth(year, month); for (int day = 1; day <= maxDay; day++) { var date = new LocalDate(year, month, day, calendar); date.WithCalendar(CalendarSystem.Iso).WithCalendar(calendar).Consume(); } } } }
/// <summary> /// Converts each day in a full leap cycle (for coverage of different scenarios) to the ISO /// calendar and back. This exercises fetching the number of days since the epoch and getting /// a year/month/day *from* a number of days. /// </summary> private static void TestLeapCycle(CalendarSystem calendar) { for (int year = 5400; year < 5419; year++) { #if !V1 int maxMonth = calendar.GetMonthsInYear(year); #else int maxMonth = calendar.GetMaxMonth(year); #endif for (int month = 1; month <= maxMonth; month++) { int maxDay = calendar.GetDaysInMonth(year, month); for (int day = 1; day <= maxDay; day++) { var date = new LocalDate(year, month, day, calendar); date.WithCalendar(CalendarSystem.Iso).WithCalendar(calendar).Consume(); } } } }