mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:27:35 +00:00
LibJS Date: Added "Invalid Date".
Setting an invalid value on a Date object now makes it invalid. Setting it again but with correct values makes it valid again.
This commit is contained in:
parent
d231c5e65b
commit
ca49f96b78
29 changed files with 490 additions and 89 deletions
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
|
||||
* Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -176,30 +177,92 @@ Value DateConstructor::construct(Function&)
|
|||
auto milliseconds = static_cast<u16>(tv.tv_usec / 1000);
|
||||
return Date::create(global_object(), datetime, milliseconds);
|
||||
}
|
||||
|
||||
auto create_invalid_date = [this]() {
|
||||
auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(0));
|
||||
auto milliseconds = static_cast<u16>(0);
|
||||
return Date::create(global_object(), datetime, milliseconds, true);
|
||||
};
|
||||
|
||||
if (vm.argument_count() == 1) {
|
||||
auto value = vm.argument(0);
|
||||
if (value.is_string())
|
||||
value = parse_simplified_iso8601(value.as_string().string());
|
||||
// A timestamp since the epoch, in UTC.
|
||||
// FIXME: This doesn't construct an "Invalid Date" object if the argument is NaN.
|
||||
// FIXME: Date() probably should use a double as internal representation, so that NaN arguments and larger offsets are handled correctly.
|
||||
double value_as_double = value.to_double(global_object());
|
||||
else
|
||||
value = value.to_number(global_object());
|
||||
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (!value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
|
||||
// A timestamp since the epoch, in UTC.
|
||||
double value_as_double = value.as_double();
|
||||
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.
|
||||
// FIXME: This doesn't construct an "Invalid Date" object if one of the arguments is NaN.
|
||||
auto arg_or = [this, &vm](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_i32(global_object()) : fallback; };
|
||||
int year = vm.argument(0).to_i32(global_object());
|
||||
int month_index = vm.argument(1).to_i32(global_object());
|
||||
int day = arg_or(2, 1);
|
||||
int hours = arg_or(3, 0);
|
||||
int minutes = arg_or(4, 0);
|
||||
int seconds = arg_or(5, 0);
|
||||
int milliseconds = arg_or(6, 0);
|
||||
auto arg_or = [&vm, this](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object()) : Value(fallback); };
|
||||
|
||||
auto year_value = vm.argument(0).to_number(global_object());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!year_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto year = year_value.as_i32();
|
||||
|
||||
auto month_index_value = vm.argument(1).to_number(global_object());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!month_index_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto month_index = month_index_value.as_i32();
|
||||
|
||||
auto day_value = arg_or(2, 1);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!day_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto day = day_value.as_i32();
|
||||
|
||||
auto hours_value = arg_or(3, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!hours_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto hours = hours_value.as_i32();
|
||||
|
||||
auto minutes_value = arg_or(4, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!minutes_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto minutes = minutes_value.as_i32();
|
||||
|
||||
auto seconds_value = arg_or(5, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!seconds_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto seconds = seconds_value.as_i32();
|
||||
|
||||
auto milliseconds_value = arg_or(6, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (!milliseconds_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto milliseconds = milliseconds_value.as_i32();
|
||||
|
||||
seconds += milliseconds / 1000;
|
||||
milliseconds %= 1000;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue