From a7a18b478e9ebd1bf668e926423305f211fc8c50 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 25 Aug 2020 20:11:12 -0400 Subject: [PATCH] AK+LibC+LibCore: Have fewer implementations of days_in_month --- AK/Time.cpp | 9 +++++++++ AK/Time.h | 4 ++++ Libraries/LibC/time.cpp | 16 ++++++---------- Libraries/LibCore/DateTime.cpp | 7 +------ 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/AK/Time.cpp b/AK/Time.cpp index 63253f90b7c..ea9e470dbae 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -42,4 +42,13 @@ int day_of_year(int year, unsigned month, int day) return day_of_year; } +int days_in_month(int year, unsigned month) +{ + ASSERT(month >= 1 && month <= 12); + if (month == 2) + return is_leap_year(year) ? 29 : 28; + + bool is_long_month = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); + return is_long_month ? 31 : 30; +} } diff --git a/AK/Time.h b/AK/Time.h index 612b92f691f..9a87f3c389e 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -35,6 +35,9 @@ namespace AK { // can be negative. int day_of_year(int year, unsigned month, int day); +// Month starts at 1. Month must be >= 1 and <= 12. +int days_in_month(int year, unsigned month); + inline bool is_leap_year(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); @@ -169,6 +172,7 @@ inline bool operator!=(const TimespecType& a, const TimespecType& b) } using AK::day_of_year; +using AK::days_in_month; 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 032da5b6f00..7d35a4e3f4e 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -60,7 +60,6 @@ char* ctime(const time_t* t) return asctime(localtime(t)); } -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; static void time_to_tm(struct tm* tm, time_t t) @@ -85,16 +84,13 @@ static void time_to_tm(struct tm* tm, time_t t) tm->tm_hour = remaining / 60; tm->tm_year = year - 1900; tm->tm_yday = days; - tm->tm_mday = 1; - if (is_leap_year(year) && days == 59) - ++tm->tm_mday; - if (is_leap_year(year) && days >= 59) - --days; + int month; - for (month = 0; month < 11 && days >= __days_per_month[month]; ++month) - days -= __days_per_month[month]; - tm->tm_mon = month; - tm->tm_mday += days; + for (month = 1; month < 12 && days >= days_in_month(year, month); ++month) + days -= days_in_month(year, month); + + tm->tm_mon = month - 1; + tm->tm_mday = days + 1; } static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds) diff --git a/Libraries/LibCore/DateTime.cpp b/Libraries/LibCore/DateTime.cpp index 3cd4ccc870a..e0dba3493e3 100644 --- a/Libraries/LibCore/DateTime.cpp +++ b/Libraries/LibCore/DateTime.cpp @@ -71,12 +71,7 @@ unsigned DateTime::weekday() const unsigned DateTime::days_in_month() const { - bool is_long_month = (m_month == 1 || m_month == 3 || m_month == 5 || m_month == 7 || m_month == 8 || m_month == 10 || m_month == 12); - - if (m_month == 2) - return is_leap_year() ? 29 : 28; - - return is_long_month ? 31 : 30; + return ::days_in_month(m_year, m_month); } unsigned DateTime::day_of_year() const