diff options
author | Nico Weber <thakis@chromium.org> | 2020-08-25 16:38:24 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-26 08:52:07 +0200 |
commit | 84ed2579594fdadef29ba145d8a7948ffe26200b (patch) | |
tree | 4a79cc738e8392253977fa1237e3f88aecbfe257 /Libraries | |
parent | 394e4c04cd5dfdca56f67e6384288a209bdbf6dc (diff) | |
download | serenity-84ed2579594fdadef29ba145d8a7948ffe26200b.zip |
AK+LibC+LibCore+Kernel: Have fewer implementations of is_leap_year
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibC/time.cpp | 24 | ||||
-rw-r--r-- | Libraries/LibCore/DateTime.cpp | 3 |
2 files changed, 12 insertions, 15 deletions
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 <AK/String.h> #include <AK/StringBuilder.h> +#include <AK/Time.h> #include <Kernel/API/Syscall.h> #include <assert.h> #include <errno.h> @@ -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 <AK/StringBuilder.h> +#include <AK/Time.h> #include <LibCore/DateTime.h> #include <sys/time.h> #include <time.h> @@ -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) |