From 4b13633346f87af9814f152365bdf390969545ae Mon Sep 17 00:00:00 2001 From: lilinus Date: Sat, 20 Jul 2024 21:02:04 +0200 Subject: [PATCH] Dedup methods in GregorianCalendarHelper --- .../System/Globalization/GregorianCalendar.cs | 2 +- .../Globalization/GregorianCalendarHelper.cs | 44 +------------------ 2 files changed, 3 insertions(+), 43 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendar.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendar.cs index 04baa4944bc73..29d8e6a014bf8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendar.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendar.cs @@ -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. /// - 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; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendarHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendarHelper.cs index 04298c12e7f6f..032e6963e167d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendarHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/GregorianCalendarHelper.cs @@ -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 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) @@ -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); } @@ -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); }