From 91e3a2aaca740e70279508d17f5f12805ca78bc7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 1 Aug 2022 14:39:41 -0400 Subject: [PATCH] LibC: Cache the time zone name found by tzset() The time zone name will be needed for TZDB lookups in various time.h functions. Cache the value found by tzset(), defaulting to the system- wide default of UTC. This also moves the time.h global definitions to the top of the file. The cached time zone name will be needed above where these variables are defined, so this is just to keep them all together. --- Userland/Libraries/LibC/time.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibC/time.cpp b/Userland/Libraries/LibC/time.cpp index ce19dbd83f..f43b8ad32f 100644 --- a/Userland/Libraries/LibC/time.cpp +++ b/Userland/Libraries/LibC/time.cpp @@ -25,6 +25,16 @@ extern "C" { +static constexpr char const* __utc = "UTC"; +static StringView __tzname { __utc, __builtin_strlen(__utc) }; +static char __tzname_standard[TZNAME_MAX]; +static char __tzname_daylight[TZNAME_MAX]; + +long timezone = 0; +long altzone = 0; +char* tzname[2] = { const_cast(__utc), const_cast(__utc) }; +int daylight = 0; + time_t time(time_t* tloc) { struct timeval tv; @@ -362,35 +372,25 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st return fits ? str.length() : 0; } -static char __tzname_standard[TZNAME_MAX]; -static char __tzname_daylight[TZNAME_MAX]; -constexpr char const* __utc = "UTC"; - -long timezone = 0; -long altzone = 0; -char* tzname[2] = { const_cast(__utc), const_cast(__utc) }; -int daylight = 0; - void tzset() { // FIXME: Actually parse the TZ environment variable, described here: // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08 - StringView time_zone; - if (char* tz = getenv("TZ"); tz != nullptr) - time_zone = { tz, strlen(tz) }; + __tzname = { tz, strlen(tz) }; else - time_zone = TimeZone::system_time_zone(); + __tzname = TimeZone::system_time_zone(); auto set_default_values = []() { timezone = 0; altzone = 0; daylight = 0; + __tzname = StringView { __utc, __builtin_strlen(__utc) }; tzname[0] = const_cast(__utc); tzname[1] = const_cast(__utc); }; - if (auto offsets = TimeZone::get_named_time_zone_offsets(time_zone, AK::Time::now_realtime()); offsets.has_value()) { + if (auto offsets = TimeZone::get_named_time_zone_offsets(__tzname, AK::Time::now_realtime()); offsets.has_value()) { if (!offsets->at(0).name.copy_characters_to_buffer(__tzname_standard, TZNAME_MAX)) return set_default_values(); if (!offsets->at(1).name.copy_characters_to_buffer(__tzname_daylight, TZNAME_MAX))