public virtual DateTime GetToDate(DateTime dt, System.Globalization.Calendar calendar) { if (this.Order == 1 && this.FromMonth > this.ToMonth) { if (calendar.GetMonth(dt) == 12) { //در اولین بازه محدوده، ماه شروع در سال قبل قرار گرفته dt = calendar.AddYears(dt, 1); } } if (this.Order == 12 && this.FromMonth > this.ToMonth) { if (calendar.GetMonth(dt) == 12) { //در آخرین بازه محدوده، ماه پایان در سال بعد قرار گرفته dt = calendar.AddYears(dt, 1); } } else if (this.Order == 0 && this.FromMonth >= this.ToMonth) { //بازه سالانه است و پایان در سال بعد قرار گرفته dt = calendar.AddYears(dt, 1); } if (calendar is PersianCalendar) { if (calendar.IsLeapYear(calendar.GetYear(dt)) && this.ToMonth == 12 && this.ToDay == 29) { return(calendar.ToDateTime(calendar.GetYear(dt), this.ToMonth, 30, 0, 0, 0, 0)); } else { return(calendar.ToDateTime(calendar.GetYear(dt), this.ToMonth, this.ToDay, 0, 0, 0, 0)); } } else { if (calendar.IsLeapYear(dt.Year) && this.ToMonth == 2 && this.ToDay == 28) { //اگر سال کبسه بود و برای ماه فوریه روز 28 انتخاب شده بود //به صورت خودکار با روز 29 جایگزین می شود return(calendar.ToDateTime(dt.Year, this.ToMonth, 29, 0, 0, 0, 0)); } else { return(calendar.ToDateTime(dt.Year, this.ToMonth, this.ToDay, 0, 0, 0, 0)); } } }
/// <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> /// 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) { // Yes, this is horrible... but the specific calendars aren't available to test // against in the PCL switch (bcl.GetType().Name) { case "GregorianCalendar": return CalendarSystem.Iso; case "HijriCalendar": return CalendarSystem.IslamicBcl; case "HebrewCalendar": return CalendarSystem.HebrewCivil; case "PersianCalendar": return bcl.IsLeapYear(1) ? CalendarSystem.PersianSimple : CalendarSystem.PersianAstronomical; case "UmAlQuraCalendar": return CalendarSystem.UmAlQura; case "JulianCalendar": return CalendarSystem.Julian; default: // No idea - we can't test with this calendar... return null; } }