summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-25 16:53:36 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-26 08:52:07 +0200
commit9b170828999d1b20da0e1f15c548188f6835a1cb (patch)
treea23b7dfd7fe93714e89d4b29d20972feec482c0f /AK
parent84ed2579594fdadef29ba145d8a7948ffe26200b (diff)
downloadserenity-9b170828999d1b20da0e1f15c548188f6835a1cb.zip
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.
Diffstat (limited to 'AK')
-rw-r--r--AK/Time.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/AK/Time.h b/AK/Time.h
index a5209908a9..309218747b 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -33,6 +33,16 @@ inline bool is_leap_year(int year)
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>
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_sub;
using AK::timeval_to_timespec;
+using AK::years_to_days_since_epoch;
using AK::operator<=;
using AK::operator<;
using AK::operator>;