]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
libstdc++: Implement std::chrono::current_zone() for AIX [PR108409]
authorJonathan Wakely <jwakely@redhat.com>
Sat, 14 Jan 2023 20:13:32 +0000 (20:13 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 14 Jan 2023 20:49:45 +0000 (20:49 +0000)
libstdc++-v3/ChangeLog:

PR libstdc++/108409
* src/c++20/tzdb.cc (current_zone()) [_AIX]: Use TZ environment
variable.

libstdc++-v3/src/c++20/tzdb.cc

index a37859dffdb9846f06bd50c42869f537bfc86a20..eaaea367f4fb251916c67b61f78bb0de9a74c9aa 100644 (file)
 #include <mutex>      // mutex
 #include <filesystem> // filesystem::read_symlink
 
+#ifndef _AIX
+# include <cstdlib>   // getenv
+#endif
+
 #ifndef __GTHREADS
 # define USE_ATOMIC_SHARED_PTR 0
 #elif _WIN32
@@ -1625,6 +1629,7 @@ namespace std::chrono
   {
     // TODO cache this function's result?
 
+#ifndef _AIX
     error_code ec;
     // This should be a symlink to e.g. /usr/share/zoneinfo/Europe/London
     auto path = filesystem::read_symlink("/etc/localtime", ec);
@@ -1655,11 +1660,25 @@ namespace std::chrono
          if (auto tz = do_locate_zone(this->zones, this->links, name))
            return tz;
       }
-
-    // TODO AIX stores current zone in $TZ in /etc/environment but the value
+#else
+    // AIX stores current zone in $TZ in /etc/environment but the value
     // is typically a POSIX time zone name, not IANA zone.
     // https://developer.ibm.com/articles/au-aix-posix/
     // https://www.ibm.com/support/pages/managing-time-zone-variable-posix
+    if (const char* env = std::getenv("TZ"))
+      {
+       string_view s(env);
+       if (s == "GMT0")
+         s = "Etc/GMT";
+       else if (s.size() == 4 && s[3] == '0')
+         s = "Etc/UTC";
+
+       // This will fail unless TZ contains an IANA time zone name,
+       // or one of the special cases above.
+       if (auto tz = do_locate_zone(this->zones, this->links, s))
+         return tz;
+      }
+#endif
 
     __throw_runtime_error("tzdb: cannot determine current zone");
   }