LibJS+LibUnicode: Return the appropriate time zone name depending on DST

This commit is contained in:
Timothy Flynn 2022-01-19 14:54:19 -05:00 committed by Linus Groh
parent bf677eb485
commit 4400150cd2
Notes: sideshowbarker 2024-07-18 02:47:59 +09:00
5 changed files with 79 additions and 26 deletions

View file

@ -2207,17 +2207,17 @@ static TimeZoneNames const* find_time_zone_names(StringView locale, StringView t
return &s_time_zones[time_zone_index];
}
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style)
Optional<StringView> 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;

View file

@ -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

View file

@ -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);

View file

@ -6,7 +6,6 @@
#include <AK/Array.h>
#include <AK/StringBuilder.h>
#include <LibTimeZone/TimeZone.h>
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/NumberFormat.h>
@ -195,10 +194,10 @@ Optional<StringView> __attribute__((weak)) get_calendar_weekday_symbol(StringVie
Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol(StringView, StringView, CalendarPatternStyle, DayPeriod) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol_for_hour(StringView, StringView, CalendarPatternStyle, u8) { return {}; }
Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle) { return {}; }
Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle, TimeZone::InDST) { return {}; }
Optional<TimeZoneFormat> __attribute__((weak)) get_time_zone_format(StringView) { return {}; }
static Optional<String> format_time_zone_offset(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time)
static Optional<String> 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<String> 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<String> 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<String> 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<String> 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();

View file

@ -12,6 +12,7 @@
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibTimeZone/TimeZone.h>
#include <LibUnicode/Forward.h>
namespace Unicode {
@ -210,7 +211,7 @@ Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringVie
Optional<StringView> 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<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style);
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style, TimeZone::InDST in_dst);
Optional<TimeZoneFormat> get_time_zone_format(StringView locale);
}