summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-04-03 01:09:41 +0100
committerLinus Groh <mail@linusgroh.de>2022-04-03 01:10:31 +0100
commit83e8dfae380c407401978a5430694045aba2f5f9 (patch)
tree0a44bd5882a673b558682da7e2732913c1b6e23d
parent4a6a7cf3c821a799ecf731a1438e4ce8ff3f9d41 (diff)
downloadserenity-83e8dfae380c407401978a5430694045aba2f5f9.zip
LibJS: Use AK::Time in system_utc_epoch_nanoseconds()
This also uses <time.h> APIs internally, but wraps them in a much nicer interface.
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp18
1 files changed, 5 insertions, 13 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp
index a37c39383b..27d4ac9f97 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Time.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
@@ -15,7 +16,6 @@
#include <LibJS/Runtime/Temporal/PlainTime.h>
#include <LibJS/Runtime/Temporal/TimeZone.h>
#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
-#include <time.h>
namespace JS::Temporal {
@@ -161,20 +161,12 @@ TimeZone* system_time_zone(GlobalObject& global_object)
BigInt* system_utc_epoch_nanoseconds(GlobalObject& global_object)
{
// 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
- struct timespec now;
- clock_gettime(CLOCK_REALTIME, &now);
- Checked<i64> ns_timestamp;
- ns_timestamp += now.tv_sec;
- ns_timestamp *= 1'000'000'000;
- ns_timestamp += now.tv_nsec;
- if (ns_timestamp.has_overflow()) {
- // TODO: Deal with this before 2262-04-21T00:47:16Z.
- VERIFY_NOT_REACHED();
- }
- auto ns = Crypto::SignedBigInteger::create_from(ns_timestamp.value());
+ auto now = Time::now_realtime().to_nanoseconds();
+ auto ns = Crypto::SignedBigInteger::create_from(now);
// 2. Set ns to the result of clamping ns between −8.64 × 10^21 and 8.64 × 10^21.
- // Uhh, these don't even fit in an i64... ¯\_(ツ)_/¯
+ // NOTE: Time::to_nanoseconds() already clamps between −(2^63) and 2^63 − 1, the range of an i64,
+ // if an overflow occurs during seconds -> nanoseconds conversion.
// 3. Return ℤ(ns).
return js_bigint(global_object.heap(), move(ns));