From 2763fb45f70f6d00dd077d2848006741d34fd251 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 17 Nov 2023 10:54:52 -0500 Subject: [PATCH] LibTimeZone: Handle systems with varying zoneinfo file locations On my mac, the zoneinfo location is /usr/share/zoneinfo.default. This breaks our implementation for finding the system time zone, which was assuming the zoneinfo location contains "/zoneinfo/". This patch makes the implementation a bit more lenient. This didn't break tests on CI because we fallback to UTC, which is the time zone CI machines are in anyways. --- Userland/Libraries/LibTimeZone/TimeZone.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp index 84959efecb..4a1ef8a3b3 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.cpp +++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp @@ -106,15 +106,20 @@ StringView current_time_zone() #ifdef AK_OS_SERENITY return system_time_zone(); #else - static constexpr auto zoneinfo = "/zoneinfo/"sv; + static constexpr auto zoneinfo = "/zoneinfo"sv; + char* real_path = realpath("/etc/localtime", nullptr); ScopeGuard free_path = [real_path]() { free(real_path); }; if (real_path) { auto time_zone = StringView { real_path, strlen(real_path) }; + // The zoneinfo file may be located in paths like /usr/share/zoneinfo/ or /usr/share/zoneinfo.default/. + // We want to strip any such prefix from the path to arrive at the time zone name. if (auto index = time_zone.find(zoneinfo); index.has_value()) time_zone = time_zone.substring_view(*index + zoneinfo.length()); + if (auto index = time_zone.find('/'); index.has_value()) + time_zone = time_zone.substring_view(*index + 1); if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value()) return *maybe_time_zone;