LibTimeZone: Add method to convert a time zone to a string

This commit is contained in:
Timothy Flynn 2022-01-10 12:45:16 -05:00 committed by Linus Groh
parent 14535fb67a
commit 1c2c98ac5d
Notes: sideshowbarker 2024-07-17 21:13:24 +09:00
5 changed files with 51 additions and 2 deletions

View file

@ -231,7 +231,7 @@ static void generate_time_zone_data_implementation(Core::File& file, TimeZoneDat
namespace TimeZone {
)~~~");
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, auto const& aliases) {
auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values, auto const& aliases) {
HashValueMap<String> hashes;
hashes.ensure_capacity(values.size());
@ -248,9 +248,10 @@ namespace TimeZone {
options.sensitivity = CaseSensitivity::CaseInsensitive;
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
generate_value_to_string(generator, "{}_to_string"sv, enum_title, enum_snake, format_identifier, values);
};
append_from_string("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases);
append_string_conversions("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases);
generator.append(R"~~~(
}

View file

@ -363,6 +363,36 @@ Optional<@return_type@> @method_name@(StringView key)
)~~~");
}
template<typename IdentifierFormatter>
void generate_value_to_string(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, IdentifierFormatter&& format_identifier, Span<String const> values)
{
generator.set("method_name", String::formatted(method_name_format, value_name));
generator.set("value_type", value_type);
generator.set("value_name", value_name);
generator.append(R"~~~(
StringView @method_name@(@value_type@ @value_name@)
{
using enum @value_type@;
switch (@value_name@) {)~~~");
for (auto const& value : values) {
generator.set("enum_value", format_identifier(value_type, value));
generator.set("string_value", value);
generator.append(R"~~~(
case @enum_value@:
return "@string_value@"sv;)~~~");
}
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
)~~~");
}
template<typename IdentifierFormatter>
void generate_enum(SourceGenerator& generator, IdentifierFormatter&& format_identifier, StringView name, StringView default_, Vector<String>& values, Vector<Alias> aliases = {})
{

View file

@ -53,4 +53,20 @@ TEST_CASE(case_insensitive_time_zone_from_string)
EXPECT_EQ(TimeZone::time_zone_from_string("uTc"sv), TimeZone::TimeZone::UTC);
}
TEST_CASE(time_zone_to_string)
{
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::America_New_York), "America/New_York"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Europe_Paris), "Europe/Paris"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_GMT_Ahead_2), "Etc/GMT+2"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_GMT_Behind_5), "Etc/GMT-5"sv);
}
TEST_CASE(time_zone_to_string_link)
{
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_UTC), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::UTC), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Universal), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_Universal), "Etc/UTC"sv);
}
#endif

View file

@ -9,5 +9,6 @@
namespace TimeZone {
Optional<TimeZone> __attribute__((weak)) time_zone_from_string(StringView) { return {}; }
StringView __attribute__((weak)) time_zone_to_string(TimeZone) { return {}; }
}

View file

@ -13,5 +13,6 @@
namespace TimeZone {
Optional<TimeZone> time_zone_from_string(StringView time_zone);
StringView time_zone_to_string(TimeZone time_zone);
}