1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Use AK::Time in system_utc_epoch_nanoseconds()

This also uses <time.h> APIs internally, but wraps them in a much nicer
interface.
This commit is contained in:
Linus Groh 2022-04-03 01:09:41 +01:00
parent 4a6a7cf3c8
commit 83e8dfae38

View file

@ -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));