1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

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.
This commit is contained in:
Timothy Flynn 2023-11-17 10:54:52 -05:00 committed by Andreas Kling
parent 6c8ab1ca0d
commit 2763fb45f7

View file

@ -106,15 +106,20 @@ StringView current_time_zone()
#ifdef AK_OS_SERENITY #ifdef AK_OS_SERENITY
return system_time_zone(); return system_time_zone();
#else #else
static constexpr auto zoneinfo = "/zoneinfo/"sv; static constexpr auto zoneinfo = "/zoneinfo"sv;
char* real_path = realpath("/etc/localtime", nullptr); char* real_path = realpath("/etc/localtime", nullptr);
ScopeGuard free_path = [real_path]() { free(real_path); }; ScopeGuard free_path = [real_path]() { free(real_path); };
if (real_path) { if (real_path) {
auto time_zone = StringView { real_path, strlen(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()) if (auto index = time_zone.find(zoneinfo); index.has_value())
time_zone = time_zone.substring_view(*index + zoneinfo.length()); 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()) if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
return *maybe_time_zone; return *maybe_time_zone;