|
@@ -1,20 +1,19 @@
|
|
|
/*
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
|
|
- * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
|
|
+ * Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
|
|
|
*
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
*/
|
|
|
|
|
|
-#include <AK/CharacterTypes.h>
|
|
|
-#include <AK/GenericLexer.h>
|
|
|
#include <AK/NumericLimits.h>
|
|
|
-#include <AK/ScopeGuard.h>
|
|
|
#include <AK/StringBuilder.h>
|
|
|
#include <AK/Time.h>
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
#include <LibJS/Runtime/Date.h>
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
|
|
+#include <LibJS/Runtime/Temporal/ISO8601.h>
|
|
|
+#include <LibJS/Runtime/Temporal/TimeZone.h>
|
|
|
#include <time.h>
|
|
|
|
|
|
namespace JS {
|
|
@@ -457,7 +456,7 @@ String system_time_zone_identifier()
|
|
|
// time zone identifier or an offset time zone identifier.
|
|
|
auto system_time_zone_string = Unicode::current_time_zone();
|
|
|
|
|
|
- if (!is_time_zone_offset_string(system_time_zone_string)) {
|
|
|
+ if (!is_offset_time_zone_identifier(system_time_zone_string)) {
|
|
|
auto time_zone_identifier = Intl::get_available_named_time_zone_identifier(system_time_zone_string);
|
|
|
if (!time_zone_identifier.has_value())
|
|
|
return "UTC"_string;
|
|
@@ -476,46 +475,55 @@ void clear_system_time_zone_cache()
|
|
|
}
|
|
|
|
|
|
// 21.4.1.25 LocalTime ( t ), https://tc39.es/ecma262/#sec-localtime
|
|
|
+// 14.5.6 LocalTime ( t ), https://tc39.es/proposal-temporal/#sec-localtime
|
|
|
double local_time(double time)
|
|
|
{
|
|
|
// 1. Let systemTimeZoneIdentifier be SystemTimeZoneIdentifier().
|
|
|
auto system_time_zone_identifier = JS::system_time_zone_identifier();
|
|
|
|
|
|
+ // 2. Let parseResult be ! ParseTimeZoneIdentifier(systemTimeZoneIdentifier).
|
|
|
+ auto parse_result = Temporal::parse_time_zone_identifier(system_time_zone_identifier);
|
|
|
+
|
|
|
double offset_nanoseconds { 0 };
|
|
|
|
|
|
- // 2. If IsTimeZoneOffsetString(systemTimeZoneIdentifier) is true, then
|
|
|
- if (is_time_zone_offset_string(system_time_zone_identifier)) {
|
|
|
- // a. Let offsetNs be ParseTimeZoneOffsetString(systemTimeZoneIdentifier).
|
|
|
- offset_nanoseconds = parse_time_zone_offset_string(system_time_zone_identifier);
|
|
|
+ // 3. If parseResult.[[OffsetMinutes]] is not EMPTY, then
|
|
|
+ if (parse_result.offset_minutes.has_value()) {
|
|
|
+ // a. Let offsetNs be parseResult.[[OffsetMinutes]] × (60 × 10**9).
|
|
|
+ offset_nanoseconds = static_cast<double>(*parse_result.offset_minutes) * 60'000'000'000;
|
|
|
}
|
|
|
- // 3. Else,
|
|
|
+ // 4. Else,
|
|
|
else {
|
|
|
// a. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, ℤ(ℝ(t) × 10^6)).
|
|
|
auto offset = get_named_time_zone_offset_milliseconds(system_time_zone_identifier, time);
|
|
|
offset_nanoseconds = static_cast<double>(offset.offset.to_nanoseconds());
|
|
|
}
|
|
|
|
|
|
- // 4. Let offsetMs be truncate(offsetNs / 10^6).
|
|
|
+ // 5. Let offsetMs be truncate(offsetNs / 10^6).
|
|
|
auto offset_milliseconds = trunc(offset_nanoseconds / 1e6);
|
|
|
|
|
|
- // 5. Return t + 𝔽(offsetMs).
|
|
|
+ // 6. Return t + 𝔽(offsetMs).
|
|
|
return time + offset_milliseconds;
|
|
|
}
|
|
|
|
|
|
// 21.4.1.26 UTC ( t ), https://tc39.es/ecma262/#sec-utc-t
|
|
|
+// 14.5.7 UTC ( t ), https://tc39.es/proposal-temporal/#sec-localtime
|
|
|
+// FIXME: Update the rest of this AO for Temporal once we have the required Temporal objects.
|
|
|
double utc_time(double time)
|
|
|
{
|
|
|
// 1. Let systemTimeZoneIdentifier be SystemTimeZoneIdentifier().
|
|
|
auto system_time_zone_identifier = JS::system_time_zone_identifier();
|
|
|
|
|
|
+ // 2. Let parseResult be ! ParseTimeZoneIdentifier(systemTimeZoneIdentifier).
|
|
|
+ auto parse_result = Temporal::parse_time_zone_identifier(system_time_zone_identifier);
|
|
|
+
|
|
|
double offset_nanoseconds { 0 };
|
|
|
|
|
|
- // 2. If IsTimeZoneOffsetString(systemTimeZoneIdentifier) is true, then
|
|
|
- if (is_time_zone_offset_string(system_time_zone_identifier)) {
|
|
|
- // a. Let offsetNs be ParseTimeZoneOffsetString(systemTimeZoneIdentifier).
|
|
|
- offset_nanoseconds = parse_time_zone_offset_string(system_time_zone_identifier);
|
|
|
+ // 3. If parseResult.[[OffsetMinutes]] is not EMPTY, then
|
|
|
+ if (parse_result.offset_minutes.has_value()) {
|
|
|
+ // a. Let offsetNs be parseResult.[[OffsetMinutes]] × (60 × 10**9).
|
|
|
+ offset_nanoseconds = static_cast<double>(*parse_result.offset_minutes) * 60'000'000'000;
|
|
|
}
|
|
|
- // 3. Else,
|
|
|
+ // 4. Else,
|
|
|
else {
|
|
|
// a. Let possibleInstants be GetNamedTimeZoneEpochNanoseconds(systemTimeZoneIdentifier, ℝ(YearFromTime(t)), ℝ(MonthFromTime(t)) + 1, ℝ(DateFromTime(t)), ℝ(HourFromTime(t)), ℝ(MinFromTime(t)), ℝ(SecFromTime(t)), ℝ(msFromTime(t)), 0, 0).
|
|
|
auto possible_instants = get_named_time_zone_epoch_nanoseconds(system_time_zone_identifier, year_from_time(time), month_from_time(time) + 1, date_from_time(time), hour_from_time(time), min_from_time(time), sec_from_time(time), ms_from_time(time), 0, 0);
|
|
@@ -544,10 +552,10 @@ double utc_time(double time)
|
|
|
offset_nanoseconds = static_cast<double>(offset.offset.to_nanoseconds());
|
|
|
}
|
|
|
|
|
|
- // 4. Let offsetMs be truncate(offsetNs / 10^6).
|
|
|
+ // 5. Let offsetMs be truncate(offsetNs / 10^6).
|
|
|
auto offset_milliseconds = trunc(offset_nanoseconds / 1e6);
|
|
|
|
|
|
- // 5. Return t - 𝔽(offsetMs).
|
|
|
+ // 6. Return t - 𝔽(offsetMs).
|
|
|
return time - offset_milliseconds;
|
|
|
}
|
|
|
|
|
@@ -636,238 +644,96 @@ double time_clip(double time)
|
|
|
return to_integer_or_infinity(time);
|
|
|
}
|
|
|
|
|
|
-// 21.4.1.33 Time Zone Offset String Format, https://tc39.es/ecma262/#sec-time-zone-offset-strings
|
|
|
-Optional<UTCOffset> parse_utc_offset(StringView offset_string)
|
|
|
+// 21.4.1.33.1 IsTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-istimezoneoffsetstring
|
|
|
+// 14.5.10 IsOffsetTimeZoneIdentifier ( offsetString ), https://tc39.es/proposal-temporal/#sec-isoffsettimezoneidentifier
|
|
|
+bool is_offset_time_zone_identifier(StringView offset_string)
|
|
|
{
|
|
|
- GenericLexer lexer { offset_string };
|
|
|
- UTCOffset parse_result;
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-ASCIISign
|
|
|
- auto parse_ascii_sign = [&]() {
|
|
|
- // ASCIISign ::: one of
|
|
|
- // + -
|
|
|
- if (lexer.next_is(is_any_of("+-"sv))) {
|
|
|
- parse_result.sign = lexer.consume();
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- };
|
|
|
-
|
|
|
- auto parse_two_digits = [&](size_t max_value) -> Optional<u8> {
|
|
|
- if (auto digits = lexer.peek_string(2); digits.has_value()) {
|
|
|
- auto number = digits->to_number<u8>(TrimWhitespace::No);
|
|
|
-
|
|
|
- if (number.has_value() && *number <= max_value) {
|
|
|
- lexer.ignore(2);
|
|
|
- return *number;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return {};
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-Hour
|
|
|
- auto parse_hour = [&]() {
|
|
|
- // Hour :::
|
|
|
- // 0 DecimalDigit
|
|
|
- // 1 DecimalDigit
|
|
|
- // 20
|
|
|
- // 21
|
|
|
- // 22
|
|
|
- // 23
|
|
|
- parse_result.hour = parse_two_digits(23);
|
|
|
- return parse_result.hour.has_value();
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-TimeSeparator
|
|
|
- auto parse_time_separator = [&](auto extended) {
|
|
|
- // TimeSeparator[Extended] :::
|
|
|
- // [+Extended] :
|
|
|
- // [~Extended] [empty]
|
|
|
- if (extended)
|
|
|
- return lexer.consume_specific(':');
|
|
|
- return true;
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-MinuteSecond
|
|
|
- auto parse_minute_second = [&](auto& result) {
|
|
|
- // MinuteSecond :::
|
|
|
- // 0 DecimalDigit
|
|
|
- // 1 DecimalDigit
|
|
|
- // 2 DecimalDigit
|
|
|
- // 3 DecimalDigit
|
|
|
- // 4 DecimalDigit
|
|
|
- // 5 DecimalDigit
|
|
|
- result = parse_two_digits(59);
|
|
|
- return result.has_value();
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-TemporalDecimalSeparator
|
|
|
- auto parse_temporal_decimal_separator = [&]() {
|
|
|
- // TemporalDecimalSeparator ::: one of
|
|
|
- // . ,
|
|
|
- return lexer.consume_specific('.') || lexer.consume_specific(',');
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-TemporalDecimalFraction
|
|
|
- auto parse_temporal_decimal_fraction = [&]() {
|
|
|
- // TemporalDecimalFraction :::
|
|
|
- // TemporalDecimalSeparator DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- // TemporalDecimalSeparator DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
|
|
|
- auto position = lexer.tell();
|
|
|
-
|
|
|
- if (!parse_temporal_decimal_separator())
|
|
|
- return false;
|
|
|
-
|
|
|
- for (size_t i = 0; i < 9; ++i) {
|
|
|
- if (!lexer.next_is(is_ascii_digit))
|
|
|
- break;
|
|
|
- lexer.ignore();
|
|
|
- }
|
|
|
-
|
|
|
- if (auto fraction = lexer.input().substring_view(position, lexer.tell() - position); fraction.length() > 1) {
|
|
|
- parse_result.fraction = fraction;
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-HourSubcomponents
|
|
|
- auto parse_hour_subcomponents = [&](auto extended) {
|
|
|
- // HourSubcomponents[Extended] :::
|
|
|
- // TimeSeparator[?Extended] MinuteSecond
|
|
|
- // TimeSeparator[?Extended] MinuteSecond TimeSeparator[?Extended] MinuteSecond TemporalDecimalFraction[opt]
|
|
|
- ArmedScopeGuard guard { [&, position = lexer.tell()]() { lexer.retreat(lexer.tell() - position); } };
|
|
|
-
|
|
|
- if (!parse_time_separator(extended))
|
|
|
- return false;
|
|
|
- if (!parse_minute_second(parse_result.minute))
|
|
|
- return false;
|
|
|
-
|
|
|
- if (lexer.is_eof()) {
|
|
|
- guard.disarm();
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- if (!parse_time_separator(extended))
|
|
|
- return false;
|
|
|
- if (!parse_minute_second(parse_result.second))
|
|
|
- return false;
|
|
|
-
|
|
|
- if (lexer.is_eof()) {
|
|
|
- guard.disarm();
|
|
|
- return true;
|
|
|
- }
|
|
|
+ // 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset[~SubMinutePrecision]).
|
|
|
+ auto parse_result = Temporal::parse_utc_offset(offset_string, Temporal::SubMinutePrecision::No);
|
|
|
|
|
|
- if (!parse_temporal_decimal_fraction())
|
|
|
- return false;
|
|
|
-
|
|
|
- guard.disarm();
|
|
|
- return true;
|
|
|
- };
|
|
|
-
|
|
|
- // https://tc39.es/ecma262/#prod-UTCOffset
|
|
|
- // UTCOffset :::
|
|
|
- // ASCIISign Hour
|
|
|
- // ASCIISign Hour HourSubcomponents[+Extended]
|
|
|
- // ASCIISign Hour HourSubcomponents[~Extended]
|
|
|
- if (!parse_ascii_sign())
|
|
|
- return {};
|
|
|
- if (!parse_hour())
|
|
|
- return {};
|
|
|
+ // 2. If parseResult is a List of errors, return false.
|
|
|
+ // 3. Return true.
|
|
|
+ return parse_result.has_value();
|
|
|
+}
|
|
|
|
|
|
- if (lexer.is_eof())
|
|
|
- return parse_result;
|
|
|
+// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
|
|
|
+// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
|
|
|
+ThrowCompletionOr<double> parse_date_time_utc_offset(VM& vm, StringView offset_string)
|
|
|
+{
|
|
|
+ // 1. Let parseResult be ParseText(offsetString, UTCOffset[+SubMinutePrecision]).
|
|
|
+ auto parse_result = Temporal::parse_utc_offset(offset_string, Temporal::SubMinutePrecision::Yes);
|
|
|
|
|
|
- if (!parse_hour_subcomponents(true) && !parse_hour_subcomponents(false))
|
|
|
- return {};
|
|
|
- if (lexer.is_eof())
|
|
|
- return parse_result;
|
|
|
+ // 2. If parseResult is a List of errors, throw a RangeError exception.
|
|
|
+ if (!parse_result.has_value())
|
|
|
+ return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneString, offset_string);
|
|
|
|
|
|
- return {};
|
|
|
+ return parse_date_time_utc_offset(*parse_result);
|
|
|
}
|
|
|
|
|
|
-// 21.4.1.33.1 IsTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-istimezoneoffsetstring
|
|
|
-bool is_time_zone_offset_string(StringView offset_string)
|
|
|
+// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
|
|
|
+// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
|
|
|
+double parse_date_time_utc_offset(StringView offset_string)
|
|
|
{
|
|
|
- // 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset).
|
|
|
- auto parse_result = parse_utc_offset(offset_string);
|
|
|
+ // OPTIMIZATION: Some callers can assume that parsing will succeed.
|
|
|
|
|
|
- // 2. If parseResult is a List of errors, return false.
|
|
|
- // 3. Return true.
|
|
|
- return parse_result.has_value();
|
|
|
+ // 1. Let parseResult be ParseText(offsetString, UTCOffset[+SubMinutePrecision]).
|
|
|
+ auto parse_result = Temporal::parse_utc_offset(offset_string, Temporal::SubMinutePrecision::Yes);
|
|
|
+ VERIFY(parse_result.has_value());
|
|
|
+
|
|
|
+ return parse_date_time_utc_offset(*parse_result);
|
|
|
}
|
|
|
|
|
|
// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
|
|
|
-double parse_time_zone_offset_string(StringView offset_string)
|
|
|
+// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
|
|
|
+double parse_date_time_utc_offset(Temporal::TimeZoneOffset const& parse_result)
|
|
|
{
|
|
|
- // 1. Let parseResult be ParseText(offsetString, UTCOffset).
|
|
|
- auto parse_result = parse_utc_offset(offset_string);
|
|
|
-
|
|
|
- // 2. Assert: parseResult is not a List of errors.
|
|
|
- VERIFY(parse_result.has_value());
|
|
|
+ // OPTIMIZATION: Some callers will have already parsed and validated the time zone identifier.
|
|
|
|
|
|
// 3. Assert: parseResult contains a ASCIISign Parse Node.
|
|
|
- VERIFY(parse_result->sign.has_value());
|
|
|
+ VERIFY(parse_result.sign.has_value());
|
|
|
|
|
|
// 4. Let parsedSign be the source text matched by the ASCIISign Parse Node contained within parseResult.
|
|
|
- auto parsed_sign = *parse_result->sign;
|
|
|
- i8 sign { 0 };
|
|
|
-
|
|
|
// 5. If parsedSign is the single code point U+002D (HYPHEN-MINUS), then
|
|
|
- if (parsed_sign == '-') {
|
|
|
- // a. Let sign be -1.
|
|
|
- sign = -1;
|
|
|
- }
|
|
|
+ // a. Let sign be -1.
|
|
|
// 6. Else,
|
|
|
- else {
|
|
|
- // a. Let sign be 1.
|
|
|
- sign = 1;
|
|
|
- }
|
|
|
+ // a. Let sign be 1.
|
|
|
+ auto sign = parse_result.sign == '-' ? -1 : 1;
|
|
|
|
|
|
- // 7. NOTE: Applications of StringToNumber below do not lose precision, since each of the parsed values is guaranteed to be a sufficiently short string of decimal digits.
|
|
|
+ // 7. NOTE: Applications of StringToNumber below do not lose precision, since each of the parsed values is guaranteed
|
|
|
+ // to be a sufficiently short string of decimal digits.
|
|
|
|
|
|
// 8. Assert: parseResult contains an Hour Parse Node.
|
|
|
- VERIFY(parse_result->hour.has_value());
|
|
|
+ VERIFY(parse_result.hours.has_value());
|
|
|
|
|
|
// 9. Let parsedHours be the source text matched by the Hour Parse Node contained within parseResult.
|
|
|
// 10. Let hours be ℝ(StringToNumber(CodePointsToString(parsedHours))).
|
|
|
- auto hours = *parse_result->hour;
|
|
|
+ auto hours = parse_result.hours->to_number<u8>().value();
|
|
|
|
|
|
// 11. If parseResult does not contain a MinuteSecond Parse Node, then
|
|
|
// a. Let minutes be 0.
|
|
|
// 12. Else,
|
|
|
// a. Let parsedMinutes be the source text matched by the first MinuteSecond Parse Node contained within parseResult.
|
|
|
// b. Let minutes be ℝ(StringToNumber(CodePointsToString(parsedMinutes))).
|
|
|
- double minutes = parse_result->minute.value_or(0);
|
|
|
+ double minutes = parse_result.minutes.has_value() ? parse_result.minutes->to_number<u8>().value() : 0;
|
|
|
|
|
|
// 13. If parseResult does not contain two MinuteSecond Parse Nodes, then
|
|
|
// a. Let seconds be 0.
|
|
|
// 14. Else,
|
|
|
// a. Let parsedSeconds be the source text matched by the second secondSecond Parse Node contained within parseResult.
|
|
|
// b. Let seconds be ℝ(StringToNumber(CodePointsToString(parsedSeconds))).
|
|
|
- double seconds = parse_result->second.value_or(0);
|
|
|
+ double seconds = parse_result.seconds.has_value() ? parse_result.seconds->to_number<u8>().value() : 0;
|
|
|
|
|
|
double nanoseconds = 0;
|
|
|
|
|
|
// 15. If parseResult does not contain a TemporalDecimalFraction Parse Node, then
|
|
|
- if (!parse_result->fraction.has_value()) {
|
|
|
+ if (!parse_result.fraction.has_value()) {
|
|
|
// a. Let nanoseconds be 0.
|
|
|
nanoseconds = 0;
|
|
|
}
|
|
|
// 16. Else,
|
|
|
else {
|
|
|
// a. Let parsedFraction be the source text matched by the TemporalDecimalFraction Parse Node contained within parseResult.
|
|
|
- auto parsed_fraction = *parse_result->fraction;
|
|
|
+ auto parsed_fraction = *parse_result.fraction;
|
|
|
|
|
|
// b. Let fraction be the string-concatenation of CodePointsToString(parsedFraction) and "000000000".
|
|
|
auto fraction = ByteString::formatted("{}000000000", parsed_fraction);
|