LibJS+LibLocale: Replace time zone display names with ICU

This commit is contained in:
Timothy Flynn 2024-06-12 11:54:55 -04:00 committed by Andreas Kling
parent 273694d8de
commit e2bffe5612
Notes: sideshowbarker 2024-07-16 23:55:09 +09:00
9 changed files with 45 additions and 429 deletions

View file

@ -59,7 +59,7 @@ if (ENABLE_UNICODE_DATABASE_DOWNLOAD)
"${CLDR_VERSION_FILE}"
"${DATE_TIME_FORMAT_DATA_HEADER}"
"${DATE_TIME_FORMAT_DATA_IMPLEMENTATION}"
arguments -r "${CLDR_CORE_PATH}" -d "${CLDR_DATES_PATH}"
arguments -r "${CLDR_CORE_PATH}"
)
invoke_generator(
"LocaleData"

View file

@ -9,9 +9,7 @@
#include <AK/ByteString.h>
#include <AK/CharacterTypes.h>
#include <AK/Error.h>
#include <AK/Find.h>
#include <AK/Format.h>
#include <AK/HashFunctions.h>
#include <AK/HashMap.h>
#include <AK/JsonObject.h>
#include <AK/JsonParser.h>
@ -19,117 +17,10 @@
#include <AK/LexicalPath.h>
#include <AK/SourceGenerator.h>
#include <AK/StringBuilder.h>
#include <AK/Traits.h>
#include <AK/Utf8View.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/Directory.h>
#include <LibLocale/DateTimeFormat.h>
#include <LibTimeZone/TimeZone.h>
struct TimeZoneNames {
unsigned hash() const
{
auto hash = pair_int_hash(short_standard_name, long_standard_name);
hash = pair_int_hash(hash, short_daylight_name);
hash = pair_int_hash(hash, long_daylight_name);
hash = pair_int_hash(hash, short_generic_name);
hash = pair_int_hash(hash, long_generic_name);
return hash;
}
bool operator==(TimeZoneNames const& other) const
{
return (short_standard_name == other.short_standard_name)
&& (long_standard_name == other.long_standard_name)
&& (short_daylight_name == other.short_daylight_name)
&& (long_daylight_name == other.long_daylight_name)
&& (short_generic_name == other.short_generic_name)
&& (long_generic_name == other.long_generic_name);
}
size_t short_standard_name { 0 };
size_t long_standard_name { 0 };
size_t short_daylight_name { 0 };
size_t long_daylight_name { 0 };
size_t short_generic_name { 0 };
size_t long_generic_name { 0 };
};
template<>
struct AK::Formatter<TimeZoneNames> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZoneNames const& time_zone)
{
return Formatter<FormatString>::format(builder,
"{{ {}, {}, {}, {}, {}, {} }}"sv,
time_zone.short_standard_name,
time_zone.long_standard_name,
time_zone.short_daylight_name,
time_zone.long_daylight_name,
time_zone.short_generic_name,
time_zone.long_generic_name);
}
};
template<>
struct AK::Traits<TimeZoneNames> : public DefaultTraits<TimeZoneNames> {
static unsigned hash(TimeZoneNames const& t) { return t.hash(); }
};
struct TimeZoneFormat {
unsigned hash() const
{
auto hash = int_hash(symbol_ahead_sign);
hash = pair_int_hash(hash, symbol_ahead_separator);
hash = pair_int_hash(hash, symbol_behind_sign);
hash = pair_int_hash(hash, symbol_behind_separator);
hash = pair_int_hash(hash, gmt_format);
hash = pair_int_hash(hash, gmt_zero_format);
return hash;
}
bool operator==(TimeZoneFormat const& other) const
{
return (symbol_ahead_sign == other.symbol_ahead_sign)
&& (symbol_ahead_separator == other.symbol_ahead_separator)
&& (symbol_behind_sign == other.symbol_behind_sign)
&& (symbol_behind_separator == other.symbol_behind_separator)
&& (gmt_format == other.gmt_format)
&& (gmt_zero_format == other.gmt_zero_format);
}
size_t symbol_ahead_sign { 0 };
size_t symbol_ahead_separator { 0 };
size_t symbol_behind_sign { 0 };
size_t symbol_behind_separator { 0 };
size_t gmt_format { 0 };
size_t gmt_zero_format { 0 };
};
template<>
struct AK::Formatter<TimeZoneFormat> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZoneFormat const& time_zone_format)
{
return Formatter<FormatString>::format(builder, "{{ {}, {}, {}, {}, {}, {} }}"sv,
time_zone_format.symbol_ahead_sign,
time_zone_format.symbol_ahead_separator,
time_zone_format.symbol_behind_sign,
time_zone_format.symbol_behind_separator,
time_zone_format.gmt_format,
time_zone_format.gmt_zero_format);
}
};
template<>
struct AK::Traits<TimeZoneFormat> : public DefaultTraits<TimeZoneFormat> {
static unsigned hash(TimeZoneFormat const& t) { return t.hash(); }
};
using TimeZoneNamesList = Vector<size_t>;
using HourCycleList = Vector<Locale::HourCycle>;
template<>
@ -140,20 +31,9 @@ struct AK::Formatter<Locale::HourCycle> : Formatter<FormatString> {
}
};
struct LocaleData {
size_t time_zones { 0 };
size_t time_zone_formats { 0 };
};
struct CLDR {
UniqueStringStorage unique_strings;
UniqueStorage<TimeZoneNames> unique_time_zones;
UniqueStorage<TimeZoneNamesList> unique_time_zone_lists;
UniqueStorage<TimeZoneFormat> unique_time_zone_formats;
UniqueStorage<HourCycleList> unique_hour_cycle_lists;
HashMap<ByteString, LocaleData> locales;
HashMap<ByteString, size_t> hour_cycles;
Vector<ByteString> hour_cycle_regions;
@ -168,9 +48,6 @@ struct CLDR {
HashMap<ByteString, Locale::Weekday> weekend_end;
Vector<ByteString> weekend_end_regions;
HashMap<ByteString, Vector<TimeZone::TimeZone>> meta_zones;
Vector<ByteString> time_zones { "UTC"sv };
};
static ErrorOr<void> parse_hour_cycles(ByteString core_path, CLDR& cldr)
@ -282,174 +159,10 @@ static ErrorOr<void> parse_week_data(ByteString core_path, CLDR& cldr)
return {};
}
static ErrorOr<void> parse_meta_zones(ByteString core_path, CLDR& cldr)
{
// https://unicode.org/reports/tr35/tr35-dates.html#Metazones
LexicalPath meta_zone_path(move(core_path));
meta_zone_path = meta_zone_path.append("supplemental"sv);
meta_zone_path = meta_zone_path.append("metaZones.json"sv);
auto meta_zone = TRY(read_json_file(meta_zone_path.string()));
auto const& supplemental_object = meta_zone.as_object().get_object("supplemental"sv).value();
auto const& meta_zone_object = supplemental_object.get_object("metaZones"sv).value();
auto const& meta_zone_array = meta_zone_object.get_array("metazones"sv).value();
meta_zone_array.for_each([&](JsonValue const& value) {
auto const& mapping = value.as_object().get_object("mapZone"sv).value();
auto const& meta_zone = mapping.get_byte_string("_other"sv).value();
auto const& golden_zone = mapping.get_byte_string("_type"sv).value();
if (auto time_zone = TimeZone::time_zone_from_string(golden_zone); time_zone.has_value()) {
auto& golden_zones = cldr.meta_zones.ensure(meta_zone);
golden_zones.append(*time_zone);
}
});
// UTC does not appear in metaZones.json. Define it for convenience so other parsers don't need to check for its existence.
if (auto time_zone = TimeZone::time_zone_from_string("UTC"sv); time_zone.has_value())
cldr.meta_zones.set("UTC"sv, { *time_zone });
return {};
}
static ErrorOr<void> parse_time_zone_names(ByteString locale_time_zone_names_path, CLDR& cldr, LocaleData& locale)
{
LexicalPath time_zone_names_path(move(locale_time_zone_names_path));
time_zone_names_path = time_zone_names_path.append("timeZoneNames.json"sv);
auto time_zone_names = TRY(read_json_file(time_zone_names_path.string()));
auto const& main_object = time_zone_names.as_object().get_object("main"sv).value();
auto const& locale_object = main_object.get_object(time_zone_names_path.parent().basename()).value();
auto const& dates_object = locale_object.get_object("dates"sv).value();
auto const& time_zone_names_object = dates_object.get_object("timeZoneNames"sv).value();
auto const& meta_zone_object = time_zone_names_object.get_object("metazone"sv);
auto const& hour_format_string = time_zone_names_object.get_byte_string("hourFormat"sv).value();
auto const& gmt_format_string = time_zone_names_object.get_byte_string("gmtFormat"sv).value();
auto const& gmt_zero_format_string = time_zone_names_object.get_byte_string("gmtZeroFormat"sv).value();
if (!meta_zone_object.has_value())
return {};
auto parse_name = [&](StringView type, JsonObject const& meta_zone_object, StringView key) -> Optional<size_t> {
auto const& names = meta_zone_object.get_object(type);
if (!names.has_value())
return {};
auto const& name = names->get_byte_string(key);
if (name.has_value())
return cldr.unique_strings.ensure(name.value());
return {};
};
auto parse_hour_format = [&](auto const& format, auto& time_zone_formats) {
auto hour_formats = format.split_view(';');
auto hour_format_ahead_start = hour_formats[0].find('H').value();
auto separator_ahead_start = hour_formats[0].find_last('H').value() + 1;
auto separator_ahead_end = hour_formats[0].find('m').value();
auto hour_format_behind_start = hour_formats[1].find('H').value();
auto separator_behind_start = hour_formats[1].find_last('H').value() + 1;
auto separator_behind_end = hour_formats[1].find('m').value();
auto symbol_ahead_sign = hour_formats[0].substring_view(0, hour_format_ahead_start);
auto symbol_ahead_separator = hour_formats[0].substring_view(separator_ahead_start, separator_ahead_end - separator_ahead_start);
auto symbol_behind_sign = hour_formats[1].substring_view(0, hour_format_behind_start);
auto symbol_behind_separator = hour_formats[1].substring_view(separator_behind_start, separator_behind_end - separator_behind_start);
time_zone_formats.symbol_ahead_sign = cldr.unique_strings.ensure(symbol_ahead_sign);
time_zone_formats.symbol_ahead_separator = cldr.unique_strings.ensure(symbol_ahead_separator);
time_zone_formats.symbol_behind_sign = cldr.unique_strings.ensure(symbol_behind_sign);
time_zone_formats.symbol_behind_separator = cldr.unique_strings.ensure(symbol_behind_separator);
};
TimeZoneNamesList time_zones;
TimeZoneFormat time_zone_formats {};
parse_hour_format(hour_format_string, time_zone_formats);
time_zone_formats.gmt_format = cldr.unique_strings.ensure(gmt_format_string);
time_zone_formats.gmt_zero_format = cldr.unique_strings.ensure(gmt_zero_format_string);
auto parse_time_zone = [&](StringView meta_zone, JsonObject const& meta_zone_object) {
auto golden_zones = cldr.meta_zones.find(meta_zone);
if (golden_zones == cldr.meta_zones.end())
return;
TimeZoneNames time_zone_names {};
if (auto name = parse_name("long"sv, meta_zone_object, "standard"sv); name.has_value())
time_zone_names.long_standard_name = name.value();
if (auto name = parse_name("short"sv, meta_zone_object, "standard"sv); name.has_value())
time_zone_names.short_standard_name = name.value();
if (auto name = parse_name("long"sv, meta_zone_object, "daylight"sv); name.has_value())
time_zone_names.long_daylight_name = name.value();
if (auto name = parse_name("short"sv, meta_zone_object, "daylight"sv); name.has_value())
time_zone_names.short_daylight_name = name.value();
if (auto name = parse_name("long"sv, meta_zone_object, "generic"sv); name.has_value())
time_zone_names.long_generic_name = name.value();
if (auto name = parse_name("short"sv, meta_zone_object, "generic"sv); name.has_value())
time_zone_names.short_generic_name = name.value();
auto time_zone_index = cldr.unique_time_zones.ensure(move(time_zone_names));
for (auto golden_zone : golden_zones->value) {
auto time_zone = to_underlying(golden_zone);
if (time_zone >= time_zones.size())
time_zones.resize(time_zone + 1);
time_zones[time_zone] = time_zone_index;
}
};
meta_zone_object->for_each_member([&](auto const& meta_zone, JsonValue const& value) {
parse_time_zone(meta_zone, value.as_object());
});
// The long and short names for UTC are not under the "timeZoneNames/metazone" object, but are under "timeZoneNames/zone/Etc".
auto const& zone_object = time_zone_names_object.get_object("zone"sv).value();
auto const& etc_object = zone_object.get_object("Etc"sv).value();
auto const& utc_object = etc_object.get_object("UTC"sv).value();
parse_time_zone("UTC"sv, utc_object);
locale.time_zones = cldr.unique_time_zone_lists.ensure(move(time_zones));
locale.time_zone_formats = cldr.unique_time_zone_formats.ensure(move(time_zone_formats));
return {};
}
static ErrorOr<void> parse_all_locales(ByteString core_path, ByteString dates_path, CLDR& cldr)
static ErrorOr<void> parse_all_locales(ByteString core_path, CLDR& cldr)
{
TRY(parse_hour_cycles(core_path, cldr));
TRY(parse_week_data(core_path, cldr));
TRY(parse_meta_zones(core_path, cldr));
auto remove_variants_from_path = [&](ByteString path) -> ErrorOr<ByteString> {
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));
if (auto script = cldr.unique_strings.get(parsed_locale.script); !script.is_empty())
builder.appendff("-{}", script);
if (auto region = cldr.unique_strings.get(parsed_locale.region); !region.is_empty())
builder.appendff("-{}", region);
return builder.to_byte_string();
};
TRY(Core::Directory::for_each_entry(TRY(String::formatted("{}/main", dates_path)), Core::DirIterator::SkipParentAndBaseDir, [&](auto& entry, auto& directory) -> ErrorOr<IterationDecision> {
auto dates_path = LexicalPath::join(directory.path().string(), entry.name).string();
auto language = TRY(remove_variants_from_path(dates_path));
auto& locale = cldr.locales.ensure(language);
TRY(parse_time_zone_names(move(dates_path), cldr, locale));
return IterationDecision::Continue;
}));
return {};
}
@ -496,9 +209,6 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::InputBufferedF
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, cldr.unique_strings.type_that_fits());
generator.set("time_zone_index_type"sv, cldr.unique_time_zones.type_that_fits());
generator.set("time_zone_list_index_type"sv, cldr.unique_time_zone_lists.type_that_fits());
generator.append(R"~~~(
#include <AK/Array.h>
@ -511,53 +221,10 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::InputBufferedF
#include <LibLocale/DateTimeFormatData.h>
#include <LibLocale/Locale.h>
#include <LibLocale/LocaleData.h>
#include <LibTimeZone/TimeZone.h>
namespace Locale {
)~~~");
cldr.unique_strings.generate(generator);
generator.append(R"~~~(
struct TimeZoneNames {
@string_index_type@ short_standard_name { 0 };
@string_index_type@ long_standard_name { 0 };
@string_index_type@ short_daylight_name { 0 };
@string_index_type@ long_daylight_name { 0 };
@string_index_type@ short_generic_name { 0 };
@string_index_type@ long_generic_name { 0 };
};
struct TimeZoneFormatImpl {
TimeZoneFormat to_time_zone_format() const {
TimeZoneFormat time_zone_format {};
time_zone_format.symbol_ahead_sign = decode_string(symbol_ahead_sign);
time_zone_format.symbol_ahead_separator = decode_string(symbol_ahead_separator);
time_zone_format.symbol_behind_sign = decode_string(symbol_behind_sign);
time_zone_format.symbol_behind_separator = decode_string(symbol_behind_separator);
time_zone_format.gmt_format = decode_string(gmt_format);
time_zone_format.gmt_zero_format = decode_string(gmt_zero_format);
return time_zone_format;
}
@string_index_type@ symbol_ahead_sign { 0 };
@string_index_type@ symbol_ahead_separator { 0 };
@string_index_type@ symbol_behind_sign { 0 };
@string_index_type@ symbol_behind_separator { 0 };
@string_index_type@ gmt_format { 0 };
@string_index_type@ gmt_zero_format { 0 };
};
)~~~");
cldr.unique_time_zones.generate(generator, "TimeZoneNames"sv, "s_time_zones"sv, 30);
cldr.unique_time_zone_lists.generate(generator, cldr.unique_time_zones.type_that_fits(), "s_time_zone_lists"sv);
cldr.unique_time_zone_formats.generate(generator, "TimeZoneFormatImpl"sv, "s_time_zone_formats"sv, 30);
cldr.unique_hour_cycle_lists.generate(generator, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycle_lists"sv);
auto append_mapping = [&](auto const& keys, auto const& map, auto type, auto name, auto mapping_getter) {
@ -581,11 +248,6 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
generator.append(" } };");
};
auto locales = cldr.locales.keys();
quick_sort(locales);
append_mapping(locales, cldr.locales, cldr.unique_time_zones.type_that_fits(), "s_locale_time_zones"sv, [](auto const& locale) { return locale.time_zones; });
append_mapping(locales, cldr.locales, cldr.unique_time_zone_formats.type_that_fits(), "s_locale_time_zone_formats"sv, [](auto const& locale) { return locale.time_zone_formats; });
append_mapping(cldr.hour_cycle_regions, cldr.hour_cycles, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycles"sv, [](auto const& hour_cycles) { return hour_cycles; });
append_mapping(cldr.minimum_days_regions, cldr.minimum_days, "u8"sv, "s_minimum_days"sv, [](auto minimum_days) { return minimum_days; });
append_mapping(cldr.first_day_regions, cldr.first_day, "u8"sv, "s_first_day"sv, [](auto first_day) { return to_underlying(first_day); });
@ -660,70 +322,6 @@ Optional<@return_type@> get_regional_@lookup_type@(StringView region)
append_regional_lookup("Weekday"sv, "weekend_end"sv);
generator.append(R"~~~(
Optional<TimeZoneFormat> get_time_zone_format(StringView locale)
{
auto locale_value = locale_from_string(locale);
if (!locale_value.has_value())
return {};
auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
auto time_zone_format_index = s_locale_time_zone_formats.at(locale_index);
auto const& time_zone_format = s_time_zone_formats.at(time_zone_format_index);
return time_zone_format.to_time_zone_format();
}
static TimeZoneNames const* find_time_zone_names(StringView locale, StringView time_zone)
{
auto locale_value = locale_from_string(locale);
if (!locale_value.has_value())
return nullptr;
auto time_zone_value = ::TimeZone::time_zone_from_string(time_zone);
if (!time_zone_value.has_value())
return nullptr;
auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
size_t time_zone_index = to_underlying(*time_zone_value);
auto time_zone_list_index = s_locale_time_zones.at(locale_index);
auto const& time_zone_list = s_time_zone_lists.at(time_zone_list_index);
if (time_zone_list.size() <= time_zone_index)
return nullptr;
time_zone_index = time_zone_list.at(time_zone_index);
return &s_time_zones[time_zone_index];
}
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style, TimeZone::InDST in_dst)
{
if (auto const* data = find_time_zone_names(locale, time_zone); data != nullptr) {
size_t name_index = 0;
switch (style) {
case CalendarPatternStyle::Short:
name_index = (in_dst == TimeZone::InDST::No) ? data->short_standard_name : data->short_daylight_name;
break;
case CalendarPatternStyle::Long:
name_index = (in_dst == TimeZone::InDST::No) ? data->long_standard_name : data->long_daylight_name;
break;
case CalendarPatternStyle::ShortGeneric:
name_index = data->short_generic_name;
break;
case CalendarPatternStyle::LongGeneric:
name_index = data->long_generic_name;
break;
default:
VERIFY_NOT_REACHED();
}
if (name_index != 0)
return decode_string(name_index);
}
return {};
}
}
)~~~");
@ -736,20 +334,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
StringView generated_header_path;
StringView generated_implementation_path;
StringView core_path;
StringView dates_path;
Core::ArgsParser args_parser;
args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path");
args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
args_parser.add_option(core_path, "Path to cldr-core directory", "core-path", 'r', "core-path");
args_parser.add_option(dates_path, "Path to cldr-dates directory", "dates-path", 'd', "dates-path");
args_parser.parse(arguments);
auto generated_header_file = TRY(open_file(generated_header_path, Core::File::OpenMode::Write));
auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::File::OpenMode::Write));
CLDR cldr;
TRY(parse_all_locales(core_path, dates_path, cldr));
TRY(parse_all_locales(core_path, cldr));
TRY(generate_unicode_locale_header(*generated_header_file, cldr));
TRY(generate_unicode_locale_implementation(*generated_implementation_file, cldr));

