mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
AK+LibC+Kernel: Have fewer implementations of year_to_days_in_epoch
I believe the implementation in RTC.cpp had an off-by-one in the year passed to is_leap_year(). If that's true, then this fixes that too.
This commit is contained in:
parent
84ed257959
commit
9b17082899
3 changed files with 13 additions and 18 deletions
11
AK/Time.h
11
AK/Time.h
|
@ -33,6 +33,16 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int years_to_days_since_epoch(int year)
|
||||||
|
{
|
||||||
|
int days = 0;
|
||||||
|
for (int current_year = 1970; current_year < year; ++current_year)
|
||||||
|
days += 365 + is_leap_year(current_year);
|
||||||
|
for (int current_year = year; current_year < 1970; ++current_year)
|
||||||
|
days -= 365 + is_leap_year(current_year);
|
||||||
|
return days;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename TimevalType>
|
template<typename TimevalType>
|
||||||
inline void timeval_sub(const TimevalType& a, const TimevalType& b, TimevalType& result)
|
inline void timeval_sub(const TimevalType& a, const TimevalType& b, TimevalType& result)
|
||||||
{
|
{
|
||||||
|
@ -160,6 +170,7 @@ using AK::timespec_to_timeval;
|
||||||
using AK::timeval_add;
|
using AK::timeval_add;
|
||||||
using AK::timeval_sub;
|
using AK::timeval_sub;
|
||||||
using AK::timeval_to_timespec;
|
using AK::timeval_to_timespec;
|
||||||
|
using AK::years_to_days_since_epoch;
|
||||||
using AK::operator<=;
|
using AK::operator<=;
|
||||||
using AK::operator<;
|
using AK::operator<;
|
||||||
using AK::operator>;
|
using AK::operator>;
|
||||||
|
|
|
@ -93,18 +93,6 @@ static unsigned days_in_months_since_start_of_year(unsigned month, unsigned year
|
||||||
return days;
|
return days;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned days_in_years_since_epoch(unsigned year)
|
|
||||||
{
|
|
||||||
unsigned days = 0;
|
|
||||||
while (year > 1969) {
|
|
||||||
days += 365;
|
|
||||||
if (is_leap_year(year))
|
|
||||||
++days;
|
|
||||||
--year;
|
|
||||||
}
|
|
||||||
return days;
|
|
||||||
}
|
|
||||||
|
|
||||||
static u8 bcd_to_binary(u8 bcd)
|
static u8 bcd_to_binary(u8 bcd)
|
||||||
{
|
{
|
||||||
return (bcd & 0x0F) + ((bcd >> 4) * 10);
|
return (bcd & 0x0F) + ((bcd >> 4) * 10);
|
||||||
|
@ -160,7 +148,7 @@ time_t now()
|
||||||
|
|
||||||
ASSERT(year >= 2018);
|
ASSERT(year >= 2018);
|
||||||
|
|
||||||
return days_in_years_since_epoch(year - 1) * 86400
|
return years_to_days_since_epoch(year) * 86400
|
||||||
+ days_in_months_since_start_of_year(month - 1, year) * 86400
|
+ days_in_months_since_start_of_year(month - 1, year) * 86400
|
||||||
+ (day - 1) * 86400
|
+ (day - 1) * 86400
|
||||||
+ hour * 3600
|
+ hour * 3600
|
||||||
|
|
|
@ -116,11 +116,7 @@ static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)
|
||||||
tm->tm_mon += 12;
|
tm->tm_mon += 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
int days = 0;
|
int days = years_to_days_since_epoch(1900 + tm->tm_year);
|
||||||
for (int year = 70; year < tm->tm_year; ++year)
|
|
||||||
days += 365 + is_leap_year(1900 + year);
|
|
||||||
for (int year = tm->tm_year; year < 70; ++year)
|
|
||||||
days -= 365 + is_leap_year(1900 + year);
|
|
||||||
|
|
||||||
tm->tm_yday = tm->tm_mday - 1;
|
tm->tm_yday = tm->tm_mday - 1;
|
||||||
for (int month = 0; month < tm->tm_mon; ++month)
|
for (int month = 0; month < tm->tm_mon; ++month)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue