mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 22:38:13 +00:00
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 :^)
This commit is contained in:
parent
6e5aa5d5df
commit
116c0c0ab3
2 changed files with 12 additions and 11 deletions
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue