From 4400150cd2d2142ecc747f7721ae217eb3ca1cbb Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 19 Jan 2022 14:54:19 -0500 Subject: [PATCH] LibJS+LibUnicode: Return the appropriate time zone name depending on DST --- .../GenerateUnicodeDateTimeFormat.cpp | 6 +-- .../LibUnicode/TestUnicodeDateTimeFormat.cpp | 50 +++++++++++++++++++ .../Libraries/LibJS/Runtime/DatePrototype.cpp | 6 ++- .../Libraries/LibUnicode/DateTimeFormat.cpp | 40 +++++++-------- .../Libraries/LibUnicode/DateTimeFormat.h | 3 +- 5 files changed, 79 insertions(+), 26 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp index 3252bf06833..e3bfebfe0b0 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp @@ -2207,17 +2207,17 @@ static TimeZoneNames const* find_time_zone_names(StringView locale, StringView t return &s_time_zones[time_zone_index]; } -Optional get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style) +Optional get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style, TimeZone::InDST in_dst) { if (auto const* data = find_time_zone_names(locale, time_zone); data != nullptr) { size_t name_index = 0; switch (style) { case CalendarPatternStyle::Short: - name_index = data->short_standard_name; + name_index = (in_dst == TimeZone::InDST::No) ? data->short_standard_name : data->short_daylight_name; break; case CalendarPatternStyle::Long: - name_index = data->long_standard_name; + name_index = (in_dst == TimeZone::InDST::No) ? data->long_standard_name : data->long_daylight_name; break; case CalendarPatternStyle::ShortGeneric: name_index = data->short_generic_name; diff --git a/Tests/LibUnicode/TestUnicodeDateTimeFormat.cpp b/Tests/LibUnicode/TestUnicodeDateTimeFormat.cpp index b22c36d4e84..d73eb5aec05 100644 --- a/Tests/LibUnicode/TestUnicodeDateTimeFormat.cpp +++ b/Tests/LibUnicode/TestUnicodeDateTimeFormat.cpp @@ -80,6 +80,56 @@ TEST_CASE(time_zone_name) } } +TEST_CASE(time_zone_name_dst) +{ + struct TestData { + StringView locale; + Unicode::CalendarPatternStyle style; + StringView time_zone; + StringView expected_result; + }; + + constexpr auto test_data = Array { + TestData { "en"sv, Unicode::CalendarPatternStyle::Long, "UTC"sv, "Coordinated Universal Time"sv }, + TestData { "en"sv, Unicode::CalendarPatternStyle::Short, "UTC"sv, "UTC"sv }, + + TestData { "ar"sv, Unicode::CalendarPatternStyle::Long, "UTC"sv, "التوقيت العالمي المنسق"sv }, + TestData { "ar"sv, Unicode::CalendarPatternStyle::Short, "UTC"sv, "UTC"sv }, + + TestData { "en"sv, Unicode::CalendarPatternStyle::Long, "America/Los_Angeles"sv, "Pacific Daylight Time"sv }, + TestData { "en"sv, Unicode::CalendarPatternStyle::Short, "America/Los_Angeles"sv, "PDT"sv }, + + TestData { "ar"sv, Unicode::CalendarPatternStyle::Long, "America/Los_Angeles"sv, "توقيت المحيط الهادي الصيفي"sv }, + TestData { "ar"sv, Unicode::CalendarPatternStyle::Short, "America/Los_Angeles"sv, "غرينتش-٧"sv }, + + TestData { "en"sv, Unicode::CalendarPatternStyle::Long, "America/Vancouver"sv, "Pacific Daylight Time"sv }, + TestData { "en"sv, Unicode::CalendarPatternStyle::Short, "America/Vancouver"sv, "PDT"sv }, + + TestData { "ar"sv, Unicode::CalendarPatternStyle::Long, "America/Vancouver"sv, "توقيت المحيط الهادي الصيفي"sv }, + TestData { "ar"sv, Unicode::CalendarPatternStyle::Short, "America/Vancouver"sv, "غرينتش-٧"sv }, + + // FIXME: This should be "British Summer Time", but the CLDR puts that one name in a section we aren't parsing. + TestData { "en"sv, Unicode::CalendarPatternStyle::Long, "Europe/London"sv, "GMT+01:00"sv }, + TestData { "en"sv, Unicode::CalendarPatternStyle::Short, "Europe/London"sv, "GMT+1"sv }, + + TestData { "ar"sv, Unicode::CalendarPatternStyle::Long, "Europe/London"sv, "غرينتش+٠١:٠٠"sv }, + TestData { "ar"sv, Unicode::CalendarPatternStyle::Short, "Europe/London"sv, "غرينتش+١"sv }, + + TestData { "en"sv, Unicode::CalendarPatternStyle::Long, "Africa/Accra"sv, "Greenwich Mean Time"sv }, + TestData { "en"sv, Unicode::CalendarPatternStyle::Short, "Africa/Accra"sv, "GMT"sv }, + + TestData { "ar"sv, Unicode::CalendarPatternStyle::Long, "Africa/Accra"sv, "توقيت غرينتش"sv }, + TestData { "ar"sv, Unicode::CalendarPatternStyle::Short, "Africa/Accra"sv, "غرينتش"sv }, + }; + + constexpr auto sep_19_2022 = AK::Time::from_seconds(1663553728); // Monday, September 19, 2022 2:15:28 AM + + for (auto const& test : test_data) { + auto time_zone = Unicode::format_time_zone(test.locale, test.time_zone, test.style, sep_19_2022); + EXPECT_EQ(time_zone, test.expected_result); + } +} + TEST_CASE(format_time_zone_offset) { constexpr auto jan_1_1833 = AK::Time::from_seconds(-4323283200); // Tuesday, January 1, 1833 12:00:00 AM diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 5aedcdd71b8..99b87fe38c6 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -1057,8 +1057,10 @@ String time_zone_string(double time) auto tz_name = TimeZone::current_time_zone(); // Most implementations seem to prefer the long-form display name of the time zone. Not super important, but we may as well match that behavior. - if (auto long_name = Unicode::get_time_zone_name(Unicode::default_locale(), tz_name, Unicode::CalendarPatternStyle::Long); long_name.has_value()) - tz_name = long_name.release_value(); + if (auto maybe_offset = TimeZone::get_time_zone_offset(tz_name, AK::Time::from_milliseconds(time)); maybe_offset.has_value()) { + if (auto long_name = Unicode::get_time_zone_name(Unicode::default_locale(), tz_name, Unicode::CalendarPatternStyle::Long, maybe_offset->in_dst); long_name.has_value()) + tz_name = long_name.release_value(); + } // 7. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName. return String::formatted("{}{:02}{:02} ({})", offset_sign, offset_hour, offset_min, tz_name); diff --git a/Userland/Libraries/LibUnicode/DateTimeFormat.cpp b/Userland/Libraries/LibUnicode/DateTimeFormat.cpp index 9c635bb98da..8bfc53da654 100644 --- a/Userland/Libraries/LibUnicode/DateTimeFormat.cpp +++ b/Userland/Libraries/LibUnicode/DateTimeFormat.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -195,10 +194,10 @@ Optional __attribute__((weak)) get_calendar_weekday_symbol(StringVie Optional __attribute__((weak)) get_calendar_day_period_symbol(StringView, StringView, CalendarPatternStyle, DayPeriod) { return {}; } Optional __attribute__((weak)) get_calendar_day_period_symbol_for_hour(StringView, StringView, CalendarPatternStyle, u8) { return {}; } -Optional __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle) { return {}; } +Optional __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle, TimeZone::InDST) { return {}; } Optional __attribute__((weak)) get_time_zone_format(StringView) { return {}; } -static Optional format_time_zone_offset(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time) +static Optional format_time_zone_offset(StringView locale, CalendarPatternStyle style, i64 offset_seconds) { auto formats = get_time_zone_format(locale); if (!formats.has_value()) @@ -208,21 +207,18 @@ static Optional format_time_zone_offset(StringView locale, StringView ti if (!number_system.has_value()) return {}; - auto offset = TimeZone::get_time_zone_offset(time_zone, time); - if (!offset.has_value()) - return {}; - if (offset->seconds == 0) + if (offset_seconds == 0) return formats->gmt_zero_format; - auto sign = offset->seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign; - auto separator = offset->seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator; - offset->seconds = llabs(offset->seconds); + auto sign = offset_seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign; + auto separator = offset_seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator; + offset_seconds = llabs(offset_seconds); - auto offset_hours = offset->seconds / 3'600; - offset->seconds %= 3'600; + auto offset_hours = offset_seconds / 3'600; + offset_seconds %= 3'600; - auto offset_minutes = offset->seconds / 60; - offset->seconds %= 60; + auto offset_minutes = offset_seconds / 60; + offset_seconds %= 60; StringBuilder builder; builder.append(sign); @@ -231,8 +227,8 @@ static Optional format_time_zone_offset(StringView locale, StringView ti // The long format always uses 2-digit hours field and minutes field, with optional 2-digit seconds field. case CalendarPatternStyle::LongOffset: builder.appendff("{:02}{}{:02}", offset_hours, separator, offset_minutes); - if (offset->seconds > 0) - builder.appendff("{}{:02}", separator, offset->seconds); + if (offset_seconds > 0) + builder.appendff("{}{:02}", separator, offset_seconds); break; // The short format is intended for the shortest representation and uses hour fields without leading zero, with optional 2-digit minutes and seconds fields. @@ -240,8 +236,8 @@ static Optional format_time_zone_offset(StringView locale, StringView ti builder.appendff("{}", offset_hours); if (offset_minutes > 0) { builder.appendff("{}{:02}", separator, offset_minutes); - if (offset->seconds > 0) - builder.appendff("{}{:02}", separator, offset->seconds); + if (offset_seconds > 0) + builder.appendff("{}{:02}", separator, offset_seconds); } break; @@ -257,18 +253,22 @@ static Optional format_time_zone_offset(StringView locale, StringView ti // https://unicode.org/reports/tr35/tr35-dates.html#Time_Zone_Format_Terminology String format_time_zone(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time) { + auto offset = TimeZone::get_time_zone_offset(time_zone, time); + if (!offset.has_value()) + return time_zone; + switch (style) { case CalendarPatternStyle::Short: case CalendarPatternStyle::Long: case CalendarPatternStyle::ShortGeneric: case CalendarPatternStyle::LongGeneric: - if (auto name = get_time_zone_name(locale, time_zone, style); name.has_value()) + if (auto name = get_time_zone_name(locale, time_zone, style, offset->in_dst); name.has_value()) return *name; break; case CalendarPatternStyle::ShortOffset: case CalendarPatternStyle::LongOffset: - return format_time_zone_offset(locale, time_zone, style, time).value_or(time_zone); + return format_time_zone_offset(locale, style, offset->seconds).value_or(time_zone); default: VERIFY_NOT_REACHED(); diff --git a/Userland/Libraries/LibUnicode/DateTimeFormat.h b/Userland/Libraries/LibUnicode/DateTimeFormat.h index b84a3f8cdaf..5adb2ecec7d 100644 --- a/Userland/Libraries/LibUnicode/DateTimeFormat.h +++ b/Userland/Libraries/LibUnicode/DateTimeFormat.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace Unicode { @@ -210,7 +211,7 @@ Optional get_calendar_day_period_symbol(StringView locale, StringVie Optional get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour); String format_time_zone(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time); -Optional get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style); +Optional get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style, TimeZone::InDST in_dst); Optional get_time_zone_format(StringView locale); }