From eeb16f03bb3c887179cfa4a9c81bd158d8c4aa48 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 5 Oct 2023 09:34:05 -0400 Subject: [PATCH] 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). --- .../LibLocale/GenerateDateTimeFormatData.cpp | 4 ++-- .../DateTimeFormat.prototype.resolvedOptions.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp index 9eb5fb687a..8481023696 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp @@ -575,9 +575,9 @@ static ErrorOr 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 { - 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; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.resolvedOptions.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.resolvedOptions.js index 68b8136552..d17b5592b4 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.resolvedOptions.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.resolvedOptions.js @@ -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", () => {