1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 03:57:40 +00:00

AK+LibC+LibCore: Have fewer implementations of days_in_month

This commit is contained in:
Nico Weber 2020-08-25 20:11:12 -04:00 committed by Andreas Kling
parent dcb81fc199
commit a7a18b478e
4 changed files with 20 additions and 16 deletions

View file

@ -42,4 +42,13 @@ int day_of_year(int year, unsigned month, int day)
return day_of_year; 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;
}
} }

View file

@ -35,6 +35,9 @@ namespace AK {
// can be negative. // can be negative.
int day_of_year(int year, unsigned month, int day); 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) inline bool is_leap_year(int year)
{ {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 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::day_of_year;
using AK::days_in_month;
using AK::is_leap_year; using AK::is_leap_year;
using AK::timespec_add; using AK::timespec_add;
using AK::timespec_add_timeval; using AK::timespec_add_timeval;

View file

@ -60,7 +60,6 @@ char* ctime(const time_t* t)
return asctime(localtime(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 const int __seconds_per_day = 60 * 60 * 24;
static void time_to_tm(struct tm* tm, time_t t) 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_hour = remaining / 60;
tm->tm_year = year - 1900; tm->tm_year = year - 1900;
tm->tm_yday = days; 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; int month;
for (month = 0; month < 11 && days >= __days_per_month[month]; ++month) for (month = 1; month < 12 && days >= days_in_month(year, month); ++month)
days -= __days_per_month[month]; days -= days_in_month(year, month);
tm->tm_mon = month;
tm->tm_mday += days; tm->tm_mon = month - 1;
tm->tm_mday = days + 1;
} }
static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds) static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)

View file

@ -71,12 +71,7 @@ unsigned DateTime::weekday() const
unsigned DateTime::days_in_month() 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); return ::days_in_month(m_year, m_month);
if (m_month == 2)
return is_leap_year() ? 29 : 28;
return is_long_month ? 31 : 30;
} }
unsigned DateTime::day_of_year() const unsigned DateTime::day_of_year() const