mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:37:36 +00:00
AK+LibC+LibCore: Have fewer implementations of day_of_week
The implementation in LibC did a timestamp->day-of-week conversion which looks like a valuable thing to have. But we only need it in time_to_tm, where we already computed year/month/day -- so let's consolidate on the day_of_week function in DateTime (which is getting extracted to AK).
This commit is contained in:
parent
b9cbb4fd00
commit
1ab8939077
4 changed files with 20 additions and 12 deletions
|
@ -64,11 +64,6 @@ static const int __seconds_per_day = 60 * 60 * 24;
|
|||
|
||||
static void time_to_tm(struct tm* tm, time_t t)
|
||||
{
|
||||
tm->tm_wday = (4 * __seconds_per_day + t) % (7 * __seconds_per_day); // 1970-01-01 was a Thursday.
|
||||
if (tm->tm_wday < 0)
|
||||
tm->tm_wday += 7 * __seconds_per_day;
|
||||
tm->tm_wday /= __seconds_per_day;
|
||||
|
||||
int year = 1970;
|
||||
for (; t >= days_in_year(year) * __seconds_per_day; ++year)
|
||||
t -= days_in_year(year) * __seconds_per_day;
|
||||
|
@ -89,8 +84,9 @@ static void time_to_tm(struct tm* tm, time_t t)
|
|||
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;
|
||||
tm->tm_wday = day_of_week(year, month, tm->tm_mday);
|
||||
tm->tm_mon = month - 1;
|
||||
}
|
||||
|
||||
static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)
|
||||
|
|
|
@ -61,12 +61,7 @@ DateTime DateTime::from_timestamp(time_t timestamp)
|
|||
|
||||
unsigned DateTime::weekday() const
|
||||
{
|
||||
int target_year = m_year;
|
||||
static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
|
||||
if (m_month < 3)
|
||||
--target_year;
|
||||
|
||||
return (target_year + target_year / 4 - target_year / 100 + target_year / 400 + seek_table[m_month - 1] + m_day) % 7;
|
||||
return ::day_of_week(m_year, m_month, m_day);
|
||||
}
|
||||
|
||||
unsigned DateTime::days_in_month() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue