/// <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> /// Initializes a new instance of <see cref="Age"/>. /// </summary> /// <param name="start">The date and time when the age started.</param> /// <param name="end">The date and time when the age ended.</param> /// <param name="calendar">Calendar used to calculate age.</param> public Age(DateTime start, DateTime end, Calendar calendar) { if (start > end) throw new ArgumentException("The starting date cannot be later than the end date."); var startDate = start.Date; var endDate = end.Date; _years = _months = _days = 0; _days += calendar.GetDayOfMonth(endDate) - calendar.GetDayOfMonth(startDate); if (_days < 0) { _days += calendar.GetDaysInMonth(calendar.GetYear(startDate), calendar.GetMonth(startDate)); _months--; } _months += calendar.GetMonth(endDate) - calendar.GetMonth(startDate); if (_months < 0) { _months += calendar.GetMonthsInYear(calendar.GetYear(startDate)); _years--; } _years += calendar.GetYear(endDate) - calendar.GetYear(startDate); var ts = endDate.Subtract(startDate); _totalDays = (Int32)ts.TotalDays; }
private static bool IsValidMonth( Calendar calendar, int year, int month, int era ) { Contract.Requires( calendar != null ); if ( month < 1 ) return false; if ( year == calendar.MinSupportedDateTime.Year && month < calendar.MinSupportedDateTime.Month ) return false; return month <= calendar.GetMonthsInYear( year, era ); }
/// <summary> /// Determines if the specified <paramref name="month"/> is a valid month value. /// </summary> /// <param name="month">The month value.</param> /// <param name="year">The year value.</param> /// <param name="cal">The calendar to use.</param> /// <returns>true if it's a valid month value; false otherwise.</returns> private static bool IsValidMonth(int month, int year, Calendar cal) { year = cal.ToFourDigitYear(year); return month >= 1 && month <= cal.GetMonthsInYear(year); }
public void TestDaysInYear (Calendar calendar, int year) { var daysInYear = calendar.GetDaysInYear (year); var daysInMonths = 0; var monthInYear = calendar.GetMonthsInYear (year); for (var m = 1; m <= monthInYear; m++) daysInMonths += calendar.GetDaysInMonth (year, m); Assert.AreEqual (daysInYear, daysInMonths, string.Format("Calendar:{0} Year:{1}",calendar.GetType(), year)); }
public static int Month( this DateTime date, Calendar calendar ) { Arg.NotNull( calendar, nameof( calendar ) ); Contract.Ensures( Contract.Result<int>() > 0 ); Contract.Ensures( Contract.Result<int>() <= calendar.GetMonthsInYear( date.Year ) ); return calendar.GetMonth( date ); }