From 24459a44b0f3737e1ef785bb45912956500b62ba Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 21 Mar 2023 10:33:09 -0400 Subject: [PATCH] LibJS: Ensure Date tests can pass in any time zone by testing UTC values When we create a Date object from a timestamp, or set its value by way of Date.prototype.setTime, the timestamp is interpreted as UTC. Change test expecatations against such instances to use UTC as well. --- Userland/Libraries/LibJS/Tests/builtins/Date/Date.js | 10 ++++------ .../Tests/builtins/Date/Date.prototype.setTime.js | 12 ++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.js index 5a1c30178f..bbf5d449d9 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.js @@ -14,16 +14,14 @@ test("timestamp constructor", () => { // The timestamp constructor takes a timestamp in milliseconds since the start of the epoch, in UTC. // 50 days and 1234 milliseconds after the start of the epoch. - // Most Date methods return values in local time, but since timezone offsets are less than 17 days, - // these checks will pass in all timezones. let timestamp = 50 * 24 * 60 * 60 * 1000 + 1234; let date = new Date(timestamp); expect(date.getTime()).toBe(timestamp); // getTime() returns the timestamp in UTC. - expect(date.getMilliseconds()).toBe(234); - expect(date.getSeconds()).toBe(1); - expect(date.getFullYear()).toBe(1970); - expect(date.getMonth()).toBe(1); // Feb + expect(date.getUTCMilliseconds()).toBe(234); + expect(date.getUTCSeconds()).toBe(1); + expect(date.getUTCFullYear()).toBe(1970); + expect(date.getUTCMonth()).toBe(1); // Feb date = new Date(NaN); expect(date.getTime()).toBe(NaN); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setTime.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setTime.js index a26de088e7..334173da36 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setTime.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setTime.js @@ -22,9 +22,9 @@ test("Timestamp as argument", () => { let date = new Date(2021, 0, 1); date.setTime(1622993746000); - expect(date.getDate()).toBe(6); - expect(date.getMonth()).toBe(5); - expect(date.getFullYear()).toBe(2021); + expect(date.getUTCDate()).toBe(6); + expect(date.getUTCMonth()).toBe(5); + expect(date.getUTCFullYear()).toBe(2021); expect(date.getUTCHours()).toBe(15); expect(date.getUTCMinutes()).toBe(35); expect(date.getUTCSeconds()).toBe(46); @@ -37,9 +37,9 @@ test("Make Invalid Date valid again", () => { expect(date.getTime()).toBe(NaN); date.setTime(1622993746000); - expect(date.getDate()).toBe(6); - expect(date.getMonth()).toBe(5); - expect(date.getFullYear()).toBe(2021); + expect(date.getUTCDate()).toBe(6); + expect(date.getUTCMonth()).toBe(5); + expect(date.getUTCFullYear()).toBe(2021); expect(date.getUTCHours()).toBe(15); expect(date.getUTCMinutes()).toBe(35); expect(date.getUTCSeconds()).toBe(46);