about summary refs log tree commit diff
path: root/absl/time/internal/cctz/src/time_zone_lookup.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/time/internal/cctz/src/time_zone_lookup.cc')
-rw-r--r--absl/time/internal/cctz/src/time_zone_lookup.cc33
1 files changed, 20 insertions, 13 deletions
diff --git a/absl/time/internal/cctz/src/time_zone_lookup.cc b/absl/time/internal/cctz/src/time_zone_lookup.cc
index fd04e2df3985..a27bfc1353a3 100644
--- a/absl/time/internal/cctz/src/time_zone_lookup.cc
+++ b/absl/time/internal/cctz/src/time_zone_lookup.cc
@@ -23,6 +23,7 @@
 
 #if defined(__APPLE__)
 #include <CoreFoundation/CFTimeZone.h>
+#include <vector>
 #endif
 
 #include <cstdlib>
@@ -121,25 +122,33 @@ time_zone fixed_time_zone(const seconds& offset) {
 
 time_zone local_time_zone() {
   const char* zone = ":localtime";
+#if defined(__ANDROID__)
+  char sysprop[PROP_VALUE_MAX];
+  if (__system_property_get("persist.sys.timezone", sysprop) > 0) {
+    zone = sysprop;
+  }
+#endif
+#if defined(__APPLE__)
+  std::vector<char> buffer;
+  CFTimeZoneRef tz_default = CFTimeZoneCopyDefault();
+  if (CFStringRef tz_name = CFTimeZoneGetName(tz_default)) {
+    CFStringEncoding encoding = kCFStringEncodingUTF8;
+    CFIndex length = CFStringGetLength(tz_name);
+    buffer.resize(CFStringGetMaximumSizeForEncoding(length, encoding) + 1);
+    if (CFStringGetCString(tz_name, &buffer[0], buffer.size(), encoding)) {
+      zone = &buffer[0];
+    }
+  }
+  CFRelease(tz_default);
+#endif
 
   // Allow ${TZ} to override to default zone.
   char* tz_env = nullptr;
 #if defined(_MSC_VER)
   _dupenv_s(&tz_env, nullptr, "TZ");
-#elif defined(__APPLE__)
-  CFTimeZoneRef system_time_zone = CFTimeZoneCopyDefault();
-  CFStringRef tz_name = CFTimeZoneGetName(system_time_zone);
-  tz_env = strdup(CFStringGetCStringPtr(tz_name, CFStringGetSystemEncoding()));
-  CFRelease(system_time_zone);
 #else
   tz_env = std::getenv("TZ");
 #endif
-#if defined(__ANDROID__)
-  char sysprop[PROP_VALUE_MAX];
-  if (tz_env == nullptr)
-    if (__system_property_get("persist.sys.timezone", sysprop) > 0)
-      tz_env = sysprop;
-#endif
   if (tz_env) zone = tz_env;
 
   // We only support the "[:]<zone-name>" form.
@@ -163,8 +172,6 @@ time_zone local_time_zone() {
 #if defined(_MSC_VER)
   free(localtime_env);
   free(tz_env);
-#elif defined(__APPLE__)
-  free(tz_env);
 #endif
 
   time_zone tz;