diff --git a/AK/Time.h b/AK/Time.h index 9a87f3c389..f70b6f828d 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -43,13 +43,18 @@ inline bool is_leap_year(int year) return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } +inline unsigned days_in_year(int year) +{ + return 365 + is_leap_year(year); +} + inline int years_to_days_since_epoch(int year) { int days = 0; for (int current_year = 1970; current_year < year; ++current_year) - days += 365 + is_leap_year(current_year); + days += days_in_year(current_year); for (int current_year = year; current_year < 1970; ++current_year) - days -= 365 + is_leap_year(current_year); + days -= days_in_year(current_year); return days; } @@ -173,6 +178,7 @@ inline bool operator!=(const TimespecType& a, const TimespecType& b) using AK::day_of_year; using AK::days_in_month; +using AK::days_in_year; using AK::is_leap_year; using AK::timespec_add; using AK::timespec_add_timeval; diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index 7d35a4e3f4..01f7a164f0 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -70,10 +70,10 @@ static void time_to_tm(struct tm* tm, time_t t) tm->tm_wday /= __seconds_per_day; int year = 1970; - for (; t >= (365 + is_leap_year(year)) * __seconds_per_day; ++year) - t -= (365 + is_leap_year(year)) * __seconds_per_day; + for (; t >= days_in_year(year) * __seconds_per_day; ++year) + t -= days_in_year(year) * __seconds_per_day; for (; t < 0; --year) - t += (365 + is_leap_year(year - 1)) * __seconds_per_day; + t += days_in_year(year - 1) * __seconds_per_day; ASSERT(t >= 0); int days = t / __seconds_per_day; @@ -275,7 +275,7 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st if (tm->tm_yday >= 7 - wday_of_year_beginning) --week_number; else { - const int days_of_last_year = 365 + is_leap_year(tm->tm_year + 1900 - 1); + const int days_of_last_year = days_in_year(tm->tm_year + 1900 - 1); const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7; week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1; if (wday_of_last_year_beginning > 3) diff --git a/Libraries/LibCore/DateTime.cpp b/Libraries/LibCore/DateTime.cpp index e0dba3493e..d632e321da 100644 --- a/Libraries/LibCore/DateTime.cpp +++ b/Libraries/LibCore/DateTime.cpp @@ -216,8 +216,7 @@ String DateTime::to_string(const String& format) const if (tm.tm_yday >= 7 - wday_of_year_beginning) --week_number; else { - const bool last_year_is_leap = ::is_leap_year(tm.tm_year + 1900 - 1); - const int days_of_last_year = 365 + last_year_is_leap; + const int days_of_last_year = days_in_year(tm.tm_year + 1900 - 1); const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7; week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1; if (wday_of_last_year_beginning > 3)