Skip to content

Commit

Permalink
Dedup methods in GregorianCalendarHelper (#105188)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilinus authored Jul 20, 2024
1 parent 851779a commit b92fbf6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal static long GetAbsoluteDate(int year, int month, int day)
/// Returns the tick count corresponding to the given year, month, and day.
/// Will check the if the parameters are valid.
/// </summary>
private static long DateToTicks(int year, int month, int day)
internal static long DateToTicks(int year, int month, int day)
{
return GetAbsoluteDate(year, month, day) * TimeSpan.TicksPerDay;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,46 +166,6 @@ internal bool IsValidYear(int year, int era)
return GetYearOffset(year, era, throwOnError: false) >= 0;
}

/*=================================GetAbsoluteDate==========================
**Action: Gets the absolute date for the given Gregorian date. The absolute date means
** the number of days from January 1st, 1 A.D.
**Returns: the absolute date
**Arguments:
** year the Gregorian year
** month the Gregorian month
** day the day
**Exceptions:
** ArgumentOutOfRangException if year, month, day value is valid.
**Note:
** This is an internal method used by DateToTicks() and the calculations of Hijri and Hebrew calendars.
** Number of Days in Prior Years (both common and leap years) +
** Number of Days in Prior Months of Current Year +
** Number of Days in Current Month
**
============================================================================*/

internal static long GetAbsoluteDate(int year, int month, int day)
{
if (year >= 1 && year <= 9999 && month >= 1 && month <= 12)
{
ReadOnlySpan<int> days = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? GregorianCalendar.DaysToMonth366 : GregorianCalendar.DaysToMonth365;
if (day >= 1 && (day <= days[month] - days[month - 1]))
{
int y = year - 1;
int absoluteDate = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
return absoluteDate;
}
}
throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay);
}

// Returns the tick count corresponding to the given year, month, and day.
// Will check the if the parameters are valid.
internal static long DateToTicks(int year, int month, int day)
{
return GetAbsoluteDate(year, month, day) * TimeSpan.TicksPerDay;
}

internal void CheckTicksRange(long ticks)
{
if (ticks < m_Cal.MinSupportedDateTime.Ticks || ticks > m_Cal.MaxSupportedDateTime.Ticks)
Expand Down Expand Up @@ -269,7 +229,7 @@ public DateTime AddMonths(DateTime time, int months)
{
d = days;
}
long ticks = DateToTicks(y, m, d) + time.TimeOfDay.Ticks;
long ticks = GregorianCalendar.DateToTicks(y, m, d) + time.TimeOfDay.Ticks;
Calendar.CheckAddResult(ticks, m_Cal.MinSupportedDateTime, m_Cal.MaxSupportedDateTime);
return new DateTime(ticks);
}
Expand Down Expand Up @@ -501,7 +461,7 @@ public bool IsLeapYear(int year, int era)
public DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
{
year = GetGregorianYear(year, era);
long ticks = DateToTicks(year, month, day) + Calendar.TimeToTicks(hour, minute, second, millisecond);
long ticks = GregorianCalendar.DateToTicks(year, month, day) + Calendar.TimeToTicks(hour, minute, second, millisecond);
CheckTicksRange(ticks);
return new DateTime(ticks);
}
Expand Down

0 comments on commit b92fbf6

Please sign in to comment.