From 116c0c0ab3cffedf429e5ca74a6d4bbba15c7247 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 21 Aug 2020 13:24:52 -0400 Subject: LibJS: Implement Date's string constructor ... by calling Date.parse(). With this, dates on http://45.33.8.238/ and http://45.33.8.238/linux/summary.html are correctly converted to local time :^) --- Libraries/LibJS/Runtime/DateConstructor.cpp | 17 ++++++----------- Libraries/LibJS/Tests/builtins/Date/Date.js | 6 ++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index 1366a919bd..ec91f36897 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -173,21 +173,16 @@ Value DateConstructor::construct(Interpreter& interpreter, Function&) auto milliseconds = static_cast(tv.tv_usec / 1000); return Date::create(global_object(), datetime, milliseconds); } - if (interpreter.argument_count() == 1 && interpreter.argument(0).is_string()) { - // FIXME: Parse simplified ISO8601-like string, like Date.parse() will do. - struct timeval tv; - gettimeofday(&tv, nullptr); - auto datetime = Core::DateTime::now(); - auto milliseconds = static_cast(tv.tv_usec / 1000); - return Date::create(global_object(), datetime, milliseconds); - } if (interpreter.argument_count() == 1) { + auto value = interpreter.argument(0); + if (value.is_string()) + value = parse_simplified_iso8601(value.as_string().string()); // A timestamp since the epoch, in UTC. // FIXME: Date() probably should use a double as internal representation, so that NaN arguments and larger offsets are handled correctly. // FIXME: DateTime::from_timestamp() seems to not support negative offsets. - double value = interpreter.argument(0).to_double(interpreter); - auto datetime = Core::DateTime::from_timestamp(static_cast(value / 1000)); - auto milliseconds = static_cast(fmod(value, 1000)); + double value_as_double = value.to_double(interpreter); + auto datetime = Core::DateTime::from_timestamp(static_cast(value_as_double / 1000)); + auto milliseconds = static_cast(fmod(value_as_double, 1000)); return Date::create(global_object(), datetime, milliseconds); } // A date/time in components, in local time. diff --git a/Libraries/LibJS/Tests/builtins/Date/Date.js b/Libraries/LibJS/Tests/builtins/Date/Date.js index c7a2669675..80551e638b 100644 --- a/Libraries/LibJS/Tests/builtins/Date/Date.js +++ b/Libraries/LibJS/Tests/builtins/Date/Date.js @@ -4,6 +4,12 @@ test("basic functionality", () => { expect(Date.prototype).not.toHaveProperty("length"); }); +test("string constructor", () => { + // The string constructor is the same as calling the timestamp constructor with the result of Date.parse(arguments). + // Since that has exhaustive tests in Date.parse.js, just do some light smoke testing here. + expect(new Date("2017-09-07T21:08:59.001Z").toISOString()).toBe("2017-09-07T21:08:59.001Z"); +}); + test("timestamp constructor", () => { // The timestamp constructor takes a timestamp in milliseconds since the start of the epoch, in UTC. -- cgit v1.2.3