2021-11-27 14:54:48 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Types.h>
|
2021-11-27 17:31:31 -05:00
|
|
|
#include <AK/Vector.h>
|
2021-11-27 14:54:48 -05:00
|
|
|
#include <LibUnicode/Forward.h>
|
|
|
|
|
|
|
|
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
|
|
|
enum class HourCycle : u8 {
|
|
|
|
H11,
|
|
|
|
H12,
|
|
|
|
H23,
|
|
|
|
H24,
|
|
|
|
};
|
|
|
|
|
2021-11-27 14:54:48 -05:00
|
|
|
enum class CalendarPatternStyle : u8 {
|
|
|
|
Narrow,
|
|
|
|
Short,
|
|
|
|
Long,
|
|
|
|
Numeric,
|
|
|
|
TwoDigit,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CalendarPattern {
|
2021-12-07 08:40:06 -05:00
|
|
|
enum class Field {
|
|
|
|
Era,
|
|
|
|
Year,
|
|
|
|
Month,
|
|
|
|
Weekday,
|
|
|
|
Day,
|
|
|
|
DayPeriod,
|
|
|
|
Hour,
|
|
|
|
Minute,
|
|
|
|
Second,
|
|
|
|
FractionalSecondDigits,
|
|
|
|
TimeZoneName,
|
|
|
|
};
|
|
|
|
|
2021-11-28 17:55:47 -05:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
|
|
|
|
{
|
2021-12-07 08:40:06 -05:00
|
|
|
callback(era, other.era, Field::Era);
|
|
|
|
callback(year, other.year, Field::Year);
|
|
|
|
callback(month, other.month, Field::Month);
|
|
|
|
callback(weekday, other.weekday, Field::Weekday);
|
|
|
|
callback(day, other.day, Field::Day);
|
|
|
|
callback(day_period, other.day_period, Field::DayPeriod);
|
|
|
|
callback(hour, other.hour, Field::Hour);
|
|
|
|
callback(minute, other.minute, Field::Minute);
|
|
|
|
callback(second, other.second, Field::Second);
|
|
|
|
callback(fractional_second_digits, other.fractional_second_digits, Field::FractionalSecondDigits);
|
|
|
|
callback(time_zone_name, other.time_zone_name, Field::TimeZoneName);
|
2021-11-28 17:55:47 -05:00
|
|
|
}
|
|
|
|
|
2021-11-27 14:54:48 -05:00
|
|
|
String pattern {};
|
|
|
|
Optional<String> pattern12 {};
|
|
|
|
|
|
|
|
// https://unicode.org/reports/tr35/tr35-dates.html#Calendar_Fields
|
|
|
|
Optional<CalendarPatternStyle> era {};
|
|
|
|
Optional<CalendarPatternStyle> year {};
|
|
|
|
Optional<CalendarPatternStyle> month {};
|
|
|
|
Optional<CalendarPatternStyle> weekday {};
|
|
|
|
Optional<CalendarPatternStyle> day {};
|
|
|
|
Optional<CalendarPatternStyle> day_period {};
|
|
|
|
Optional<CalendarPatternStyle> hour {};
|
|
|
|
Optional<CalendarPatternStyle> minute {};
|
|
|
|
Optional<CalendarPatternStyle> second {};
|
|
|
|
Optional<u8> fractional_second_digits {};
|
|
|
|
Optional<CalendarPatternStyle> time_zone_name {};
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class CalendarFormatType : u8 {
|
|
|
|
Date,
|
|
|
|
Time,
|
|
|
|
DateTime,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CalendarFormat {
|
|
|
|
CalendarPattern full_format {};
|
|
|
|
CalendarPattern long_format {};
|
|
|
|
CalendarPattern medium_format {};
|
|
|
|
CalendarPattern short_format {};
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
StringView hour_cycle_to_string(HourCycle hour_cycle);
|
2021-11-27 14:54:48 -05:00
|
|
|
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
|
|
|
|
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
|
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
|
|
|
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale);
|
|
|
|
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
|
2021-11-27 14:54:48 -05:00
|
|
|
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
|
2021-11-27 17:31:31 -05:00
|
|
|
Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
|
2021-11-27 14:54:48 -05:00
|
|
|
|
|
|
|
}
|