diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-10 18:12:12 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-10 19:51:07 +0100 |
commit | 39cdffd78d37801a9b9684d0781b18570b5e0129 (patch) | |
tree | 2986698c9d66a70f64479ad6e8418c2b00156772 | |
parent | a5bc366d9a00a261e39e2968f03beb1245919e49 (diff) | |
download | serenity-39cdffd78d37801a9b9684d0781b18570b5e0129.zip |
LibJS: Make Date.now() return a floor()'d milliseconds value
It is defined as follows:
21.4.3.1 Date.now ( )
https://tc39.es/ecma262/#sec-date.now
The now function returns the time value designating the UTC date and
time of the occurrence of the call to now.
"Time value" is defined as:
21.4.1.1 Time Values and Time Range
https://tc39.es/ecma262/#sec-time-values-and-time-range
An ECMAScript time value is a Number, either a finite integral
Number representing an instant in time to millisecond precision or
NaN representing no specific instant.
By flooring the value we match the behavior in the Temporal proposal's
Temporal.ZonedDateTime.prototype.epochMilliseconds getter:
4. Let ms be RoundTowardsZero(ℝ(ns) / 10^6).
With that being defined as:
13.30 RoundTowardsZero ( x )
https://tc39.es/proposal-temporal/#sec-temporal-roundtowardszero
1. Return the mathematical value that is the same sign as x and
whose magnitude is floor(abs(x)).
This is makes the last of the currently 15 Temporal tests in test262
work, which compares Temporal.now.instant() with Date.now() :^)
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/DateConstructor.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js | 1 |
2 files changed, 2 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp index 218a58db6f..d453a8a4d2 100644 --- a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -290,7 +290,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now) { struct timeval tv; gettimeofday(&tv, nullptr); - return Value(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0); + return Value(floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0)); } // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js index 46c1b6d8cd..490787f6f5 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js @@ -3,6 +3,7 @@ test("basic functionality", () => { for (var i = 0; i < 100; ++i) { var now = Date.now(); expect(now).not.toBeNaN(); + expect(Math.floor(now)).toBe(now); expect(now).toBeGreaterThan(1580000000000); expect(now).toBeGreaterThanOrEqual(last); last = now; |