From 39cdffd78d37801a9b9684d0781b18570b5e0129 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 10 Jul 2021 18:12:12 +0100 Subject: [PATCH] LibJS: Make Date.now() return a floor()'d milliseconds value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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() :^) --- Userland/Libraries/LibJS/Runtime/DateConstructor.cpp | 2 +- Userland/Libraries/LibJS/Tests/builtins/Date/Date.now.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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;