summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-21 13:24:52 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-21 21:12:54 +0200
commit116c0c0ab3cffedf429e5ca74a6d4bbba15c7247 (patch)
treeece722f43a705fca35b9c69575c849661a447d31
parent6e5aa5d5dfb38272143f8b7bfa81c2a8aacac66e (diff)
downloadserenity-116c0c0ab3cffedf429e5ca74a6d4bbba15c7247.zip
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 :^)
-rw-r--r--Libraries/LibJS/Runtime/DateConstructor.cpp17
-rw-r--r--Libraries/LibJS/Tests/builtins/Date/Date.js6
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<u16>(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<u16>(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<time_t>(value / 1000));
- auto milliseconds = static_cast<u16>(fmod(value, 1000));
+ double value_as_double = value.to_double(interpreter);
+ auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000));
+ auto milliseconds = static_cast<u16>(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.