diff --git a/AK/NumberFormat.cpp b/AK/NumberFormat.cpp index 2027b22bf38c..85d4ae208a2d 100644 --- a/AK/NumberFormat.cpp +++ b/AK/NumberFormat.cpp @@ -73,16 +73,20 @@ String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_se return MUST(String::formatted("{} ({} bytes)", human_readable_size_string, size)); } -String human_readable_time(i64 time_in_seconds) +String human_readable_time(Duration duration) { - auto days = time_in_seconds / 86400; - time_in_seconds = time_in_seconds % 86400; + auto milliseconds = duration.to_milliseconds(); - auto hours = time_in_seconds / 3600; - time_in_seconds = time_in_seconds % 3600; + auto days = milliseconds / 86400000; + milliseconds = milliseconds % 86400000; - auto minutes = time_in_seconds / 60; - time_in_seconds = time_in_seconds % 60; + auto hours = milliseconds / 3600000; + milliseconds = milliseconds % 3600000; + + auto minutes = milliseconds / 60000; + milliseconds = milliseconds % 60000; + + auto seconds = static_cast(milliseconds) / 1000.0; StringBuilder builder; @@ -95,7 +99,7 @@ String human_readable_time(i64 time_in_seconds) if (minutes > 0) builder.appendff("{} minute{} ", minutes, minutes == 1 ? "" : "s"); - builder.appendff("{} second{}", time_in_seconds, time_in_seconds == 1 ? "" : "s"); + builder.appendff("{:.3} second{}", seconds, seconds == 1.0 ? "" : "s"); return MUST(builder.to_string()); } diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index ba99d0caf7c8..c2809e7eeb4d 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -7,6 +7,7 @@ #pragma once #include +#include namespace AK { @@ -24,7 +25,7 @@ String human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadab String human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No); String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No); -String human_readable_time(i64 time_in_seconds); +String human_readable_time(Duration); String human_readable_digital_time(i64 time_in_seconds); } diff --git a/Libraries/LibJS/Console.cpp b/Libraries/LibJS/Console.cpp index 5c93af23b92d..6b81804d8cec 100644 --- a/Libraries/LibJS/Console.cpp +++ b/Libraries/LibJS/Console.cpp @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include namespace JS { @@ -604,7 +604,7 @@ ThrowCompletionOr Console::time_log() auto start_time = maybe_start_time->value; // 3. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format. - auto duration = TRY(format_time_since(start_time)); + auto duration = AK::human_readable_time(start_time.elapsed_time()); // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration)); @@ -653,7 +653,7 @@ ThrowCompletionOr Console::time_end() m_timer_table.remove(label); // 4. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format. - auto duration = TRY(format_time_since(start_time)); + auto duration = AK::human_readable_time(start_time.elapsed_time()); // 5. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration)); @@ -724,35 +724,6 @@ ThrowCompletionOr Console::value_vector_to_string(GC::MarkedVector Console::format_time_since(Core::ElapsedTimer timer) -{ - auto& vm = realm().vm(); - - auto elapsed_ms = timer.elapsed_time().to_milliseconds(); - auto duration = TRY(Temporal::balance_duration(vm, 0, 0, 0, 0, elapsed_ms, 0, "0"_sbigint, "year"sv)); - - auto append = [&](auto& builder, auto format, auto number) { - if (!builder.is_empty()) - builder.append(' '); - builder.appendff(format, number); - }; - - StringBuilder builder; - - if (duration.days > 0) - append(builder, "{:.0} day(s)"sv, duration.days); - if (duration.hours > 0) - append(builder, "{:.0} hour(s)"sv, duration.hours); - if (duration.minutes > 0) - append(builder, "{:.0} minute(s)"sv, duration.minutes); - if (duration.seconds > 0 || duration.milliseconds > 0) { - double combined_seconds = duration.seconds + (0.001 * duration.milliseconds); - append(builder, "{:.3} seconds"sv, combined_seconds); - } - - return MUST(builder.to_string()); -} - ConsoleClient::ConsoleClient(Console& console) : m_console(console) { diff --git a/Libraries/LibJS/Console.h b/Libraries/LibJS/Console.h index a0a6c4ad6dae..5791aebb185d 100644 --- a/Libraries/LibJS/Console.h +++ b/Libraries/LibJS/Console.h @@ -96,7 +96,6 @@ class Console : public Cell { virtual void visit_edges(Visitor&) override; ThrowCompletionOr value_vector_to_string(GC::MarkedVector const&); - ThrowCompletionOr format_time_since(Core::ElapsedTimer timer); GC::Ref m_realm; GC::Ptr m_client; diff --git a/Libraries/LibJS/Runtime/Date.cpp b/Libraries/LibJS/Runtime/Date.cpp index b5d9c42e458d..8a2a45d359bc 100644 --- a/Libraries/LibJS/Runtime/Date.cpp +++ b/Libraries/LibJS/Runtime/Date.cpp @@ -5,14 +5,16 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include +#include #include #include #include #include #include #include -#include #include namespace JS { @@ -634,11 +636,171 @@ 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 parse_utc_offset(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 { + if (auto digits = lexer.peek_string(2); digits.has_value()) { + auto number = digits->to_number(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; + } + + 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 {}; + + if (lexer.is_eof()) + return parse_result; + + if (!parse_hour_subcomponents(true) && !parse_hour_subcomponents(false)) + return {}; + if (lexer.is_eof()) + return parse_result; + + return {}; +} + // 21.4.1.33.1 IsTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-istimezoneoffsetstring bool is_time_zone_offset_string(StringView offset_string) { // 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset). - auto parse_result = Temporal::parse_iso8601(Temporal::Production::TimeZoneNumericUTCOffset, offset_string); + auto parse_result = parse_utc_offset(offset_string); // 2. If parseResult is a List of errors, return false. // 3. Return true. @@ -649,20 +811,20 @@ bool is_time_zone_offset_string(StringView offset_string) double parse_time_zone_offset_string(StringView offset_string) { // 1. Let parseResult be ParseText(offsetString, UTCOffset). - auto parse_result = Temporal::parse_iso8601(Temporal::Production::TimeZoneNumericUTCOffset, offset_string); + auto parse_result = parse_utc_offset(offset_string); // 2. Assert: parseResult is not a List of errors. VERIFY(parse_result.has_value()); // 3. Assert: parseResult contains a ASCIISign Parse Node. - VERIFY(parse_result->time_zone_utc_offset_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->time_zone_utc_offset_sign; + 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 == "-"sv) { + if (parsed_sign == '-') { // a. Let sign be -1. sign = -1; } @@ -675,55 +837,37 @@ double parse_time_zone_offset_string(StringView offset_string) // 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->time_zone_utc_offset_hour.has_value()); + VERIFY(parse_result->hour.has_value()); // 9. Let parsedHours be the source text matched by the Hour Parse Node contained within parseResult. - auto parsed_hours = *parse_result->time_zone_utc_offset_hour; - // 10. Let hours be ℝ(StringToNumber(CodePointsToString(parsedHours))). - auto hours = string_to_number(parsed_hours); - - double minutes { 0 }; - double seconds { 0 }; - double nanoseconds { 0 }; + auto hours = *parse_result->hour; // 11. If parseResult does not contain a MinuteSecond Parse Node, then - if (!parse_result->time_zone_utc_offset_minute.has_value()) { - // a. Let minutes be 0. - minutes = 0; - } + // a. Let minutes be 0. // 12. Else, - else { - // a. Let parsedMinutes be the source text matched by the first MinuteSecond Parse Node contained within parseResult. - auto parsed_minutes = *parse_result->time_zone_utc_offset_minute; - - // b. Let minutes be ℝ(StringToNumber(CodePointsToString(parsedMinutes))). - minutes = string_to_number(parsed_minutes); - } + // 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); // 13. If parseResult does not contain two MinuteSecond Parse Nodes, then - if (!parse_result->time_zone_utc_offset_second.has_value()) { - // a. Let seconds be 0. - seconds = 0; - } + // a. Let seconds be 0. // 14. Else, - else { - // a. Let parsedSeconds be the source text matched by the second secondSecond Parse Node contained within parseResult. - auto parsed_seconds = *parse_result->time_zone_utc_offset_second; + // 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); - // b. Let seconds be ℝ(StringToNumber(CodePointsToString(parsedSeconds))). - seconds = string_to_number(parsed_seconds); - } + double nanoseconds = 0; // 15. If parseResult does not contain a TemporalDecimalFraction Parse Node, then - if (!parse_result->time_zone_utc_offset_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->time_zone_utc_offset_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); diff --git a/Libraries/LibJS/Runtime/Date.h b/Libraries/LibJS/Runtime/Date.h index 17da2fdd9f8a..91036948e835 100644 --- a/Libraries/LibJS/Runtime/Date.h +++ b/Libraries/LibJS/Runtime/Date.h @@ -58,6 +58,14 @@ constexpr inline double ms_per_day = 86'400'000; constexpr inline double ns_per_day = 86'400'000'000'000; extern Crypto::SignedBigInteger const ns_per_day_bigint; +struct UTCOffset { + Optional sign; + Optional hour; + Optional minute; + Optional second; + Optional fraction; +}; + double day(double); double time_within_day(double); u16 days_in_year(i32); @@ -85,6 +93,7 @@ double make_time(double hour, double min, double sec, double ms); double make_day(double year, double month, double date); double make_date(double day, double time); double time_clip(double time); +Optional parse_utc_offset(StringView); bool is_time_zone_offset_string(StringView offset_string); double parse_time_zone_offset_string(StringView offset_string); diff --git a/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp b/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp index fb898df54e7b..64a9187ae6f9 100644 --- a/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp +++ b/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -205,13 +204,13 @@ ThrowCompletionOr> create_date_time_format(VM& vm, Funct if (is_time_zone_offset_string) { // a. Let parseResult be ParseText(StringToCodePoints(timeZone), UTCOffset). - auto parse_result = Temporal::parse_iso8601(Temporal::Production::TimeZoneNumericUTCOffset, time_zone); + auto parse_result = parse_utc_offset(time_zone); // b. Assert: parseResult is a Parse Node. VERIFY(parse_result.has_value()); // c. If parseResult contains more than one MinuteSecond Parse Node, throw a RangeError exception. - if (parse_result->time_zone_utc_offset_second.has_value()) + if (parse_result->second.has_value()) return vm.throw_completion(ErrorType::OptionIsNotValidValue, time_zone, vm.names.timeZone); // d. Let offsetNanoseconds be ParseTimeZoneOffsetString(timeZone). diff --git a/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp b/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp index 6b9884de230a..185edf2d42e6 100644 --- a/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp +++ b/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include namespace JS::Intl { @@ -157,7 +158,7 @@ static GC::Ref construct_list_format(VM& vm, DurationFormat const& d } // 1.1.3 ToDurationRecord ( input ), https://tc39.es/proposal-intl-duration-format/#sec-todurationrecord -ThrowCompletionOr to_duration_record(VM& vm, Value input) +ThrowCompletionOr to_duration_record(VM& vm, Value input) { // 1. If Type(input) is not Object, then if (!input.is_object()) { @@ -172,7 +173,7 @@ ThrowCompletionOr to_duration_record(VM& vm, Value inp auto& input_object = input.as_object(); // 2. Let result be a new Duration Record with each field set to 0. - Temporal::DurationRecord result = {}; + DurationRecord result = {}; bool any_defined = false; auto set_duration_record_value = [&](auto const& name, auto& value_slot) -> ThrowCompletionOr { @@ -240,6 +241,24 @@ ThrowCompletionOr to_duration_record(VM& vm, Value inp return result; } +// 1.1.4 DurationSign ( duration ), https://tc39.es/proposal-intl-duration-format/#sec-durationsign +i8 duration_sign(DurationRecord const& duration) +{ + // 1. For each value v of « duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]] », do + for (auto value : { duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds }) { + // a. If v < 0, return -1. + if (value < 0) + return -1; + + // b. If v > 0, return 1. + if (value > 0) + return 1; + } + + // 2. Return 0. + return 0; +} + // 1.1.6 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle, twoDigitHours ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions ThrowCompletionOr get_duration_unit_options(VM& vm, String const& unit, Object const& options, StringView base_style, ReadonlySpan styles_list, StringView digital_base, StringView previous_style, bool two_digit_hours) { @@ -349,7 +368,7 @@ ThrowCompletionOr get_duration_unit_options(VM& vm, String } // 1.1.7 AddFractionalDigits ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-addfractionaldigits -double add_fractional_digits(DurationFormat const& duration_format, Temporal::DurationRecord const& duration) +double add_fractional_digits(DurationFormat const& duration_format, DurationRecord const& duration) { // 1. Let result be 0. double result = 0; @@ -625,7 +644,7 @@ Vector format_numeric_seconds(VM& vm, DurationFormat const& } // 1.1.12 FormatNumericUnits ( durationFormat, duration, firstNumericUnit, signDisplayed ), https://tc39.es/proposal-intl-duration-format/#sec-formatnumericunits -Vector format_numeric_units(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration, StringView first_numeric_unit, bool sign_displayed) +Vector format_numeric_units(VM& vm, DurationFormat const& duration_format, DurationRecord const& duration, StringView first_numeric_unit, bool sign_displayed) { // 1. Assert: firstNumericUnit is "hours", "minutes", or "seconds". VERIFY(first_numeric_unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv)); @@ -696,8 +715,8 @@ Vector format_numeric_units(VM& vm, DurationFormat const& du if (hours_formatted) { // a. If signDisplayed is true, then if (sign_displayed) { - // i. If hoursValue is 0 and DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is -1, then - if (hours_value == 0 && Temporal::duration_sign(duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds) == -1) { + // i. If hoursValue is 0 and DurationSign(duration) is -1, then + if (hours_value == 0 && duration_sign(duration) == -1) { // 1. Set hoursValue to negative-zero. hours_value = -0.0; } @@ -714,8 +733,8 @@ Vector format_numeric_units(VM& vm, DurationFormat const& du if (minutes_formatted) { // a. If signDisplayed is true, then if (sign_displayed) { - // i. If minutesValue is 0 and DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is -1, then - if (minutes_value == 0 && Temporal::duration_sign(duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds) == -1) { + // i. If minutesValue is 0 and DurationSign(duration) is -1, then + if (minutes_value == 0 && duration_sign(duration) == -1) { // 1. Set minutesValue to negative-zero. minutes_value = -0.0; } @@ -833,7 +852,7 @@ Vector list_format_parts(VM& vm, DurationFormat const& durat } // 1.1.7 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern -Vector partition_duration_format_pattern(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration) +Vector partition_duration_format_pattern(VM& vm, DurationFormat const& duration_format, DurationRecord const& duration) { auto& realm = *vm.current_realm(); @@ -930,8 +949,8 @@ Vector partition_duration_format_pattern(VM& vm, DurationFor // a. Set signDisplayed to false. sign_displayed = false; - // b. If value is 0 and DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is -1, then - if (value == 0 && Temporal::duration_sign(duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds) == -1) { + // b. If value is 0 and DurationSign(duration) is -1, then + if (value == 0 && duration_sign(duration) == -1) { // i. Set value to negative-zero. value = -0.0; } diff --git a/Libraries/LibJS/Runtime/Intl/DurationFormat.h b/Libraries/LibJS/Runtime/Intl/DurationFormat.h index cbaaae1db44d..8dbfe4f47a9d 100644 --- a/Libraries/LibJS/Runtime/Intl/DurationFormat.h +++ b/Libraries/LibJS/Runtime/Intl/DurationFormat.h @@ -11,7 +11,6 @@ #include #include #include -#include #include namespace JS::Intl { @@ -199,8 +198,22 @@ class DurationFormat final : public Object { Optional m_fractional_digits; // [[FractionalDigits]] }; +// 1.1.1 Duration Records, https://tc39.es/proposal-intl-duration-format/#sec-duration-records +struct DurationRecord { + double years { 0 }; + double months { 0 }; + double weeks { 0 }; + double days { 0 }; + double hours { 0 }; + double minutes { 0 }; + double seconds { 0 }; + double milliseconds { 0 }; + double microseconds { 0 }; + double nanoseconds { 0 }; +}; + struct DurationInstanceComponent { - double Temporal::DurationRecord::*value_slot; + double DurationRecord::*value_slot; DurationFormat::ValueStyle (DurationFormat::*get_style_slot)() const; void (DurationFormat::*set_style_slot)(StringView); DurationFormat::Display (DurationFormat::*get_display_slot)() const; @@ -216,16 +229,16 @@ static constexpr AK::Array date_values = { "long"sv, "short"sv, " static constexpr AK::Array time_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv, "2-digit"sv }; static constexpr AK::Array sub_second_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv }; static constexpr AK::Array duration_instances_components { - DurationInstanceComponent { &Temporal::DurationRecord::years, &DurationFormat::years_style, &DurationFormat::set_years_style, &DurationFormat::years_display, &DurationFormat::set_years_display, "years"sv, "year"sv, date_values, "short"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::months, &DurationFormat::months_style, &DurationFormat::set_months_style, &DurationFormat::months_display, &DurationFormat::set_months_display, "months"sv, "month"sv, date_values, "short"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::weeks, &DurationFormat::weeks_style, &DurationFormat::set_weeks_style, &DurationFormat::weeks_display, &DurationFormat::set_weeks_display, "weeks"sv, "week"sv, date_values, "short"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::days, &DurationFormat::days_style, &DurationFormat::set_days_style, &DurationFormat::days_display, &DurationFormat::set_days_display, "days"sv, "day"sv, date_values, "short"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::hours, &DurationFormat::hours_style, &DurationFormat::set_hours_style, &DurationFormat::hours_display, &DurationFormat::set_hours_display, "hours"sv, "hour"sv, time_values, "numeric"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::minutes, &DurationFormat::minutes_style, &DurationFormat::set_minutes_style, &DurationFormat::minutes_display, &DurationFormat::set_minutes_display, "minutes"sv, "minute"sv, time_values, "numeric"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::seconds, &DurationFormat::seconds_style, &DurationFormat::set_seconds_style, &DurationFormat::seconds_display, &DurationFormat::set_seconds_display, "seconds"sv, "second"sv, time_values, "numeric"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::milliseconds, &DurationFormat::milliseconds_style, &DurationFormat::set_milliseconds_style, &DurationFormat::milliseconds_display, &DurationFormat::set_milliseconds_display, "milliseconds"sv, "millisecond"sv, sub_second_values, "numeric"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::microseconds, &DurationFormat::microseconds_style, &DurationFormat::set_microseconds_style, &DurationFormat::microseconds_display, &DurationFormat::set_microseconds_display, "microseconds"sv, "microsecond"sv, sub_second_values, "numeric"sv }, - DurationInstanceComponent { &Temporal::DurationRecord::nanoseconds, &DurationFormat::nanoseconds_style, &DurationFormat::set_nanoseconds_style, &DurationFormat::nanoseconds_display, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, "nanosecond"sv, sub_second_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::years, &DurationFormat::years_style, &DurationFormat::set_years_style, &DurationFormat::years_display, &DurationFormat::set_years_display, "years"sv, "year"sv, date_values, "short"sv }, + DurationInstanceComponent { &DurationRecord::months, &DurationFormat::months_style, &DurationFormat::set_months_style, &DurationFormat::months_display, &DurationFormat::set_months_display, "months"sv, "month"sv, date_values, "short"sv }, + DurationInstanceComponent { &DurationRecord::weeks, &DurationFormat::weeks_style, &DurationFormat::set_weeks_style, &DurationFormat::weeks_display, &DurationFormat::set_weeks_display, "weeks"sv, "week"sv, date_values, "short"sv }, + DurationInstanceComponent { &DurationRecord::days, &DurationFormat::days_style, &DurationFormat::set_days_style, &DurationFormat::days_display, &DurationFormat::set_days_display, "days"sv, "day"sv, date_values, "short"sv }, + DurationInstanceComponent { &DurationRecord::hours, &DurationFormat::hours_style, &DurationFormat::set_hours_style, &DurationFormat::hours_display, &DurationFormat::set_hours_display, "hours"sv, "hour"sv, time_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::minutes, &DurationFormat::minutes_style, &DurationFormat::set_minutes_style, &DurationFormat::minutes_display, &DurationFormat::set_minutes_display, "minutes"sv, "minute"sv, time_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::seconds, &DurationFormat::seconds_style, &DurationFormat::set_seconds_style, &DurationFormat::seconds_display, &DurationFormat::set_seconds_display, "seconds"sv, "second"sv, time_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::milliseconds, &DurationFormat::milliseconds_style, &DurationFormat::set_milliseconds_style, &DurationFormat::milliseconds_display, &DurationFormat::set_milliseconds_display, "milliseconds"sv, "millisecond"sv, sub_second_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::microseconds, &DurationFormat::microseconds_style, &DurationFormat::set_microseconds_style, &DurationFormat::microseconds_display, &DurationFormat::set_microseconds_display, "microseconds"sv, "microsecond"sv, sub_second_values, "numeric"sv }, + DurationInstanceComponent { &DurationRecord::nanoseconds, &DurationFormat::nanoseconds_style, &DurationFormat::set_nanoseconds_style, &DurationFormat::nanoseconds_display, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, "nanosecond"sv, sub_second_values, "numeric"sv }, }; struct DurationUnitOptions { @@ -239,15 +252,16 @@ struct DurationFormatPart { StringView unit; }; -ThrowCompletionOr to_duration_record(VM&, Value input); +ThrowCompletionOr to_duration_record(VM&, Value input); +i8 duration_sign(DurationRecord const&); ThrowCompletionOr get_duration_unit_options(VM&, String const& unit, Object const& options, StringView base_style, ReadonlySpan styles_list, StringView digital_base, StringView previous_style, bool two_digit_hours); -double add_fractional_digits(DurationFormat const&, Temporal::DurationRecord const&); +double add_fractional_digits(DurationFormat const&, DurationRecord const&); bool next_unit_fractional(DurationFormat const&, StringView unit); Vector format_numeric_hours(VM&, DurationFormat const&, double hours_value, bool sign_displayed); Vector format_numeric_minutes(VM&, DurationFormat const&, double minutes_value, bool hours_displayed, bool sign_displayed); Vector format_numeric_seconds(VM&, DurationFormat const&, double seconds_value, bool minutes_displayed, bool sign_displayed); -Vector format_numeric_units(VM&, DurationFormat const&, Temporal::DurationRecord const&, StringView first_numeric_unit, bool sign_displayed); +Vector format_numeric_units(VM&, DurationFormat const&, DurationRecord const&, StringView first_numeric_unit, bool sign_displayed); Vector list_format_parts(VM&, DurationFormat const&, Vector>& partitioned_parts_list); -Vector partition_duration_format_pattern(VM&, DurationFormat const&, Temporal::DurationRecord const&); +Vector partition_duration_format_pattern(VM&, DurationFormat const&, DurationRecord const&); } diff --git a/Libraries/LibJS/Runtime/Intl/Intl.cpp b/Libraries/LibJS/Runtime/Intl/Intl.cpp index a753ebbeeaeb..849dd0963d48 100644 --- a/Libraries/LibJS/Runtime/Intl/Intl.cpp +++ b/Libraries/LibJS/Runtime/Intl/Intl.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include diff --git a/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js b/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js index d4238d3e8972..3fb6b96ab727 100644 --- a/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js +++ b/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js @@ -128,8 +128,8 @@ describe("correct behavior", () => { expect(console.debug.toString()).toBe("function debug() { [native code] }"); expect(Function.toString()).toBe("function Function() { [native code] }"); expect( - Object.getOwnPropertyDescriptor(Temporal.TimeZone.prototype, "id").get.toString() - ).toBe("function get id() { [native code] }"); + Object.getOwnPropertyDescriptor(Intl.NumberFormat.prototype, "format").get.toString() + ).toBe("function get format() { [native code] }"); const values = [ // Callable Proxy diff --git a/Libraries/LibJS/Tests/test-common.js b/Libraries/LibJS/Tests/test-common.js index 18d56572e281..e020dee48b43 100644 --- a/Libraries/LibJS/Tests/test-common.js +++ b/Libraries/LibJS/Tests/test-common.js @@ -599,20 +599,20 @@ class ExpectationError extends Error { return; } - const now = () => Temporal.Now.instant().epochNanoseconds; - const start = now(); - const time_us = () => Number(BigInt.asIntN(53, (now() - start) / 1000n)); + const start = Date.now(); + const time_ms = () => Date.now() - start; + try { callback(); suite[message] = { result: "pass", - duration: time_us(), + duration: time_ms(), }; } catch (e) { suite[message] = { result: "fail", details: String(e), - duration: time_us(), + duration: time_ms(), }; } }; @@ -652,20 +652,20 @@ class ExpectationError extends Error { return; } - const now = () => Temporal.Now.instant().epochNanoseconds; - const start = now(); - const time_us = () => Number(BigInt.asIntN(53, (now() - start) / 1000n)); + const start = Date.now(); + const time_ms = () => Date.now() - start; + try { callback(); suite[message] = { result: "fail", details: "Expected test to fail, but it passed", - duration: time_us(), + duration: time_ms(), }; } catch (e) { suite[message] = { result: "xfail", - duration: time_us(), + duration: time_ms(), }; } }; @@ -677,21 +677,21 @@ class ExpectationError extends Error { withinSameSecond = callback => { let callbackDuration; for (let tries = 0; tries < 5; tries++) { - const start = Temporal.Now.instant(); + const start = Date.now(); const result = callback(); - const end = Temporal.Now.instant(); - if (start.epochSeconds !== end.epochSeconds) { - callbackDuration = start.until(end); + const end = Date.now(); + + if (start / 1000 != end / 1000) { + callbackDuration = end - start; continue; } + return result; } throw new ExpectationError( `Tried to execute callback '${callback}' 5 times within the same second but ` + `failed. Make sure the callback does as little work as possible (the last run ` + - `took ${callbackDuration.total( - "milliseconds" - )} ms) and the machine is not overloaded. If you see this ` + + `took ${callbackDuration}ms) and the machine is not overloaded. If you see this ` + `error appearing in the CI it is most likely a flaky failure!` ); };