Selaa lähdekoodia

LibJS: Use AK::Time in system_utc_epoch_nanoseconds()

This also uses <time.h> APIs internally, but wraps them in a much nicer
interface.
Linus Groh 3 vuotta sitten
vanhempi
commit
83e8dfae38
1 muutettua tiedostoa jossa 5 lisäystä ja 13 poistoa
  1. 5 13
      Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp

+ 5 - 13
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));