Kaynağa Gözat

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.
Timothy Flynn 3 yıl önce
ebeveyn
işleme
91e3a2aaca
1 değiştirilmiş dosya ile 14 ekleme ve 14 silme
  1. 14 14
      Userland/Libraries/LibC/time.cpp

+ 14 - 14
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<char*>(__utc), const_cast<char*>(__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<char*>(__utc), const_cast<char*>(__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<char*>(__utc);
         tzname[1] = const_cast<char*>(__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))