From b26b18a0bcc31ebf1856ff11ba3771cc2e35fb98 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Wed, 2 Nov 2022 19:40:49 +0000 Subject: [PATCH] LibJS: Add timeZoneName: "critical" option to ZonedDateTime.toString() This is a normative change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/d84937f --- .../LibJS/Runtime/Temporal/AbstractOperations.cpp | 4 ++-- .../Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp | 7 +++++-- .../ZonedDateTime/ZonedDateTime.prototype.toString.js | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index f7102fcd63d..1e6bcb58a88 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -256,8 +256,8 @@ ThrowCompletionOr to_show_calendar_option(VM& vm, Object const& normaliz // 13.10 ToShowTimeZoneNameOption ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-toshowtimezonenameoption ThrowCompletionOr to_show_time_zone_name_option(VM& vm, Object const& normalized_options) { - // 1. Return ? GetOption(normalizedOptions, "timeZoneName", "string, « "auto", "never" », "auto"). - auto option = TRY(get_option(vm, normalized_options, vm.names.timeZoneName, OptionType::String, { "auto"sv, "never"sv }, "auto"sv)); + // 1. Return ? GetOption(normalizedOptions, "timeZoneName", "string", « "auto", "never", "critical" », "auto"). + auto option = TRY(get_option(vm, normalized_options, vm.names.timeZoneName, OptionType::String, { "auto"sv, "never"sv, "critical"sv }, "auto"sv)); VERIFY(option.is_string()); return option.as_string().string(); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index 510eadd1e6b..5957ea37816 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -343,8 +343,11 @@ ThrowCompletionOr temporal_zoned_date_time_to_string(VM& vm, ZonedDateTi // a. Let timeZoneID be ? ToString(timeZone). auto time_zone_id = TRY(Value(&time_zone).to_string(vm)); - // b. Let timeZoneString be the string-concatenation of the code unit 0x005B (LEFT SQUARE BRACKET), timeZoneID, and the code unit 0x005D (RIGHT SQUARE BRACKET). - time_zone_string = String::formatted("[{}]", time_zone_id); + // b. If showTimeZone is "critical", let flag be "!"; else let flag be the empty String. + auto flag = show_time_zone == "critical"sv ? "!"sv : ""sv; + + // c. Let timeZoneString be the string-concatenation of the code unit 0x005B (LEFT SQUARE BRACKET), flag, timeZoneID, and the code unit 0x005D (RIGHT SQUARE BRACKET). + time_zone_string = String::formatted("[{}{}]", flag, time_zone_id); } // 14. Let calendarString be ? MaybeFormatCalendarAnnotation(zonedDateTime.[[Calendar]], showCalendar). diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js index 39fe61082b8..21ea09b4418 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js @@ -72,6 +72,7 @@ describe("correct behavior", () => { const values = [ ["auto", "2021-11-03T01:33:05.1002003+00:00[UTC]"], ["never", "2021-11-03T01:33:05.1002003+00:00"], + ["critical", "2021-11-03T01:33:05.1002003+00:00[!UTC]"], ]; for (const [timeZoneName, expected] of values) {