summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-01-11 20:16:15 +0100
committerLinus Groh <mail@linusgroh.de>2022-01-11 22:17:39 +0100
commit205d63c3f0585ef56591c6e64cacf7ab435c5699 (patch)
tree18ee99419537d9fb85fb99ba39f3923974bc8b19
parentf1276144ba36c12cf3750cbf2320e79e52fbeb59 (diff)
downloadserenity-205d63c3f0585ef56591c6e64cacf7ab435c5699.zip
LibTimeZone: Operate in UTC-only mode when !ENABLE_TIME_ZONE_DATA
Instead of only having dummy functions that don't work with any input, let's at least support one time zone: 'UTC'. This matches the basic Temporal implementation for engines without ECMA-262, for example.
-rw-r--r--Tests/LibTimeZone/TestTimeZone.cpp20
-rw-r--r--Userland/Libraries/LibTimeZone/TimeZone.cpp41
2 files changed, 58 insertions, 3 deletions
diff --git a/Tests/LibTimeZone/TestTimeZone.cpp b/Tests/LibTimeZone/TestTimeZone.cpp
index d93fbd5c1e..50efd28a25 100644
--- a/Tests/LibTimeZone/TestTimeZone.cpp
+++ b/Tests/LibTimeZone/TestTimeZone.cpp
@@ -126,4 +126,24 @@ TEST_CASE(get_time_zone_offset)
EXPECT(!TimeZone::get_time_zone_offset("I don't exist"sv, {}).has_value());
}
+#else
+
+TEST_CASE(time_zone_from_string)
+{
+ EXPECT_EQ(TimeZone::time_zone_from_string("UTC"sv), TimeZone::TimeZone::UTC);
+
+ EXPECT(!TimeZone::time_zone_from_string("Europe/Paris"sv).has_value());
+ EXPECT(!TimeZone::time_zone_from_string("Etc/UTC"sv).has_value());
+ EXPECT(!TimeZone::time_zone_from_string("I don't exist"sv).has_value());
+}
+
+TEST_CASE(get_time_zone_offset)
+{
+ EXPECT_EQ(TimeZone::get_time_zone_offset("UTC", AK::Time::from_seconds(123456)), 0);
+
+ EXPECT(!TimeZone::get_time_zone_offset("Europe/Paris"sv, {}).has_value());
+ EXPECT(!TimeZone::get_time_zone_offset("Etc/UTC"sv, {}).has_value());
+ EXPECT(!TimeZone::get_time_zone_offset("I don't exist"sv, {}).has_value());
+}
+
#endif
diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp
index d74fe6e034..b87f11fc3a 100644
--- a/Userland/Libraries/LibTimeZone/TimeZone.cpp
+++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp
@@ -8,8 +8,35 @@
namespace TimeZone {
-Optional<TimeZone> __attribute__((weak)) time_zone_from_string(StringView) { return {}; }
-StringView __attribute__((weak)) time_zone_to_string(TimeZone) { return {}; }
+// NOTE: Without ENABLE_TIME_ZONE_DATA LibTimeZone operates in a UTC-only mode and only recognizes
+// the 'UTC' time zone, which is slightly more useful than a bunch of dummy functions that
+// can't do anything. When we build with time zone data, these weakly linked functions are
+// replaced with their proper counterparts.
+
+#if !ENABLE_TIME_ZONE_DATA
+enum class TimeZone : u16 {
+ UTC,
+};
+#endif
+
+Optional<TimeZone> __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone)
+{
+#if !ENABLE_TIME_ZONE_DATA
+ if (time_zone.equals_ignoring_case("UTC"sv))
+ return TimeZone::UTC;
+#endif
+ return {};
+}
+
+StringView __attribute__((weak)) time_zone_to_string([[maybe_unused]] TimeZone time_zone)
+{
+#if !ENABLE_TIME_ZONE_DATA
+ VERIFY(time_zone == TimeZone::UTC);
+ return "UTC"sv;
+#else
+ return {};
+#endif
+}
Optional<StringView> canonicalize_time_zone(StringView time_zone)
{
@@ -24,7 +51,15 @@ Optional<StringView> canonicalize_time_zone(StringView time_zone)
return canonical_time_zone;
}
-Optional<i64> __attribute__((weak)) get_time_zone_offset(TimeZone, AK::Time) { return {}; }
+Optional<i64> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::Time)
+{
+#if !ENABLE_TIME_ZONE_DATA
+ VERIFY(time_zone == TimeZone::UTC);
+ return 0;
+#else
+ return {};
+#endif
+}
Optional<i64> get_time_zone_offset(StringView time_zone, AK::Time time)
{