diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index 1dc4cbfa1d3..2acfc149314 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -2,7 +2,7 @@ * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2020, Nico Weber * Copyright (c) 2021, Petróczi Zoltán - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,10 +16,9 @@ #include #include #include +#include #include #include -#include -#include namespace JS { @@ -227,17 +226,17 @@ void DateConstructor::initialize(Realm& realm) } // 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date +// 14.6.1 Date ( ...values ), https://tc39.es/proposal-temporal/#sec-temporal-date ThrowCompletionOr DateConstructor::call() { - // 1. If NewTarget is undefined, then - // a. Let now be the time value (UTC) identifying the current time. - auto now = AK::UnixDateTime::now().milliseconds_since_epoch(); + auto& vm = this->vm(); - // b. Return ToDateString(now). - return PrimitiveString::create(vm(), to_date_string(now)); + // 1. If NewTarget is undefined, return ToDateString(SystemUTCEpochMilliseconds()). + return PrimitiveString::create(vm, to_date_string(Temporal::system_utc_epoch_milliseconds(vm))); } // 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date +// 14.6.1 Date ( ...values ), https://tc39.es/proposal-temporal/#sec-temporal-date ThrowCompletionOr> DateConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); @@ -247,9 +246,8 @@ ThrowCompletionOr> DateConstructor::construct(FunctionObject& ne // 2. Let numberOfArgs be the number of elements in values. // 3. If numberOfArgs = 0, then if (vm.argument_count() == 0) { - // a. Let dv be the time value (UTC) identifying the current time. - auto now = AK::UnixDateTime::now().milliseconds_since_epoch(); - date_value = static_cast(now); + // a. Let dv be SystemUTCEpochMilliseconds(). + date_value = Temporal::system_utc_epoch_milliseconds(vm); } // 4. Else if numberOfArgs = 1, then else if (vm.argument_count() == 1) { @@ -333,11 +331,11 @@ ThrowCompletionOr> DateConstructor::construct(FunctionObject& ne } // 21.4.3.1 Date.now ( ), https://tc39.es/ecma262/#sec-date.now +// 14.7.1 Date.now ( ), https://tc39.es/proposal-temporal/#sec-temporal-date.now JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now) { - struct timeval tv; - gettimeofday(&tv, nullptr); - return Value(floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0)); + // 1. Return SystemUTCEpochMilliseconds(). + return Temporal::system_utc_epoch_milliseconds(vm); } // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse diff --git a/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Libraries/LibJS/Runtime/Temporal/Now.cpp index dce9540053c..54b0bc311b7 100644 --- a/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -119,6 +119,19 @@ JS_DEFINE_NATIVE_FUNCTION(Now::plain_time_iso) return MUST(create_temporal_time(vm, iso_date_time.time)); } +// 2.3.2 SystemUTCEpochMilliseconds ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemutcepochmilliseconds +double system_utc_epoch_milliseconds(VM& vm) +{ + // 1. Let global be GetGlobalObject(). + auto const& global = vm.get_global_object(); + + // 2. Let nowNs be HostSystemUTCEpochNanoseconds(global). + auto now_ns = vm.host_system_utc_epoch_nanoseconds(global); + + // 3. Return 𝔽(floor(nowNs / 10**6)). + return big_floor(now_ns, NANOSECONDS_PER_MILLISECOND).to_double(); +} + // 2.3.3 SystemUTCEpochNanoseconds ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemutcepochnanoseconds Crypto::SignedBigInteger system_utc_epoch_nanoseconds(VM& vm) { diff --git a/Libraries/LibJS/Runtime/Temporal/Now.h b/Libraries/LibJS/Runtime/Temporal/Now.h index 0fea8d993c3..8c5bad0b381 100644 --- a/Libraries/LibJS/Runtime/Temporal/Now.h +++ b/Libraries/LibJS/Runtime/Temporal/Now.h @@ -32,6 +32,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(plain_time_iso); }; +double system_utc_epoch_milliseconds(VM&); Crypto::SignedBigInteger system_utc_epoch_nanoseconds(VM&); ThrowCompletionOr system_date_time(VM&, Value temporal_time_zone_like);