summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-10 12:45:16 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-11 00:36:45 +0100
commit1c2c98ac5d09cc8c448067fd3fe90f234b495480 (patch)
tree10bf79773922926c07a5e9d6595cd5b9a2ee25fe
parent14535fb67a5cee71b07a83e9f761132107284403 (diff)
downloadserenity-1c2c98ac5d09cc8c448067fd3fe90f234b495480.zip
LibTimeZone: Add method to convert a time zone to a string
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp5
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h30
-rw-r--r--Tests/LibTimeZone/TestTimeZone.cpp16
-rw-r--r--Userland/Libraries/LibTimeZone/TimeZone.cpp1
-rw-r--r--Userland/Libraries/LibTimeZone/TimeZone.h1
5 files changed, 51 insertions, 2 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp
index cded014ffe..726e4ee8b4 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp
+++ b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp
@@ -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"~~~(
}
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h
index f2ec07ce86..669ace5c79 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h
+++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h
@@ -364,6 +364,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 = {})
{
quick_sort(values, [](auto const& value1, auto const& value2) { return value1.to_lowercase() < value2.to_lowercase(); });
diff --git a/Tests/LibTimeZone/TestTimeZone.cpp b/Tests/LibTimeZone/TestTimeZone.cpp
index 57f9f9f163..01f805872f 100644
--- a/Tests/LibTimeZone/TestTimeZone.cpp
+++ b/Tests/LibTimeZone/TestTimeZone.cpp
@@ -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
diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp
index 479aad1ddf..fc1fc6065d 100644
--- a/Userland/Libraries/LibTimeZone/TimeZone.cpp
+++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp
@@ -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 {}; }
}
diff --git a/Userland/Libraries/LibTimeZone/TimeZone.h b/Userland/Libraries/LibTimeZone/TimeZone.h
index 552755dfee..63cc1e49a9 100644
--- a/Userland/Libraries/LibTimeZone/TimeZone.h
+++ b/Userland/Libraries/LibTimeZone/TimeZone.h
@@ -13,5 +13,6 @@
namespace TimeZone {
Optional<TimeZone> time_zone_from_string(StringView time_zone);
+StringView time_zone_to_string(TimeZone time_zone);
}