2020-03-30 00:21:56 +01:00
|
|
|
|
/*
|
2023-04-13 00:47:15 +02:00
|
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
2020-08-21 12:51:00 -04:00
|
|
|
|
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
|
2021-03-16 15:02:16 +01:00
|
|
|
|
* Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com>
|
2024-11-26 10:51:11 -05:00
|
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
|
2020-03-30 00:21:56 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-30 00:21:56 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2021-06-13 10:47:09 +02:00
|
|
|
|
#include <AK/CharacterTypes.h>
|
2020-08-21 12:51:00 -04:00
|
|
|
|
#include <AK/GenericLexer.h>
|
2022-01-14 08:22:46 -05:00
|
|
|
|
#include <AK/Time.h>
|
2020-03-30 00:21:56 +01:00
|
|
|
|
#include <LibCore/DateTime.h>
|
2021-06-20 01:09:39 +01:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-03-30 00:21:56 +01:00
|
|
|
|
#include <LibJS/Runtime/Date.h>
|
|
|
|
|
#include <LibJS/Runtime/DateConstructor.h>
|
2022-01-14 17:42:42 -05:00
|
|
|
|
#include <LibJS/Runtime/DatePrototype.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2024-11-26 10:51:11 -05:00
|
|
|
|
#include <LibJS/Runtime/Temporal/Now.h>
|
2020-09-27 20:18:30 +02:00
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2023-10-06 17:54:21 +02:00
|
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2020-03-30 00:21:56 +01:00
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(DateConstructor);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
|
2024-11-26 11:05:57 -05:00
|
|
|
|
static Optional<double> parse_simplified_iso8601(ByteString const& iso_8601)
|
2020-09-18 09:49:51 +02:00
|
|
|
|
{
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format
|
2020-08-21 12:51:00 -04:00
|
|
|
|
GenericLexer lexer(iso_8601);
|
2021-07-10 18:47:16 +01:00
|
|
|
|
auto lex_n_digits = [&](size_t n, Optional<int>& out) {
|
2020-08-21 12:51:00 -04:00
|
|
|
|
if (lexer.tell_remaining() < n)
|
|
|
|
|
return false;
|
|
|
|
|
int r = 0;
|
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
|
char ch = lexer.consume();
|
2021-06-13 10:47:09 +02:00
|
|
|
|
if (!is_ascii_digit(ch))
|
2020-08-21 12:51:00 -04:00
|
|
|
|
return false;
|
|
|
|
|
r = 10 * r + ch - '0';
|
|
|
|
|
}
|
|
|
|
|
out = r;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-10 18:47:16 +01:00
|
|
|
|
Optional<int> year;
|
|
|
|
|
Optional<int> month;
|
|
|
|
|
Optional<int> day;
|
|
|
|
|
Optional<int> hours;
|
|
|
|
|
Optional<int> minutes;
|
|
|
|
|
Optional<int> seconds;
|
|
|
|
|
Optional<int> milliseconds;
|
|
|
|
|
Optional<char> timezone;
|
|
|
|
|
Optional<int> timezone_hours;
|
|
|
|
|
Optional<int> timezone_minutes;
|
|
|
|
|
|
2020-08-21 12:51:00 -04:00
|
|
|
|
auto lex_year = [&]() {
|
|
|
|
|
if (lexer.consume_specific('+'))
|
|
|
|
|
return lex_n_digits(6, year);
|
|
|
|
|
if (lexer.consume_specific('-')) {
|
2021-07-10 18:47:16 +01:00
|
|
|
|
Optional<int> absolute_year;
|
2020-08-21 12:51:00 -04:00
|
|
|
|
if (!lex_n_digits(6, absolute_year))
|
|
|
|
|
return false;
|
2022-04-30 20:50:17 +03:00
|
|
|
|
// The representation of the year 0 as -000000 is invalid.
|
|
|
|
|
if (absolute_year.value() == 0)
|
|
|
|
|
return false;
|
2021-07-10 18:47:16 +01:00
|
|
|
|
year = -absolute_year.value();
|
2020-08-21 12:51:00 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return lex_n_digits(4, year);
|
|
|
|
|
};
|
2021-07-10 18:47:16 +01:00
|
|
|
|
auto lex_month = [&]() { return lex_n_digits(2, month) && *month >= 1 && *month <= 12; };
|
|
|
|
|
auto lex_day = [&]() { return lex_n_digits(2, day) && *day >= 1 && *day <= 31; };
|
2020-08-21 12:51:00 -04:00
|
|
|
|
auto lex_date = [&]() { return lex_year() && (!lexer.consume_specific('-') || (lex_month() && (!lexer.consume_specific('-') || lex_day()))); };
|
|
|
|
|
|
2021-07-10 18:47:16 +01:00
|
|
|
|
auto lex_hours_minutes = [&](Optional<int>& out_h, Optional<int>& out_m) {
|
|
|
|
|
Optional<int> h;
|
|
|
|
|
Optional<int> m;
|
|
|
|
|
if (lex_n_digits(2, h) && *h >= 0 && *h <= 24 && lexer.consume_specific(':') && lex_n_digits(2, m) && *m >= 0 && *m <= 59) {
|
|
|
|
|
out_h = move(h);
|
|
|
|
|
out_m = move(m);
|
2020-08-21 12:51:00 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2021-07-10 18:47:16 +01:00
|
|
|
|
auto lex_seconds = [&]() { return lex_n_digits(2, seconds) && *seconds >= 0 && *seconds <= 59; };
|
2022-03-09 13:46:12 +01:00
|
|
|
|
auto lex_milliseconds = [&]() {
|
|
|
|
|
// Date.parse() is allowed to accept an arbitrary number of implementation-defined formats.
|
|
|
|
|
// Milliseconds are parsed slightly different as other engines allow effectively any number of digits here.
|
|
|
|
|
// We require at least one digit and only use the first three.
|
|
|
|
|
|
|
|
|
|
auto digits_read = 0;
|
|
|
|
|
int result = 0;
|
|
|
|
|
while (!lexer.is_eof() && is_ascii_digit(lexer.peek())) {
|
|
|
|
|
char ch = lexer.consume();
|
|
|
|
|
if (digits_read < 3)
|
|
|
|
|
result = 10 * result + ch - '0';
|
|
|
|
|
|
|
|
|
|
++digits_read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (digits_read == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// If we got less than three digits pretend we have trailing zeros.
|
|
|
|
|
while (digits_read < 3) {
|
|
|
|
|
result *= 10;
|
|
|
|
|
++digits_read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
milliseconds = result;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2020-08-21 12:51:00 -04:00
|
|
|
|
auto lex_seconds_milliseconds = [&]() { return lex_seconds() && (!lexer.consume_specific('.') || lex_milliseconds()); };
|
|
|
|
|
auto lex_timezone = [&]() {
|
|
|
|
|
if (lexer.consume_specific('+')) {
|
|
|
|
|
timezone = '+';
|
|
|
|
|
return lex_hours_minutes(timezone_hours, timezone_minutes);
|
|
|
|
|
}
|
|
|
|
|
if (lexer.consume_specific('-')) {
|
|
|
|
|
timezone = '-';
|
|
|
|
|
return lex_hours_minutes(timezone_hours, timezone_minutes);
|
|
|
|
|
}
|
|
|
|
|
if (lexer.consume_specific('Z'))
|
|
|
|
|
timezone = 'Z';
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
auto lex_time = [&]() { return lex_hours_minutes(hours, minutes) && (!lexer.consume_specific(':') || lex_seconds_milliseconds()) && lex_timezone(); };
|
|
|
|
|
|
2024-11-26 11:05:57 -05:00
|
|
|
|
if (!lex_date() || (lexer.consume_specific('T') && !lex_time()) || !lexer.is_eof())
|
|
|
|
|
return {};
|
2020-08-21 12:51:00 -04:00
|
|
|
|
|
2021-07-10 18:47:16 +01:00
|
|
|
|
// We parsed a valid date simplified ISO 8601 string.
|
|
|
|
|
VERIFY(year.has_value()); // A valid date string always has at least a year.
|
2023-03-13 22:35:22 +01:00
|
|
|
|
auto time = AK::UnixDateTime::from_unix_time_parts(*year, month.value_or(1), day.value_or(1), hours.value_or(0), minutes.value_or(0), seconds.value_or(0), milliseconds.value_or(0));
|
|
|
|
|
auto time_ms = static_cast<double>(time.milliseconds_since_epoch());
|
2020-08-21 12:51:00 -04:00
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// https://tc39.es/ecma262/#sec-date.parse:
|
2020-08-21 12:51:00 -04:00
|
|
|
|
// "When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time."
|
2022-01-14 08:22:46 -05:00
|
|
|
|
if (!timezone.has_value() && hours.has_value())
|
2022-01-14 17:33:54 -05:00
|
|
|
|
time_ms = utc_time(time_ms);
|
2020-08-21 12:51:00 -04:00
|
|
|
|
|
|
|
|
|
if (timezone == '-')
|
2022-01-14 08:22:46 -05:00
|
|
|
|
time_ms += *timezone_hours * 3'600'000 + *timezone_minutes * 60'000;
|
2020-08-21 12:51:00 -04:00
|
|
|
|
else if (timezone == '+')
|
2022-01-14 08:22:46 -05:00
|
|
|
|
time_ms -= *timezone_hours * 3'600'000 + *timezone_minutes * 60'000;
|
2020-08-21 12:51:00 -04:00
|
|
|
|
|
2022-05-06 20:45:25 +02:00
|
|
|
|
return time_clip(time_ms);
|
2020-08-21 12:51:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-08 11:15:07 -04:00
|
|
|
|
static double parse_date_string(VM& vm, ByteString const& date_string)
|
2021-09-26 23:35:00 +02:00
|
|
|
|
{
|
2024-10-13 10:21:41 +13:00
|
|
|
|
if (date_string.is_empty())
|
|
|
|
|
return NAN;
|
|
|
|
|
|
2024-11-26 11:05:57 -05:00
|
|
|
|
if (auto time = parse_simplified_iso8601(date_string); time.has_value())
|
|
|
|
|
return *time;
|
2021-09-26 23:35:00 +02:00
|
|
|
|
|
|
|
|
|
// Date.parse() is allowed to accept an arbitrary number of implementation-defined formats.
|
2022-09-17 17:12:31 +02:00
|
|
|
|
// FIXME: Exactly what timezone and which additional formats we should support is unclear.
|
|
|
|
|
// Both Chrome and Firefox seem to support "4/17/2019 11:08 PM +0000" with most parts
|
|
|
|
|
// being optional, however this is not clearly documented anywhere.
|
2023-11-01 15:38:50 -04:00
|
|
|
|
static constexpr auto extra_formats = AK::Array {
|
2024-01-28 23:36:40 -06:00
|
|
|
|
"%a%t%b%t%d%t%Y%t%T%tGMT%z%t(%+)"sv, // "Tue Nov 07 2023 10:05:55 GMT-0500 (Eastern Standard Time)"
|
|
|
|
|
"%a,%t%d%t%b%t%Y%t%T%t%Z"sv, // "Tue, 07 Nov 2023 15:05:55 GMT"
|
|
|
|
|
"%a%t%b%t%e%t%T%t%z%t%Y"sv, // "Wed Apr 17 23:08:53 +0000 2019"
|
|
|
|
|
"%m/%e/%Y"sv, // "4/17/2019"
|
|
|
|
|
"%m/%e/%Y%t%R%t%z"sv, // "12/05/2022 10:00 -0800"
|
|
|
|
|
"%Y/%m/%e%t%R"sv, // "2014/11/14 13:05"
|
|
|
|
|
"%Y-%m-%e%t%R"sv, // "2014-11-14 13:05"
|
2024-09-08 10:42:26 -04:00
|
|
|
|
"%B%t%e,%t%Y"sv, // "June 5, 2023"
|
2024-01-28 23:36:40 -06:00
|
|
|
|
"%B%t%e,%t%Y%t%T"sv, // "June 5, 2023 17:00:00"
|
|
|
|
|
"%b%t%d%t%Y%t%Z"sv, // "Jan 01 1970 GMT"
|
|
|
|
|
"%a%t%b%t%e%t%T%t%Y%t%z"sv, // "Wed Apr 17 23:08:53 2019 +0000"
|
|
|
|
|
"%Y-%m-%e%t%R%z"sv, // "2021-07-01 03:00Z"
|
|
|
|
|
"%a,%t%e%t%b%t%Y%t%T%t%z"sv, // "Wed, 17 Jan 2024 11:36:34 +0000"
|
|
|
|
|
"%a%t%b%t%e%t%Y%t%T%tGMT%t%x%t(%+)"sv, // "Sun Jan 21 2024 21:11:31 GMT 0100 (Central European Standard Time)"
|
|
|
|
|
"%Y-%m-%e%t%T"sv, // "2024-01-15 00:00:01"
|
|
|
|
|
"%a%t%b%t%e%t%Y%t%T%t%Z"sv, // "Tue Nov 07 2023 10:05:55 UTC"
|
|
|
|
|
"%a%t%b%t%e%t%T%t%Y"sv, // "Wed Apr 17 23:08:53 2019"
|
2024-10-14 20:42:42 -04:00
|
|
|
|
"%a%t%b%t%e%t%Y%t%T"sv, // "Wed Apr 17 2019 23:08:53"
|
2024-01-28 23:36:40 -06:00
|
|
|
|
"%Y-%m-%eT%T%X%z"sv, // "2024-01-26T22:10:11.306+0000"
|
|
|
|
|
"%m/%e/%Y,%t%T%t%p"sv, // "1/27/2024, 9:28:30 AM"
|
2024-09-14 12:21:10 +02:00
|
|
|
|
"%Y-%m-%e"sv, // "2024-1-15"
|
2024-09-20 18:23:42 -05:00
|
|
|
|
"%Y-%m-%e%t%T%tGMT%z"sv, // "2024-07-05 00:00:00 GMT-0800"
|
2024-10-14 20:42:42 -04:00
|
|
|
|
"%d%t%B%t%Y"sv, // "01 February 2013"
|
2024-10-25 11:14:01 +02:00
|
|
|
|
"%d%t%B%t%Y%t%R"sv, // "01 February 2013 08:00"
|
|
|
|
|
"%d%t%b%t%Y"sv, // "01 Jan 2000"
|
|
|
|
|
"%d%t%b%t%Y%t%R"sv, // "01 Jan 2000 08:00"
|
2024-10-30 10:24:10 +01:00
|
|
|
|
"%A,%t%B%t%e,%t%Y,%t%R%t%Z"sv, // "Tuesday, October 29, 2024, 18:00 UTC"
|
2024-11-12 11:05:05 +00:00
|
|
|
|
"%B%t%d%t%Y%t%T%t%z"sv, // "November 19 2024 00:00:00 +0900"
|
2024-11-19 21:53:14 -06:00
|
|
|
|
"%a%t%b%t%e%t%Y"sv // "Wed Nov 20 2024"
|
2023-11-01 15:38:50 -04:00
|
|
|
|
};
|
|
|
|
|
|
2022-09-17 17:12:31 +02:00
|
|
|
|
for (auto const& format : extra_formats) {
|
|
|
|
|
auto maybe_datetime = Core::DateTime::parse(format, date_string);
|
|
|
|
|
if (maybe_datetime.has_value())
|
|
|
|
|
return 1000.0 * maybe_datetime->timestamp();
|
|
|
|
|
}
|
2021-09-26 23:35:00 +02:00
|
|
|
|
|
2024-09-08 11:15:07 -04:00
|
|
|
|
vm.host_unrecognized_date_string(date_string);
|
2022-05-06 20:45:25 +02:00
|
|
|
|
return NAN;
|
2021-09-26 23:35:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
|
DateConstructor::DateConstructor(Realm& realm)
|
2023-04-13 00:47:15 +02:00
|
|
|
|
: NativeFunction(realm.vm().names.Date.as_string(), realm.intrinsics().function_prototype())
|
2020-06-20 15:40:48 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void DateConstructor::initialize(Realm& realm)
|
2020-03-30 00:21:56 +01:00
|
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
|
|
// 21.4.3.3 Date.prototype, https://tc39.es/ecma262/#sec-date.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().date_prototype(), 0);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
2021-06-13 00:58:25 +01:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 21:47:35 +01:00
|
|
|
|
define_native_function(realm, vm.names.now, now, 0, attr);
|
|
|
|
|
define_native_function(realm, vm.names.parse, parse, 1, attr);
|
|
|
|
|
define_native_function(realm, vm.names.UTC, utc, 7, attr);
|
2021-07-08 02:49:53 +03:00
|
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(7), Attribute::Configurable);
|
2020-03-30 00:21:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// 14.6.1 Date ( ...values ), https://tc39.es/proposal-temporal/#sec-temporal-date
|
2021-10-20 21:16:30 +01:00
|
|
|
|
ThrowCompletionOr<Value> DateConstructor::call()
|
2020-04-01 18:31:24 +01:00
|
|
|
|
{
|
2024-11-26 10:51:11 -05:00
|
|
|
|
auto& vm = this->vm();
|
2022-01-14 17:42:42 -05:00
|
|
|
|
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// 1. If NewTarget is undefined, return ToDateString(SystemUTCEpochMilliseconds()).
|
|
|
|
|
return PrimitiveString::create(vm, to_date_string(Temporal::system_utc_epoch_milliseconds(vm)));
|
2020-04-01 18:31:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// 14.6.1 Date ( ...values ), https://tc39.es/proposal-temporal/#sec-temporal-date
|
2024-11-15 04:01:23 +13:00
|
|
|
|
ThrowCompletionOr<GC::Ref<Object>> DateConstructor::construct(FunctionObject& new_target)
|
2020-03-30 00:21:56 +01:00
|
|
|
|
{
|
2021-02-13 12:35:31 +01:00
|
|
|
|
auto& vm = this->vm();
|
2021-06-20 01:09:39 +01:00
|
|
|
|
|
2022-05-06 20:45:25 +02:00
|
|
|
|
double date_value;
|
2022-01-14 17:42:42 -05:00
|
|
|
|
|
|
|
|
|
// 2. Let numberOfArgs be the number of elements in values.
|
|
|
|
|
// 3. If numberOfArgs = 0, then
|
2021-06-20 01:09:39 +01:00
|
|
|
|
if (vm.argument_count() == 0) {
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// a. Let dv be SystemUTCEpochMilliseconds().
|
|
|
|
|
date_value = Temporal::system_utc_epoch_milliseconds(vm);
|
2021-06-20 01:09:39 +01:00
|
|
|
|
}
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// 4. Else if numberOfArgs = 1, then
|
|
|
|
|
else if (vm.argument_count() == 1) {
|
|
|
|
|
// a. Let value be values[0].
|
|
|
|
|
auto value = vm.argument(0);
|
2022-05-06 20:45:25 +02:00
|
|
|
|
double time_value;
|
2021-03-16 15:02:16 +01:00
|
|
|
|
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// b. If Type(value) is Object and value has a [[DateValue]] internal slot, then
|
|
|
|
|
if (value.is_object() && is<Date>(value.as_object())) {
|
|
|
|
|
// i. Let tv be ! thisTimeValue(value).
|
2022-08-21 17:45:18 +01:00
|
|
|
|
time_value = MUST(this_time_value(vm, value));
|
2022-01-14 17:42:42 -05:00
|
|
|
|
}
|
|
|
|
|
// c. Else,
|
|
|
|
|
else {
|
|
|
|
|
// i. Let v be ? ToPrimitive(value).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto primitive = TRY(value.to_primitive(vm));
|
2022-01-14 17:42:42 -05:00
|
|
|
|
|
|
|
|
|
// ii. If Type(v) is String, then
|
|
|
|
|
if (primitive.is_string()) {
|
|
|
|
|
// 1. Assert: The next step never returns an abrupt completion because Type(v) is String.
|
|
|
|
|
// 2. Let tv be the result of parsing v as a date, in exactly the same manner as for the parse method (21.4.3.2).
|
2024-09-08 11:15:07 -04:00
|
|
|
|
time_value = parse_date_string(vm, primitive.as_string().byte_string());
|
2022-01-14 17:42:42 -05:00
|
|
|
|
}
|
|
|
|
|
// iii. Else,
|
|
|
|
|
else {
|
|
|
|
|
// 1. Let tv be ? ToNumber(v).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
time_value = TRY(primitive.to_number(vm)).as_double();
|
2022-01-14 17:42:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-16 15:02:16 +01:00
|
|
|
|
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// d. Let dv be TimeClip(tv).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
date_value = time_clip(time_value);
|
2020-08-20 10:41:10 -04:00
|
|
|
|
}
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// 5. Else,
|
|
|
|
|
else {
|
|
|
|
|
// a. Assert: numberOfArgs ≥ 2.
|
|
|
|
|
// b. Let y be ? ToNumber(values[0]).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto year = TRY(vm.argument(0).to_number(vm)).as_double();
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// c. Let m be ? ToNumber(values[1]).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto month = TRY(vm.argument(1).to_number(vm)).as_double();
|
2022-01-14 17:42:42 -05:00
|
|
|
|
|
2022-08-22 11:48:08 +01:00
|
|
|
|
auto arg_or = [&vm](size_t i, double fallback) -> ThrowCompletionOr<double> {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return vm.argument_count() > i ? TRY(vm.argument(i).to_number(vm)).as_double() : fallback;
|
2022-01-14 17:42:42 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// d. If numberOfArgs > 2, let dt be ? ToNumber(values[2]); else let dt be 1𝔽.
|
|
|
|
|
auto date = TRY(arg_or(2, 1));
|
|
|
|
|
// e. If numberOfArgs > 3, let h be ? ToNumber(values[3]); else let h be +0𝔽.
|
|
|
|
|
auto hours = TRY(arg_or(3, 0));
|
|
|
|
|
// f. If numberOfArgs > 4, let min be ? ToNumber(values[4]); else let min be +0𝔽.
|
|
|
|
|
auto minutes = TRY(arg_or(4, 0));
|
|
|
|
|
// g. If numberOfArgs > 5, let s be ? ToNumber(values[5]); else let s be +0𝔽.
|
|
|
|
|
auto seconds = TRY(arg_or(5, 0));
|
|
|
|
|
// h. If numberOfArgs > 6, let milli be ? ToNumber(values[6]); else let milli be +0𝔽.
|
|
|
|
|
auto milliseconds = TRY(arg_or(6, 0));
|
|
|
|
|
|
|
|
|
|
// i. If y is NaN, let yr be NaN.
|
|
|
|
|
// j. Else,
|
2022-05-06 20:45:25 +02:00
|
|
|
|
if (!isnan(year)) {
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// i. Let yi be ! ToIntegerOrInfinity(y).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
auto year_integer = to_integer_or_infinity(year);
|
2022-01-14 17:42:42 -05:00
|
|
|
|
|
|
|
|
|
// ii. If 0 ≤ yi ≤ 99, let yr be 1900𝔽 + 𝔽(yi); otherwise, let yr be y.
|
2022-05-06 20:45:25 +02:00
|
|
|
|
if (0 <= year_integer && year_integer <= 99)
|
|
|
|
|
year = 1900 + year_integer;
|
2022-01-14 17:42:42 -05:00
|
|
|
|
}
|
2021-03-16 15:02:16 +01:00
|
|
|
|
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// k. Let finalDate be MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli)).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
auto day = make_day(year, month, date);
|
|
|
|
|
auto time = make_time(hours, minutes, seconds, milliseconds);
|
2022-01-14 17:42:42 -05:00
|
|
|
|
auto final_date = make_date(day, time);
|
2021-03-16 15:02:16 +01:00
|
|
|
|
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// l. Let dv be TimeClip(UTC(finalDate)).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
date_value = time_clip(utc_time(final_date));
|
2020-08-24 09:26:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 17:42:42 -05:00
|
|
|
|
// 6. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Date.prototype%", « [[DateValue]] »).
|
|
|
|
|
// 7. Set O.[[DateValue]] to dv.
|
|
|
|
|
// 8. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
|
return TRY(ordinary_create_from_constructor<Date>(vm, new_target, &Intrinsics::date_prototype, date_value));
|
2020-03-30 00:21:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.3.1 Date.now ( ), https://tc39.es/ecma262/#sec-date.now
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// 14.7.1 Date.now ( ), https://tc39.es/proposal-temporal/#sec-temporal-date.now
|
2021-10-23 03:28:01 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now)
|
2020-03-30 00:21:56 +01:00
|
|
|
|
{
|
2024-11-26 10:51:11 -05:00
|
|
|
|
// 1. Return SystemUTCEpochMilliseconds().
|
|
|
|
|
return Temporal::system_utc_epoch_milliseconds(vm);
|
2020-03-30 00:21:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
|
2021-10-23 03:28:01 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse)
|
2020-08-21 12:51:00 -04:00
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
|
if (!vm.argument_count())
|
2020-08-21 12:51:00 -04:00
|
|
|
|
return js_nan();
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
|
auto date_string = TRY(vm.argument(0).to_byte_string(vm));
|
2020-08-21 12:51:00 -04:00
|
|
|
|
|
2024-09-08 11:15:07 -04:00
|
|
|
|
return Value(parse_date_string(vm, date_string));
|
2020-08-21 12:51:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc
|
2021-10-23 03:28:01 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
|
2020-08-20 16:02:07 -04:00
|
|
|
|
{
|
2022-08-22 11:48:08 +01:00
|
|
|
|
auto arg_or = [&vm](size_t i, double fallback) -> ThrowCompletionOr<double> {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return vm.argument_count() > i ? TRY(vm.argument(i).to_number(vm)).as_double() : fallback;
|
2021-10-17 23:33:35 +03:00
|
|
|
|
};
|
2020-08-20 16:02:07 -04:00
|
|
|
|
|
2022-01-05 11:19:17 -05:00
|
|
|
|
// 1. Let y be ? ToNumber(year).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto year = TRY(vm.argument(0).to_number(vm)).as_double();
|
2022-01-05 11:19:17 -05:00
|
|
|
|
// 2. If month is present, let m be ? ToNumber(month); else let m be +0𝔽.
|
|
|
|
|
auto month = TRY(arg_or(1, 0));
|
|
|
|
|
// 3. If date is present, let dt be ? ToNumber(date); else let dt be 1𝔽.
|
|
|
|
|
auto date = TRY(arg_or(2, 1));
|
|
|
|
|
// 4. If hours is present, let h be ? ToNumber(hours); else let h be +0𝔽.
|
|
|
|
|
auto hours = TRY(arg_or(3, 0));
|
|
|
|
|
// 5. If minutes is present, let min be ? ToNumber(minutes); else let min be +0𝔽.
|
|
|
|
|
auto minutes = TRY(arg_or(4, 0));
|
|
|
|
|
// 6. If seconds is present, let s be ? ToNumber(seconds); else let s be +0𝔽.
|
|
|
|
|
auto seconds = TRY(arg_or(5, 0));
|
|
|
|
|
// 7. If ms is present, let milli be ? ToNumber(ms); else let milli be +0𝔽.
|
|
|
|
|
auto milliseconds = TRY(arg_or(6, 0));
|
|
|
|
|
|
|
|
|
|
// 8. If y is NaN, let yr be NaN.
|
|
|
|
|
// 9. Else,
|
2022-05-06 20:45:25 +02:00
|
|
|
|
if (!isnan(year)) {
|
2022-01-05 11:19:17 -05:00
|
|
|
|
// a. Let yi be ! ToIntegerOrInfinity(y).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
auto year_integer = to_integer_or_infinity(year);
|
2022-01-05 11:19:17 -05:00
|
|
|
|
|
|
|
|
|
// b. If 0 ≤ yi ≤ 99, let yr be 1900𝔽 + 𝔽(yi); otherwise, let yr be y.
|
2022-05-06 20:45:25 +02:00
|
|
|
|
if (0 <= year_integer && year_integer <= 99)
|
|
|
|
|
year = 1900 + year_integer;
|
2022-01-05 11:19:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 10. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
|
2022-05-06 20:45:25 +02:00
|
|
|
|
auto day = make_day(year, month, date);
|
|
|
|
|
auto time = make_time(hours, minutes, seconds, milliseconds);
|
|
|
|
|
return Value(time_clip(make_date(day, time)));
|
2020-08-20 16:02:07 -04:00
|
|
|
|
}
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
2020-03-30 00:21:56 +01:00
|
|
|
|
}
|