View file

@ -24,7 +24,7 @@
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibLocale/DateTimeFormat.h>
#include <LibLocale/DisplayNames.h>
#include <LibLocale/Locale.h>
#include <LibTimeZone/TimeZone.h>
@ -1152,12 +1152,14 @@ ByteString time_zone_string(double time)
auto offset_hour = hour_from_time(offset);
// 9. Let tzName be an implementation-defined string that is either the empty String or the string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), an implementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
auto tz_name = TimeZone::current_time_zone();
String tz_name;
// Most implementations seem to prefer the long-form display name of the time zone. Not super important, but we may as well match that behavior.
if (auto maybe_offset = TimeZone::get_time_zone_offset(tz_name, AK::UnixDateTime::from_milliseconds_since_epoch(time)); maybe_offset.has_value()) {
if (auto long_name = Locale::get_time_zone_name(Locale::default_locale(), tz_name, Locale::CalendarPatternStyle::Long, maybe_offset->in_dst); long_name.has_value())
tz_name = long_name.release_value();
if (auto name = Locale::time_zone_display_name(Locale::default_locale(), tz_name, maybe_offset->in_dst, time); name.has_value())
tz_name = name.release_value();
} else {
tz_name = MUST(String::from_utf8(TimeZone::current_time_zone()));
}
// 10. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.

