From 84ed2579594fdadef29ba145d8a7948ffe26200b Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 25 Aug 2020 16:38:24 -0400 Subject: [PATCH] AK+LibC+LibCore+Kernel: Have fewer implementations of is_leap_year --- AK/Time.h | 6 ++++++ Kernel/RTC.cpp | 6 +----- Libraries/LibC/time.cpp | 24 ++++++++++-------------- Libraries/LibCore/DateTime.cpp | 3 ++- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/AK/Time.h b/AK/Time.h index dd3e48c770..a5209908a9 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -28,6 +28,11 @@ namespace AK { +inline bool is_leap_year(int year) +{ + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); +} + template inline void timeval_sub(const TimevalType& a, const TimevalType& b, TimevalType& result) { @@ -146,6 +151,7 @@ inline bool operator!=(const TimespecType& a, const TimespecType& b) } +using AK::is_leap_year; using AK::timespec_add; using AK::timespec_add_timeval; using AK::timespec_sub; diff --git a/Kernel/RTC.cpp b/Kernel/RTC.cpp index 46e2528939..d2944f7100 100644 --- a/Kernel/RTC.cpp +++ b/Kernel/RTC.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -48,11 +49,6 @@ static bool update_in_progress() return CMOS::read(0x0a) & 0x80; } -inline bool is_leap_year(unsigned year) -{ - return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0)); -} - static unsigned days_in_months_since_start_of_year(unsigned month, unsigned year) { ASSERT(month <= 11); diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index 68810ae509..fce068699a 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -59,11 +60,6 @@ char* ctime(const time_t* t) return asctime(localtime(t)); } -static inline bool __is_leap_year(int year) -{ - return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0)); -} - static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static const int __seconds_per_day = 60 * 60 * 24; @@ -75,10 +71,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 >= (365 + is_leap_year(year)) * __seconds_per_day; ++year) + t -= (365 + is_leap_year(year)) * __seconds_per_day; for (; t < 0; --year) - t += (365 + __is_leap_year(year - 1)) * __seconds_per_day; + t += (365 + is_leap_year(year - 1)) * __seconds_per_day; ASSERT(t >= 0); int days = t / __seconds_per_day; @@ -90,9 +86,9 @@ static void time_to_tm(struct tm* tm, time_t t) tm->tm_year = year - 1900; tm->tm_yday = days; tm->tm_mday = 1; - if (__is_leap_year(year) && days == 59) + if (is_leap_year(year) && days == 59) ++tm->tm_mday; - if (__is_leap_year(year) && days >= 59) + if (is_leap_year(year) && days >= 59) --days; int month; for (month = 0; month < 11 && days >= __days_per_month[month]; ++month) @@ -122,14 +118,14 @@ static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds) int days = 0; for (int year = 70; year < tm->tm_year; ++year) - days += 365 + __is_leap_year(1900 + year); + days += 365 + is_leap_year(1900 + year); for (int year = tm->tm_year; year < 70; ++year) - days -= 365 + __is_leap_year(1900 + year); + days -= 365 + is_leap_year(1900 + year); tm->tm_yday = tm->tm_mday - 1; for (int month = 0; month < tm->tm_mon; ++month) tm->tm_yday += __days_per_month[month]; - if (tm->tm_mon > 1 && __is_leap_year(1900 + tm->tm_year)) + if (tm->tm_mon > 1 && is_leap_year(1900 + tm->tm_year)) ++tm->tm_yday; days += tm->tm_yday; @@ -293,7 +289,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 = 365 + is_leap_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 38fa3eafbd..a1cd35cc7b 100644 --- a/Libraries/LibCore/DateTime.cpp +++ b/Libraries/LibCore/DateTime.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -91,7 +92,7 @@ unsigned DateTime::day_of_year() const bool DateTime::is_leap_year() const { - return ((m_year % 400 == 0) || (m_year % 4 == 0 && m_year % 100 != 0)); + return ::is_leap_year(m_year); } void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)