LibLocale: Parse day-period hour cycle preferences

For example, the locale "fr-FR" will have the preferred hour cycle list
of "H hB", meaning h23 and h12-with-day-periods. Whether date-times are
actually formatted with day-periods is up to the user, but we need to
parse the hour cycle as h12 to know that the FR region supports h12.

This bug was revealed by LibJS no longer blindly falling back to h12 (if
the `hour12` option is true) or h24 (if the `hour12` option is false).
This commit is contained in:
Timothy Flynn 2023-10-05 09:34:05 -04:00 committed by Andreas Kling
parent 05e080c4ba
commit eeb16f03bb
2 changed files with 14 additions and 2 deletions

View file

@ -575,9 +575,9 @@ static ErrorOr<void> parse_hour_cycles(DeprecatedString core_path, CLDR& cldr)
auto const& time_data_object = supplemental_object.get_object("timeData"sv).value();
auto parse_hour_cycle = [](StringView hour_cycle) -> Optional<Locale::HourCycle> {
if (hour_cycle == "h"sv)
if (hour_cycle.is_one_of("h"sv, "hb"sv, "hB"sv))
return Locale::HourCycle::H12;
if (hour_cycle == "H"sv)
if (hour_cycle.is_one_of("H"sv, "Hb"sv, "HB"sv))
return Locale::HourCycle::H23;
if (hour_cycle == "K"sv)
return Locale::HourCycle::H11;

View file

@ -109,6 +109,18 @@ describe("correct behavior", () => {
const ja3 = Intl.DateTimeFormat("ja", { hour: "numeric", hour12: false });
expect(ja3.resolvedOptions().hourCycle).toBe("h23");
expect(ja3.resolvedOptions().hour12).toBeFalse();
const fr1 = Intl.DateTimeFormat("fr", { hour: "numeric" });
expect(fr1.resolvedOptions().hourCycle).toBe("h23");
expect(fr1.resolvedOptions().hour12).toBeFalse();
const fr2 = Intl.DateTimeFormat("fr", { hour: "numeric", hour12: true });
expect(fr2.resolvedOptions().hourCycle).toBe("h12");
expect(fr2.resolvedOptions().hour12).toBeTrue();
const fr3 = Intl.DateTimeFormat("fr", { hour: "numeric", hour12: false });
expect(fr3.resolvedOptions().hourCycle).toBe("h23");
expect(fr3.resolvedOptions().hour12).toBeFalse();
});
test("timeZone", () => {