mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 23:58:11 +00:00
LibUnicode: Parse and generate relative-time format patterns
Relative-time format patterns are of one of two forms: * Tensed - refer to the past or the future, e.g. "N years ago" or "in N years". * Numbered - refer to a specific numeric value, e.g. "in 1 year" becomes "next year" and "in 0 years" becomes "this year". In ECMA-402, tensed and numbered refer to the numeric formatting options of "always" and "auto", respectively.
This commit is contained in:
parent
27eda77c97
commit
789f093b2e
4 changed files with 295 additions and 1 deletions
|
@ -13,6 +13,7 @@ set(SOURCES
|
|||
DateTimeFormat.cpp
|
||||
Locale.cpp
|
||||
NumberFormat.cpp
|
||||
RelativeTimeFormat.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibUnicode unicode)
|
||||
|
|
58
Userland/Libraries/LibUnicode/RelativeTimeFormat.cpp
Normal file
58
Userland/Libraries/LibUnicode/RelativeTimeFormat.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibUnicode/RelativeTimeFormat.h>
|
||||
|
||||
namespace Unicode {
|
||||
|
||||
Optional<TimeUnit> time_unit_from_string(StringView time_unit)
|
||||
{
|
||||
if (time_unit == "second"sv)
|
||||
return TimeUnit::Second;
|
||||
if (time_unit == "minute"sv)
|
||||
return TimeUnit::Minute;
|
||||
if (time_unit == "hour"sv)
|
||||
return TimeUnit::Hour;
|
||||
if (time_unit == "day"sv)
|
||||
return TimeUnit::Day;
|
||||
if (time_unit == "week"sv)
|
||||
return TimeUnit::Week;
|
||||
if (time_unit == "month"sv)
|
||||
return TimeUnit::Month;
|
||||
if (time_unit == "quarter"sv)
|
||||
return TimeUnit::Quarter;
|
||||
if (time_unit == "year"sv)
|
||||
return TimeUnit::Year;
|
||||
return {};
|
||||
}
|
||||
|
||||
StringView time_unit_to_string(TimeUnit time_unit)
|
||||
{
|
||||
switch (time_unit) {
|
||||
case TimeUnit::Second:
|
||||
return "second"sv;
|
||||
case TimeUnit::Minute:
|
||||
return "minute"sv;
|
||||
case TimeUnit::Hour:
|
||||
return "hour"sv;
|
||||
case TimeUnit::Day:
|
||||
return "day"sv;
|
||||
case TimeUnit::Week:
|
||||
return "week"sv;
|
||||
case TimeUnit::Month:
|
||||
return "month"sv;
|
||||
case TimeUnit::Quarter:
|
||||
return "quarter"sv;
|
||||
case TimeUnit::Year:
|
||||
return "year"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
Vector<RelativeTimeFormat> __attribute__((weak)) get_relative_time_format_patterns(StringView, TimeUnit, StringView, Style) { return {}; }
|
||||
|
||||
}
|
48
Userland/Libraries/LibUnicode/RelativeTimeFormat.h
Normal file
48
Userland/Libraries/LibUnicode/RelativeTimeFormat.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibUnicode/Forward.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
|
||||
namespace Unicode {
|
||||
|
||||
// These are just the subset of fields in the CLDR required for ECMA-402.
|
||||
enum class TimeUnit {
|
||||
Second,
|
||||
Minute,
|
||||
Hour,
|
||||
Day,
|
||||
Week,
|
||||
Month,
|
||||
Quarter,
|
||||
Year,
|
||||
};
|
||||
|
||||
struct RelativeTimeFormat {
|
||||
enum class Plurality {
|
||||
Zero,
|
||||
One,
|
||||
Two,
|
||||
Few,
|
||||
Many,
|
||||
Other,
|
||||
};
|
||||
|
||||
Plurality plurality { Plurality::Other };
|
||||
StringView pattern;
|
||||
};
|
||||
|
||||
Optional<TimeUnit> time_unit_from_string(StringView time_unit);
|
||||
StringView time_unit_to_string(TimeUnit time_unit);
|
||||
|
||||
Vector<RelativeTimeFormat> get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue