mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:27:45 +00:00
LibUnicode: Parse and generate date, time, and date-time format patterns
This commit is contained in:
parent
5c57341672
commit
f471ecdbe9
6 changed files with 384 additions and 5 deletions
|
@ -4,6 +4,7 @@ SET(SOURCES
|
|||
${UNICODE_DATA_SOURCES}
|
||||
CharacterTypes.cpp
|
||||
CurrencyCode.cpp
|
||||
DateTimeFormat.cpp
|
||||
Locale.cpp
|
||||
NumberFormat.cpp
|
||||
)
|
||||
|
|
66
Userland/Libraries/LibUnicode/DateTimeFormat.cpp
Normal file
66
Userland/Libraries/LibUnicode/DateTimeFormat.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibUnicode/DateTimeFormat.h>
|
||||
|
||||
#if ENABLE_UNICODE_DATA
|
||||
# include <LibUnicode/UnicodeDateTimeFormat.h>
|
||||
#endif
|
||||
|
||||
namespace Unicode {
|
||||
|
||||
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:
|
||||
return "Numeric"sv;
|
||||
case CalendarPatternStyle::TwoDigit:
|
||||
return "2-digit"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
60
Userland/Libraries/LibUnicode/DateTimeFormat.h
Normal file
60
Userland/Libraries/LibUnicode/DateTimeFormat.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* 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>
|
||||
#include <LibUnicode/Forward.h>
|
||||
|
||||
namespace Unicode {
|
||||
|
||||
enum class CalendarPatternStyle : u8 {
|
||||
Narrow,
|
||||
Short,
|
||||
Long,
|
||||
Numeric,
|
||||
TwoDigit,
|
||||
};
|
||||
|
||||
struct CalendarPattern {
|
||||
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 {};
|
||||
};
|
||||
|
||||
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
|
||||
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
|
||||
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
|
||||
|
||||
}
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
namespace Unicode {
|
||||
|
||||
enum class CalendarFormatType : u8;
|
||||
enum class CalendarPatternStyle : u8;
|
||||
enum class CompactNumberFormatType : u8;
|
||||
enum class Condition : u8;
|
||||
enum class GeneralCategory : u8;
|
||||
|
@ -24,6 +26,8 @@ enum class Style : u8;
|
|||
enum class Territory : u8;
|
||||
enum class WordBreakProperty : u8;
|
||||
|
||||
struct CalendarFormat;
|
||||
struct CalendarPattern;
|
||||
struct CurrencyCode;
|
||||
struct Keyword;
|
||||
struct LanguageID;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue