From 0f26ab89aeaf5975640aa7e676a8194ab73910fb Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 21 Jul 2022 11:28:59 -0400 Subject: [PATCH] LibJS+LibUnicode: Handle flexible day periods on both sides of midnight Commit ec7d535 only partially handled the case of flexible day periods rolling over midnight, in that it only worked for hours after midnight. For example, the en locale defines a day period range of [21:00, 06:00). The previous method of adding 24 hours to the given hour would change e.g. 23:00 to 47:00, which isn't valid. --- .../LibUnicode/GenerateUnicodeDateTimeFormat.cpp | 12 ++++++++---- .../DateTimeFormat.prototype.format.js | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp index c0dfece74b..ab6f3847b3 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp @@ -2289,14 +2289,18 @@ Optional get_calendar_day_period_symbol_for_hour(StringView locale, for (auto day_period_index : day_periods) { auto day_period = s_day_periods[day_period_index]; - auto h = hour; + bool hour_falls_within_day_period = false; if (day_period.begin > day_period.end) { - day_period.end += 24; - h += 24; + if (hour >= day_period.begin) + hour_falls_within_day_period = true; + else if (hour <= day_period.end) + hour_falls_within_day_period = true; + } else if ((day_period.begin <= hour) && (hour < day_period.end)) { + hour_falls_within_day_period = true; } - if ((day_period.begin <= h) && (h < day_period.end)) { + if (hour_falls_within_day_period) { auto period = static_cast(day_period.day_period); return get_calendar_day_period_symbol(locale, calendar, style, period); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.format.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.format.js index 5a3d9a94b3..709a5d7331 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.format.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.format.js @@ -263,11 +263,15 @@ describe("dayPeriod", () => { }); test("flexible day period rolls over midnight", () => { - // For the en locale, this time (05:00) falls in the flexible day period range of [21:00, 06:00). - const date = Date.UTC(2017, 11, 12, 5, 0, 0, 0); - const en = new Intl.DateTimeFormat("en", { dayPeriod: "short", timeZone: "UTC" }); - expect(en.format(date)).toBe("5 at night"); + + // For the en locale, these times (05:00 and 23:00) fall in the flexible day period range of + // [21:00, 06:00), on either side of midnight. + const date1 = Date.UTC(2017, 11, 12, 5, 0, 0, 0); + const date2 = Date.UTC(2017, 11, 12, 23, 0, 0, 0); + + expect(en.format(date1)).toBe("5 at night"); + expect(en.format(date2)).toBe("11 at night"); }); });