2021-11-27 14:54:48 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-12-08 19:52:48 -05:00
|
|
|
#include <AK/Array.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2021-11-27 14:54:48 -05:00
|
|
|
#include <LibUnicode/DateTimeFormat.h>
|
LibUnicode: Parse and generate regional hour cycles
Unlike most data in the CLDR, hour cycles are not stored on a per-locale
basis. Instead, they are keyed by a string that is usually a region, but
sometimes is a locale. Therefore, given a locale, to determine the hour
cycles for that locale, we:
1. Check if the locale itself is assigned hour cycles.
2. If the locale has a region, check if that region is assigned hour
cycles.
3. Otherwise, maximize that locale, and if the maximized locale has
a region, check if that region is assigned hour cycles.
4. If the above all fail, fallback to the "001" region.
Further, each locale's default hour cycle is the first assigned hour
cycle.
2021-11-27 20:57:21 -05:00
|
|
|
#include <LibUnicode/Locale.h>
|
2021-11-27 14:54:48 -05:00
|
|
|
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
# include <LibUnicode/UnicodeDateTimeFormat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Unicode {
|
|
|
|
|
LibUnicode: Parse and generate regional hour cycles
Unlike most data in the CLDR, hour cycles are not stored on a per-locale
basis. Instead, they are keyed by a string that is usually a region, but
sometimes is a locale. Therefore, given a locale, to determine the hour
cycles for that locale, we:
1. Check if the locale itself is assigned hour cycles.
2. If the locale has a region, check if that region is assigned hour
cycles.
3. Otherwise, maximize that locale, and if the maximized locale has
a region, check if that region is assigned hour cycles.
4. If the above all fail, fallback to the "001" region.
Further, each locale's default hour cycle is the first assigned hour
cycle.
2021-11-27 20:57:21 -05:00
|
|
|
HourCycle hour_cycle_from_string(StringView hour_cycle)
|
|
|
|
{
|
|
|
|
if (hour_cycle == "h11"sv)
|
|
|
|
return Unicode::HourCycle::H11;
|
|
|
|
else if (hour_cycle == "h12"sv)
|
|
|
|
return Unicode::HourCycle::H12;
|
|
|
|
else if (hour_cycle == "h23"sv)
|
|
|
|
return Unicode::HourCycle::H23;
|
|
|
|
else if (hour_cycle == "h24"sv)
|
|
|
|
return Unicode::HourCycle::H24;
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView hour_cycle_to_string(HourCycle hour_cycle)
|
|
|
|
{
|
|
|
|
switch (hour_cycle) {
|
|
|
|
case HourCycle::H11:
|
|
|
|
return "h11"sv;
|
|
|
|
case HourCycle::H12:
|
|
|
|
return "h12"sv;
|
|
|
|
case HourCycle::H23:
|
|
|
|
return "h23"sv;
|
|
|
|
case HourCycle::H24:
|
|
|
|
return "h24"sv;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:54:48 -05:00
|
|
|
CalendarPatternStyle calendar_pattern_style_from_string(StringView style)
|
|
|
|
{
|
|
|
|
if (style == "narrow"sv)
|
|
|
|
return CalendarPatternStyle::Narrow;
|
|
|
|
if (style == "short"sv)
|
|
|
|
return CalendarPatternStyle::Short;
|
|
|
|
if (style == "long"sv)
|
|
|
|
return CalendarPatternStyle::Long;
|
|
|
|
if (style == "numeric"sv)
|
|
|
|
return CalendarPatternStyle::Numeric;
|
|
|
|
if (style == "2-digit"sv)
|
|
|
|
return CalendarPatternStyle::TwoDigit;
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
|
|
|
|
{
|
|
|
|
switch (style) {
|
|
|
|
case CalendarPatternStyle::Narrow:
|
|
|
|
return "narrow"sv;
|
|
|
|
case CalendarPatternStyle::Short:
|
|
|
|
return "short"sv;
|
|
|
|
case CalendarPatternStyle::Long:
|
|
|
|
return "long"sv;
|
|
|
|
case CalendarPatternStyle::Numeric:
|
2021-11-30 10:39:56 -05:00
|
|
|
return "numeric"sv;
|
2021-11-27 14:54:48 -05:00
|
|
|
case CalendarPatternStyle::TwoDigit:
|
|
|
|
return "2-digit"sv;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
LibUnicode: Parse and generate regional hour cycles
Unlike most data in the CLDR, hour cycles are not stored on a per-locale
basis. Instead, they are keyed by a string that is usually a region, but
sometimes is a locale. Therefore, given a locale, to determine the hour
cycles for that locale, we:
1. Check if the locale itself is assigned hour cycles.
2. If the locale has a region, check if that region is assigned hour
cycles.
3. Otherwise, maximize that locale, and if the maximized locale has
a region, check if that region is assigned hour cycles.
4. If the above all fail, fallback to the "001" region.
Further, each locale's default hour cycle is the first assigned hour
cycle.
2021-11-27 20:57:21 -05:00
|
|
|
// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
|
|
Vector<Unicode::HourCycle> get_regional_hour_cycles([[maybe_unused]] StringView locale)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
if (auto hour_cycles = Detail::get_regional_hour_cycles(locale); !hour_cycles.is_empty())
|
|
|
|
return hour_cycles;
|
|
|
|
|
|
|
|
auto return_default_hour_cycles = []() {
|
|
|
|
auto hour_cycles = Detail::get_regional_hour_cycles("001"sv);
|
|
|
|
VERIFY(!hour_cycles.is_empty());
|
|
|
|
return hour_cycles;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto language = parse_unicode_language_id(locale);
|
|
|
|
if (!language.has_value())
|
|
|
|
return return_default_hour_cycles();
|
|
|
|
|
|
|
|
if (!language->region.has_value())
|
|
|
|
language = add_likely_subtags(*language);
|
|
|
|
if (!language.has_value() || !language->region.has_value())
|
|
|
|
return return_default_hour_cycles();
|
|
|
|
|
|
|
|
if (auto hour_cycles = Detail::get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
|
|
|
|
return hour_cycles;
|
|
|
|
|
|
|
|
return return_default_hour_cycles();
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
|
|
|
|
{
|
|
|
|
if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
|
|
|
|
return hour_cycles.first();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:52:48 -05:00
|
|
|
String combine_skeletons(StringView first, StringView second)
|
|
|
|
{
|
|
|
|
// https://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
|
|
|
|
constexpr auto field_order = Array {
|
|
|
|
"G"sv, // Era
|
|
|
|
"yYuUr"sv, // Year
|
|
|
|
"ML"sv, // Month
|
|
|
|
"dDFg"sv, // Day
|
|
|
|
"Eec"sv, // Weekday
|
|
|
|
"abB"sv, // Period
|
|
|
|
"hHKk"sv, // Hour
|
|
|
|
"m"sv, // Minute
|
|
|
|
"sSA"sv, // Second
|
|
|
|
"zZOvVXx"sv, // Zone
|
|
|
|
};
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
auto append_from_skeleton = [&](auto skeleton, auto ch) {
|
|
|
|
auto first_index = skeleton.find(ch);
|
|
|
|
if (!first_index.has_value())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto last_index = skeleton.find_last(ch);
|
|
|
|
|
|
|
|
builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto fields : field_order) {
|
|
|
|
for (auto ch : fields) {
|
|
|
|
if (append_from_skeleton(first, ch))
|
|
|
|
break;
|
|
|
|
if (append_from_skeleton(second, ch))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:54:48 -05:00
|
|
|
Optional<CalendarFormat> get_calendar_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarFormatType type)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
switch (type) {
|
|
|
|
case CalendarFormatType::Date:
|
|
|
|
return Detail::get_calendar_date_format(locale, calendar);
|
|
|
|
case CalendarFormatType::Time:
|
|
|
|
return Detail::get_calendar_time_format(locale, calendar);
|
|
|
|
case CalendarFormatType::DateTime:
|
|
|
|
return Detail::get_calendar_date_time_format(locale, calendar);
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:31:31 -05:00
|
|
|
Vector<CalendarPattern> get_calendar_available_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_available_formats(locale, calendar);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:07:30 -05:00
|
|
|
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_default_range_format(locale, calendar);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] StringView skeleton)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_range_formats(locale, calendar, skeleton);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] StringView skeleton)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_range12_formats(locale, calendar, skeleton);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:22:39 -05:00
|
|
|
Optional<StringView> get_calendar_era_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Era value)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_era_symbol(locale, calendar, style, value);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StringView> get_calendar_month_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Month value)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_month_symbol(locale, calendar, style, value);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StringView> get_calendar_weekday_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Weekday value)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_weekday_symbol(locale, calendar, style, value);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StringView> get_calendar_day_period_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::DayPeriod value)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_day_period_symbol(locale, calendar, style, value);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-10 12:57:17 -05:00
|
|
|
Optional<StringView> get_calendar_day_period_symbol_for_hour([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] u8 hour)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_calendar_day_period_symbol_for_hour(locale, calendar, style, hour);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-06 15:46:49 -05:00
|
|
|
Optional<StringView> get_time_zone_name([[maybe_unused]] StringView locale, [[maybe_unused]] StringView time_zone, [[maybe_unused]] CalendarPatternStyle style)
|
|
|
|
{
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
|
|
return Detail::get_time_zone_name(locale, time_zone, style);
|
|
|
|
#else
|
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:54:48 -05:00
|
|
|
}
|