diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-18 17:05:21 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-19 21:20:41 +0000 |
commit | e9e8e2bdf42a680b4073b79427e4112b74ff5c90 (patch) | |
tree | 2ffbbfccf67a5a6e46774be9b930c8f240ee8c82 /AK/Time.h | |
parent | bc719e7bac1677ac00bd26ccbfcb346c9ee2ec02 (diff) | |
download | serenity-e9e8e2bdf42a680b4073b79427e4112b74ff5c90.zip |
AK: Add helper to convert an epoch time in seconds to a year
Diffstat (limited to 'AK/Time.h')
-rw-r--r-- | AK/Time.h | 10 |
1 files changed, 10 insertions, 0 deletions
@@ -10,6 +10,7 @@ #include <AK/Assertions.h> #include <AK/Platform.h> #include <AK/Types.h> +#include <math.h> // 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<double>(seconds) / seconds_per_year; + return 1970 + static_cast<i64>(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; |