浏览代码

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
Luke Wilde 2 年之前
父节点
当前提交
b26b18a0bc

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -256,8 +256,8 @@ ThrowCompletionOr<String> to_show_calendar_option(VM& vm, Object const& normaliz
 // 13.10 ToShowTimeZoneNameOption ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-toshowtimezonenameoption
 ThrowCompletionOr<String> 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();

+ 5 - 2
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp

@@ -343,8 +343,11 @@ ThrowCompletionOr<String> 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).

+ 1 - 0
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) {