mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 04:13:11 +00:00
AK+LibC+LibCore: Add a days_in_year function
This commit is contained in:
parent
a7a18b478e
commit
2236385e1f
10
AK/Time.h
10
AK/Time.h
|
@ -43,13 +43,18 @@ inline bool is_leap_year(int year)
|
|||
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
|
||||
}
|
||||
|
||||
inline unsigned days_in_year(int year)
|
||||
{
|
||||
return 365 + is_leap_year(year);
|
||||
}
|
||||
|
||||
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);
|
||||
days += days_in_year(current_year);
|
||||
for (int current_year = year; current_year < 1970; ++current_year)
|
||||
days -= 365 + is_leap_year(current_year);
|
||||
days -= days_in_year(current_year);
|
||||
return days;
|
||||
}
|
||||
|
||||
|
@ -173,6 +178,7 @@ inline bool operator!=(const TimespecType& a, const TimespecType& b)
|
|||
|
||||
using AK::day_of_year;
|
||||
using AK::days_in_month;
|
||||
using AK::days_in_year;
|
||||
using AK::is_leap_year;
|
||||
using AK::timespec_add;
|
||||
using AK::timespec_add_timeval;
|
||||
|
|
|
@ -70,10 +70,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 >= days_in_year(year) * __seconds_per_day; ++year)
|
||||
t -= days_in_year(year) * __seconds_per_day;
|
||||
for (; t < 0; --year)
|
||||
t += (365 + is_leap_year(year - 1)) * __seconds_per_day;
|
||||
t += days_in_year(year - 1) * __seconds_per_day;
|
||||
ASSERT(t >= 0);
|
||||
|
||||
int days = t / __seconds_per_day;
|
||||
|
@ -275,7 +275,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 = days_in_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)
|
||||
|
|
|
@ -216,8 +216,7 @@ String DateTime::to_string(const String& format) const
|
|||
if (tm.tm_yday >= 7 - wday_of_year_beginning)
|
||||
--week_number;
|
||||
else {
|
||||
const bool last_year_is_leap = ::is_leap_year(tm.tm_year + 1900 - 1);
|
||||
const int days_of_last_year = 365 + last_year_is_leap;
|
||||
const int days_of_last_year = days_in_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)
|
||||
|
|
Loading…
Reference in a new issue