View file

@ -553,9 +553,6 @@ Optional<Weekday> get_locale_weekend_end(StringView locale)
return find_regional_values_for_locale<Optional<Weekday>>(locale, get_regional_weekend_end);
}
Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle, TimeZone::InDST) { return {}; }
Optional<TimeZoneFormat> __attribute__((weak)) get_time_zone_format(StringView) { return {}; }
// ICU does not contain a field enumeration for "literal" partitions. Define a custom field so that we may provide a
// type for those partitions.
static constexpr i32 LITERAL_FIELD = -1;

View file

@ -13,7 +13,6 @@
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibLocale/Forward.h>
#include <LibTimeZone/TimeZone.h>
namespace Locale {
@ -97,17 +96,6 @@ struct CalendarPattern {
Optional<CalendarPatternStyle> time_zone_name;
};
struct TimeZoneFormat {
StringView symbol_ahead_sign {};
StringView symbol_ahead_separator {};
StringView symbol_behind_sign {};
StringView symbol_behind_separator {};
StringView gmt_format {};
StringView gmt_zero_format {};
};
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
Vector<HourCycle> get_regional_hour_cycles(StringView region);
Vector<HourCycle> get_locale_hour_cycles(StringView locale);
@ -129,9 +117,6 @@ Optional<WeekendEndRegion> weekend_end_region_from_string(StringView weekend_end
Optional<Weekday> get_regional_weekend_end(StringView region);
Optional<Weekday> get_locale_weekend_end(StringView locale);
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style, TimeZone::InDST in_dst);
Optional<TimeZoneFormat> get_time_zone_format(StringView locale);
class DateTimeFormat {
public:
static NonnullOwnPtr<DateTimeFormat> create_for_date_and_time_style(

View file

@ -13,6 +13,7 @@
#include <unicode/dtptngen.h>
#include <unicode/localebuilder.h>
#include <unicode/locdspnm.h>
#include <unicode/tznames.h>
#include <unicode/udatpg.h>
namespace Locale {
@ -171,6 +172,22 @@ Optional<String> date_time_field_display_name(StringView locale, StringView fiel
return icu_string_to_string(result);
}
Optional<String> time_zone_display_name(StringView locale, StringView time_zone_identifier, TimeZone::InDST in_dst, double time)
{
auto locale_data = LocaleData::for_locale(locale);
if (!locale_data.has_value())
return {};
icu::UnicodeString time_zone_name;
auto type = in_dst == TimeZone::InDST::Yes ? UTZNM_LONG_DAYLIGHT : UTZNM_LONG_STANDARD;
locale_data->time_zone_names().getDisplayName(icu_string(time_zone_identifier), type, time, time_zone_name);
if (static_cast<bool>(time_zone_name.isBogus()))
return {};
return icu_string_to_string(time_zone_name);
}
static constexpr Array<UChar, 4> icu_currency_code(StringView currency)
{
VERIFY(currency.length() == 3);

View file

@ -10,6 +10,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibLocale/Locale.h>
#include <LibTimeZone/TimeZone.h>
namespace Locale {
@ -26,6 +27,7 @@ Optional<String> region_display_name(StringView locale, StringView region);
Optional<String> script_display_name(StringView locale, StringView script);
Optional<String> calendar_display_name(StringView locale, StringView calendar);
Optional<String> date_time_field_display_name(StringView locale, StringView field, Style);
Optional<String> time_zone_display_name(StringView locale, StringView time_zone_identifier, TimeZone::InDST, double time);
Optional<String> currency_display_name(StringView locale, StringView currency, Style);
Optional<String> currency_numeric_display_name(StringView locale, StringView currency);

View file

@ -13,6 +13,7 @@
#include <unicode/dtptngen.h>
#include <unicode/locdspnm.h>
#include <unicode/tznames.h>
#include <unicode/unistr.h>
namespace Locale {
@ -85,6 +86,18 @@ icu::DateTimePatternGenerator& LocaleData::date_time_pattern_generator()
return *m_date_time_pattern_generator;
}
icu::TimeZoneNames& LocaleData::time_zone_names()
{
if (!m_time_zone_names) {
UErrorCode status = U_ZERO_ERROR;
m_time_zone_names = adopt_own(*icu::TimeZoneNames::createInstance(locale(), status));
VERIFY(icu_success(status));
}
return *m_time_zone_names;
}
icu::UnicodeString icu_string(StringView string)
{
return icu::UnicodeString::fromUTF8(icu_string_piece(string));

View file

@ -21,6 +21,7 @@
U_NAMESPACE_BEGIN
class DateTimePatternGenerator;
class LocaleDisplayNames;
class TimeZoneNames;
class UnicodeString;
U_NAMESPACE_END
@ -39,6 +40,8 @@ public:
icu::DateTimePatternGenerator& date_time_pattern_generator();
icu::TimeZoneNames& time_zone_names();
private:
explicit LocaleData(icu::Locale locale);
@ -48,6 +51,7 @@ private:
OwnPtr<icu::LocaleDisplayNames> m_standard_display_names;
OwnPtr<icu::LocaleDisplayNames> m_dialect_display_names;
OwnPtr<icu::DateTimePatternGenerator> m_date_time_pattern_generator;
OwnPtr<icu::TimeZoneNames> m_time_zone_names;
};
static constexpr bool icu_success(UErrorCode code)