From 814f13bc2a44a5580b79f8e3dee634cdb115b592 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 5 Jul 2022 16:20:36 -0400 Subject: [PATCH] LibJS: Implement Intl.Locale.prototype.timeZones property --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/Intl/Locale.cpp | 22 ++++++++++++ .../Libraries/LibJS/Runtime/Intl/Locale.h | 1 + .../LibJS/Runtime/Intl/LocalePrototype.cpp | 19 ++++++++++ .../LibJS/Runtime/Intl/LocalePrototype.h | 1 + .../Intl/Locale/Locale.prototype.timeZones.js | 35 +++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.timeZones.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 0fb65be1a3..6375ca2b43 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -476,6 +476,7 @@ namespace JS { P(timeStyle) \ P(timeZone) \ P(timeZoneName) \ + P(timeZones) \ P(toDateString) \ P(toExponential) \ P(toFixed) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp index 7b032afc46..42ce61f154 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp @@ -4,9 +4,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include +#include #include namespace JS::Intl { @@ -145,4 +147,24 @@ Array* numbering_systems_of_locale(GlobalObject& global_object, Locale const& lo return create_array_from_list_or_restricted(global_object, move(list), move(restricted)); } +// 1.1.6 TimeZonesOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-time-zones-of-locale +// NOTE: Our implementation takes a region rather than a Locale object to avoid needlessly parsing the locale twice. +Array* time_zones_of_locale(GlobalObject& global_object, StringView region) +{ + auto& vm = global_object.vm(); + + // 1. Let locale be loc.[[Locale]]. + // 2. Assert: locale matches the unicode_locale_id production. + // 3. Let region be the substring of locale corresponding to the unicode_region_subtag production of the unicode_language_id. + + // 4. Let list be a List of unique canonical time zone identifiers, which must be String values indicating a canonical Zone name of the IANA Time Zone Database, ordered as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn, of those in common use in region. If no time zones are commonly used in region, let list be a new empty List. + auto list = TimeZone::time_zones_in_region(region); + quick_sort(list); + + // 5. Return ! CreateArrayFromList( list ). + return Array::create_from(global_object, list, [&vm](auto value) { + return js_string(vm, value); + }); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h index 6db49aa405..88f148fa0f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h @@ -78,5 +78,6 @@ Array* calendars_of_locale(GlobalObject& global_object, Locale const& locale); Array* collations_of_locale(GlobalObject& global_object, Locale const& locale); Array* hour_cycles_of_locale(GlobalObject& global_object, Locale const& locale); Array* numbering_systems_of_locale(GlobalObject& global_object, Locale const& locale); +Array* time_zones_of_locale(GlobalObject& global_object, StringView region); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 768595b67c..6631e761e4 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -47,6 +47,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.language, language, {}, Attribute::Configurable); define_native_accessor(vm.names.script, script, {}, Attribute::Configurable); define_native_accessor(vm.names.region, region, {}, Attribute::Configurable); + define_native_accessor(vm.names.timeZones, time_zones, {}, Attribute::Configurable); } // 14.3.3 Intl.Locale.prototype.maximize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.maximize @@ -223,4 +224,22 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::region) JS_ENUMERATE_LOCALE_INFO_PROPERTIES #undef __JS_ENUMERATE +// 1.4.20 get Intl.Locale.prototype.timeZones, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.timeZones +JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::time_zones) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = TRY(typed_this_object(global_object)); + + // 3. Let locale be loc.[[Locale]]. + auto locale = Unicode::parse_unicode_locale_id(locale_object->locale()); + + // 4. If the unicode_language_id production of locale does not contain the ["-" unicode_region_subtag] sequence, return undefined. + if (!locale.has_value() || !locale->language_id.region.has_value()) + return js_undefined(); + + // 5. Return ! TimeZonesOfLocale(loc). + return time_zones_of_locale(global_object, locale->language_id.region.value()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 5e3af11f97..20256b760d 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -38,6 +38,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(language); JS_DECLARE_NATIVE_FUNCTION(script); JS_DECLARE_NATIVE_FUNCTION(region); + JS_DECLARE_NATIVE_FUNCTION(time_zones); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.timeZones.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.timeZones.js new file mode 100644 index 0000000000..ac4846645f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.timeZones.js @@ -0,0 +1,35 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.timeZones; + }).toThrowWithMessage(TypeError, "Not an object of type Intl.Locale"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").timeZones).toBeUndefined(); + expect(new Intl.Locale("ar-Latn").timeZones).toBeUndefined(); + + const adZones = new Intl.Locale("en-AD").timeZones; + expect(Array.isArray(adZones)).toBeTrue(); + expect(adZones).toEqual(["Europe/Andorra"]); + + const esZones = new Intl.Locale("en-ES").timeZones; + expect(Array.isArray(esZones)).toBeTrue(); + expect(esZones).toEqual(["Africa/Ceuta", "Atlantic/Canary", "Europe/Madrid"]); + }); + + test("zone list is sorted", () => { + const zones = new Intl.Locale("en-US").timeZones; + const sortedZones = zones.toSorted(); + + expect(zones).toEqual(sortedZones); + }); + + test("invalid region produces empty list", () => { + const zones = new Intl.Locale("en-ZZ").timeZones; + expect(Array.isArray(zones)).toBeTrue(); + expect(zones).toEqual([]); + }); +});