mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibJS+LibUnicode: Port retrieving available regional time zones to ICU
This commit is contained in:
parent
4fc0fba646
commit
1b2d47e6bb
Notes:
sideshowbarker
2024-07-17 09:56:35 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/1b2d47e6bb Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/281
@ -49,6 +49,18 @@ TEST_CASE(available_time_zones)
|
|||||||
EXPECT(!time_zones.contains_slow("EAT"sv));
|
EXPECT(!time_zones.contains_slow("EAT"sv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(available_time_zones_in_region)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
auto time_zones = Unicode::available_time_zones_in_region("AD"sv);
|
||||||
|
EXPECT_EQ(time_zones, to_array({ "Europe/Andorra"_string }));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto time_zones = Unicode::available_time_zones_in_region("ES"sv);
|
||||||
|
EXPECT_EQ(time_zones, to_array({ "Africa/Ceuta"_string, "Atlantic/Canary"_string, "Europe/Madrid"_string }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE(resolve_primary_time_zone)
|
TEST_CASE(resolve_primary_time_zone)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(Unicode::resolve_primary_time_zone("UTC"sv), "Etc/UTC"sv);
|
EXPECT_EQ(Unicode::resolve_primary_time_zone("UTC"sv), "Etc/UTC"sv);
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
#include <LibJS/Runtime/Array.h>
|
#include <LibJS/Runtime/Array.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Intl/Locale.h>
|
#include <LibJS/Runtime/Intl/Locale.h>
|
||||||
#include <LibTimeZone/TimeZone.h>
|
|
||||||
#include <LibUnicode/DateTimeFormat.h>
|
#include <LibUnicode/DateTimeFormat.h>
|
||||||
#include <LibUnicode/Locale.h>
|
#include <LibUnicode/Locale.h>
|
||||||
|
#include <LibUnicode/TimeZone.h>
|
||||||
#include <LibUnicode/UnicodeKeywords.h>
|
#include <LibUnicode/UnicodeKeywords.h>
|
||||||
|
|
||||||
namespace JS::Intl {
|
namespace JS::Intl {
|
||||||
@ -142,12 +142,11 @@ NonnullGCPtr<Array> time_zones_of_locale(VM& vm, StringView region)
|
|||||||
// 3. Let region be the substring of locale corresponding to the unicode_region_subtag production of the unicode_language_id.
|
// 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.
|
// 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);
|
auto list = Unicode::available_time_zones_in_region(region);
|
||||||
quick_sort(list);
|
|
||||||
|
|
||||||
// 5. Return ! CreateArrayFromList( list ).
|
// 5. Return ! CreateArrayFromList( list ).
|
||||||
return Array::create_from<StringView>(realm, list, [&vm](auto value) {
|
return Array::create_from<String>(realm, list, [&vm](auto value) {
|
||||||
return PrimitiveString::create(vm, String::from_utf8(value).release_value());
|
return PrimitiveString::create(vm, value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,12 +76,13 @@ static constexpr bool is_legacy_non_iana_time_zone(StringView time_zone)
|
|||||||
return legacy_zones.contains_slow(time_zone);
|
return legacy_zones.contains_slow(time_zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String> const& available_time_zones()
|
static Vector<String> icu_available_time_zones(Optional<ByteString> const& region)
|
||||||
{
|
{
|
||||||
static auto time_zones = []() -> Vector<String> {
|
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
|
||||||
auto time_zone_enumerator = adopt_own_if_nonnull(icu::TimeZone::createEnumeration(status));
|
char const* icu_region = region.has_value() ? region->characters() : nullptr;
|
||||||
|
|
||||||
|
auto time_zone_enumerator = adopt_own_if_nonnull(icu::TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, icu_region, nullptr, status));
|
||||||
if (icu_failure(status))
|
if (icu_failure(status))
|
||||||
return { "UTC"_string };
|
return { "UTC"_string };
|
||||||
|
|
||||||
@ -91,11 +92,19 @@ Vector<String> const& available_time_zones()
|
|||||||
|
|
||||||
quick_sort(time_zones);
|
quick_sort(time_zones);
|
||||||
return time_zones;
|
return time_zones;
|
||||||
}();
|
}
|
||||||
|
|
||||||
|
Vector<String> const& available_time_zones()
|
||||||
|
{
|
||||||
|
static auto time_zones = icu_available_time_zones({});
|
||||||
return time_zones;
|
return time_zones;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<String> available_time_zones_in_region(StringView region)
|
||||||
|
{
|
||||||
|
return icu_available_time_zones(region);
|
||||||
|
}
|
||||||
|
|
||||||
Optional<String> resolve_primary_time_zone(StringView time_zone)
|
Optional<String> resolve_primary_time_zone(StringView time_zone)
|
||||||
{
|
{
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
@ -14,6 +14,7 @@ namespace Unicode {
|
|||||||
|
|
||||||
String current_time_zone();
|
String current_time_zone();
|
||||||
Vector<String> const& available_time_zones();
|
Vector<String> const& available_time_zones();
|
||||||
|
Vector<String> available_time_zones_in_region(StringView region);
|
||||||
Optional<String> resolve_primary_time_zone(StringView time_zone);
|
Optional<String> resolve_primary_time_zone(StringView time_zone);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user