mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibJS: Selectively display DateTimeFormat day periods as noon
This commit is contained in:
parent
32c07bc6c3
commit
b8d4f8debf
Notes:
sideshowbarker
2024-07-17 08:40:27 +09:00
Author: https://github.com/trflynn89
Commit: b8d4f8debf
Pull-request: https://github.com/SerenityOS/serenity/pull/14637
Reviewed-by: https://github.com/linusg ✅
2 changed files with 68 additions and 1 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Find.h>
|
||||
#include <AK/IterationDecision.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
|
@ -503,6 +504,30 @@ static Optional<StyleAndValue> find_calendar_field(StringView name, Unicode::Cal
|
|||
return {};
|
||||
}
|
||||
|
||||
static Optional<StringView> resolve_day_period(StringView locale, StringView calendar, Unicode::CalendarPatternStyle style, Span<PatternPartition const> pattern_parts, LocalTime local_time)
|
||||
{
|
||||
// Use the "noon" day period if the locale has it, but only if the time is either exactly 12:00.00 or would be displayed as such.
|
||||
if (local_time.hour == 12) {
|
||||
auto it = find_if(pattern_parts.begin(), pattern_parts.end(), [&](auto const& part) {
|
||||
if (part.type == "minute"sv && local_time.minute != 0)
|
||||
return true;
|
||||
if (part.type == "second"sv && local_time.second != 0)
|
||||
return true;
|
||||
if (part.type == "fractionalSecondDigits"sv && local_time.millisecond != 0)
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
|
||||
if (it == pattern_parts.end()) {
|
||||
auto noon_symbol = Unicode::get_calendar_day_period_symbol(locale, calendar, style, Unicode::DayPeriod::Noon);
|
||||
if (noon_symbol.has_value())
|
||||
return *noon_symbol;
|
||||
}
|
||||
}
|
||||
|
||||
return Unicode::get_calendar_day_period_symbol_for_hour(locale, calendar, style, local_time.hour);
|
||||
}
|
||||
|
||||
// 11.5.6 FormatDateTimePattern ( dateTimeFormat, patternParts, x, rangeFormatOptions ), https://tc39.es/ecma402/#sec-formatdatetimepattern
|
||||
ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObject& global_object, DateTimeFormat& date_time_format, Vector<PatternPartition> pattern_parts, double time, Unicode::CalendarPattern const* range_format_options)
|
||||
{
|
||||
|
@ -606,7 +631,7 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObjec
|
|||
auto style = date_time_format.day_period();
|
||||
|
||||
// ii. Let fv be a String value representing the day period of tm in the form given by f; the String value depends upon the implementation and the effective locale of dateTimeFormat.
|
||||
auto symbol = Unicode::get_calendar_day_period_symbol_for_hour(data_locale, date_time_format.calendar(), style, local_time.hour);
|
||||
auto symbol = resolve_day_period(data_locale, date_time_format.calendar(), style, pattern_parts, local_time);
|
||||
if (symbol.has_value())
|
||||
formatted_value = *symbol;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue