From da27937144f1d23bbca39c4e2f7a7bc1484b2dc1 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 19 Jan 2022 18:05:31 -0500 Subject: [PATCH] LibTimeZone: Add an API to retrieve a list of all known IANA time zones --- .../LibTimeZone/GenerateTimeZoneData.cpp | 16 ++++++++++++++++ Userland/Libraries/LibTimeZone/TimeZone.cpp | 10 ++++++++++ Userland/Libraries/LibTimeZone/TimeZone.h | 1 + 3 files changed, 27 insertions(+) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp index b83d5312b1e..b98dc3574e0 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp @@ -524,6 +524,22 @@ Optional get_time_zone_offset(TimeZone time_zone, AK::Time time) return dst_offset; } +Span all_time_zones() +{ + static constexpr auto all_time_zones = Array { + )~~~"); + + for (auto const& time_zone : time_zone_data.time_zone_names) { + generator.set("time_zone", time_zone); + generator.append("\"@time_zone@\"sv, "); + } + + generator.append(R"~~~( + }; + + return all_time_zones; +} + } )~~~"); diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp index c898ed365ad..f9953c53b27 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.cpp +++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp @@ -31,6 +31,16 @@ StringView current_time_zone() return canonicalize_time_zone(tzname[0]).value_or("UTC"sv); } +Span __attribute__((weak)) all_time_zones() +{ +#if !ENABLE_TIME_ZONE_DATA + static constexpr auto utc = Array { "UTC"sv }; + return utc; +#else + return {}; +#endif +} + Optional __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone) { #if !ENABLE_TIME_ZONE_DATA diff --git a/Userland/Libraries/LibTimeZone/TimeZone.h b/Userland/Libraries/LibTimeZone/TimeZone.h index 6a1e7b2a81e..493f89eb4d8 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.h +++ b/Userland/Libraries/LibTimeZone/TimeZone.h @@ -25,6 +25,7 @@ struct Offset { }; StringView current_time_zone(); +Span all_time_zones(); Optional time_zone_from_string(StringView time_zone); StringView time_zone_to_string(TimeZone time_zone);