summaryrefslogtreecommitdiff
path: root/AK/Time.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-20 13:54:25 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-23 12:48:26 +0000
commitb873a222d7ece48f227516a3f2ba8717018ebae4 (patch)
tree00dc434459a1454d23ccc4bad9bbae4f6274ba7d /AK/Time.h
parente986ce961adab6193ad35b3ef188b134c0cb88a1 (diff)
downloadserenity-b873a222d7ece48f227516a3f2ba8717018ebae4.zip
AK: Implement AK::Time's seconds_since_epoch_to_year without LibM
In order for this method to be used within LibC itself, avoid using the floor() method from LibM, which is not available from within LibC. This header similarly avoids other standard headers as well.
Diffstat (limited to 'AK/Time.h')
-rw-r--r--AK/Time.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/AK/Time.h b/AK/Time.h
index 3752c37949..adea9fe2b1 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -10,7 +10,6 @@
#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.
@@ -83,8 +82,17 @@ constexpr i64 seconds_since_epoch_to_year(i64 seconds)
{
constexpr double seconds_per_year = 60.0 * 60.0 * 24.0 * 365.2425;
+ // NOTE: We are not using floor() from <math.h> to avoid LibC / DynamicLoader dependency issues.
+ auto round_down = [](double value) -> i64 {
+ auto as_i64 = static_cast<i64>(value);
+
+ if ((value == as_i64) || (as_i64 >= 0))
+ return as_i64;
+ return as_i64 - 1;
+ };
+
auto years_since_epoch = static_cast<double>(seconds) / seconds_per_year;
- return 1970 + static_cast<i64>(floor(years_since_epoch));
+ return 1970 + round_down(years_since_epoch);
}
/*