From e9e8e2bdf42a680b4073b79427e4112b74ff5c90 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 18 Jan 2022 17:05:21 -0500 Subject: [PATCH] AK: Add helper to convert an epoch time in seconds to a year --- AK/Time.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/AK/Time.h b/AK/Time.h index 645a9d67d0..3752c37949 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -10,6 +10,7 @@ #include #include #include +#include // Kernel and Userspace pull in the definitions from different places. // Avoid trying to figure out which one. @@ -78,6 +79,14 @@ constexpr int days_since_epoch(int year, int month, int day) return years_to_days_since_epoch(year) + day_of_year(year, month, day); } +constexpr i64 seconds_since_epoch_to_year(i64 seconds) +{ + constexpr double seconds_per_year = 60.0 * 60.0 * 24.0 * 365.2425; + + auto years_since_epoch = static_cast(seconds) / seconds_per_year; + return 1970 + static_cast(floor(years_since_epoch)); +} + /* * Represents a time amount in a "safe" way. * Minimum: 0 seconds, 0 nanoseconds @@ -338,6 +347,7 @@ using AK::days_in_month; using AK::days_in_year; using AK::days_since_epoch; using AK::is_leap_year; +using AK::seconds_since_epoch_to_year; using AK::Time; using AK::timespec_add; using AK::timespec_add_timeval;