mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:47:35 +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:
parent
6c8ab1ca0d
commit
2763fb45f7
1 changed files with 6 additions and 1 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue