Skip to content

Commit 93f8d10

Browse files
authored
Update per-platform support for determining the local time zone (#101)
Treat the Apple Core Foundation "default time zone" and the Android "persist.sys.timezone" property consistently as initial values for the local time zone (overriding ":localtime"). Then allow ${TZ} to override that on all platforms. The only slightly confusing thing is CFTimeZoneCopyDefault() appears to also heed ${TZ} internally, but that's OK.
1 parent 157a94a commit 93f8d10

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/time_zone_lookup.cc

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
#if defined(__APPLE__)
2525
#include <CoreFoundation/CFTimeZone.h>
26+
#include <vector>
2627
#endif
2728

2829
#include <cstdlib>
2930
#include <cstring>
3031
#include <string>
31-
#include <vector>
3232

3333
#include "time_zone_fixed.h"
3434
#include "time_zone_impl.h"
@@ -120,31 +120,32 @@ time_zone fixed_time_zone(const seconds& offset) {
120120

121121
time_zone local_time_zone() {
122122
const char* zone = ":localtime";
123-
124-
// Allow ${TZ} to override to default zone.
125-
char* tz_env = nullptr;
126-
#if defined(_MSC_VER)
127-
_dupenv_s(&tz_env, nullptr, "TZ");
128-
#elif defined(__APPLE__)
129-
CFTimeZoneRef system_time_zone = CFTimeZoneCopyDefault();
130-
if (CFStringRef tz_name = CFTimeZoneGetName(system_time_zone)) {
123+
#if defined(__ANDROID__)
124+
char sysprop[PROP_VALUE_MAX];
125+
if (__system_property_get("persist.sys.timezone", sysprop) > 0) {
126+
zone = sysprop;
127+
}
128+
#endif
129+
#if defined(__APPLE__)
130+
std::vector<char> buffer;
131+
CFTimeZoneRef tz_default = CFTimeZoneCopyDefault();
132+
if (CFStringRef tz_name = CFTimeZoneGetName(tz_default)) {
131133
CFStringEncoding encoding = kCFStringEncodingUTF8;
132134
CFIndex length = CFStringGetLength(tz_name);
133-
CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, encoding);
134-
std::vector<char> buffer(max_size + 1);
135+
buffer.resize(CFStringGetMaximumSizeForEncoding(length, encoding) + 1);
135136
if (CFStringGetCString(tz_name, &buffer[0], buffer.size(), encoding)) {
136-
tz_env = strdup(&buffer[0]);
137+
zone = &buffer[0];
137138
}
138139
}
139-
CFRelease(system_time_zone);
140+
CFRelease(tz_default);
141+
#endif
142+
143+
// Allow ${TZ} to override to default zone.
144+
char* tz_env = nullptr;
145+
#if defined(_MSC_VER)
146+
_dupenv_s(&tz_env, nullptr, "TZ");
140147
#else
141148
tz_env = std::getenv("TZ");
142-
#endif
143-
#if defined(__ANDROID__)
144-
char sysprop[PROP_VALUE_MAX];
145-
if (tz_env == nullptr)
146-
if (__system_property_get("persist.sys.timezone", sysprop) > 0)
147-
tz_env = sysprop;
148149
#endif
149150
if (tz_env) zone = tz_env;
150151

@@ -169,8 +170,6 @@ time_zone local_time_zone() {
169170
#if defined(_MSC_VER)
170171
free(localtime_env);
171172
free(tz_env);
172-
#elif defined(__APPLE__)
173-
free(tz_env);
174173
#endif
175174

176175
time_zone tz;

0 commit comments

Comments
 (0)