Fix tzalloc(nullptr) and add a test.
This works (by reading /etc/localtime) on NetBSD, but not on Android
since we have no such file. Fix that by using our equivalent system
property instead.
Also s/time zone/timezone/ in documentation and comments. We've always
been inconsistent about this (as is upstream in code comments and
documentation) but it seems especially odd now we expose a _type_ that
spells it "timezone" to talk of "time zone" even as we're describing
that type and its associated functions.
Bug: https://github.com/chronotope/chrono/issues/499
Test: treehugger
Change-Id: I142995a3ab4deff1073a0aa9e63ce8eac850b93d
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index bef51a9..abf6ce0 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -97,7 +97,7 @@
static void* gmtime_no_stack_overflow_14313703_fn(void*) {
const char* original_tz = getenv("TZ");
- // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
+ // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist.
setenv("TZ", "gmtime_stack_overflow_14313703", 1);
tzset();
if (original_tz != nullptr) {
@@ -324,7 +324,7 @@
// According to C language specification the only tm struct field needed to
// find out replacement for %z and %Z in strftime is tm_isdst. Which is
-// wrong, as time zones change their standard offset and even DST savings.
+// wrong, as timezones change their standard offset and even DST savings.
// tzcode deviates from C language specification and requires tm struct either
// to be output of localtime-like functions or to be modified by mktime call
// before passing to strftime. See tz mailing discussion for more details
@@ -560,7 +560,7 @@
EXPECT_EQ(1, tm.tm_isdst);
EXPECT_EQ(3600, tm.tm_gmtoff);
- // And as long as we're in Europe/Berlin, those are the only time zone
+ // And as long as we're in Europe/Berlin, those are the only timezone
// abbreviations that are recognized.
tm = {};
ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
@@ -1118,7 +1118,7 @@
// Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
// http://b/31848040
- // This isn't a great test, because very few time zones were actually affected, and there's
+ // This isn't a great test, because very few timezones were actually affected, and there's
// no real logic to which ones were affected: it was just a coincidence of the data that came
// after them in the tzdata file.
@@ -1159,10 +1159,10 @@
TEST(time, bug_31339449) {
// POSIX says localtime acts as if it calls tzset.
// tzset does two things:
- // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
+ // 1. it sets the timezone ctime/localtime/mktime/strftime will use.
// 2. it sets the global `tzname`.
// POSIX says localtime_r need not set `tzname` (2).
- // Q: should localtime_r set the time zone (1)?
+ // Q: should localtime_r set the timezone (1)?
// Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
// Pick a time, any time...
@@ -1361,7 +1361,7 @@
ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm));
AssertTmEq(tm, 17);
- // Just check that mktime()'s time zone didn't change.
+ // Just check that mktime()'s timezone didn't change.
tm = {};
ASSERT_EQ(&tm, localtime_r(&t, &tm));
ASSERT_EQ(0, tm.tm_hour);
@@ -1403,7 +1403,7 @@
tm = {.tm_year = 93, .tm_mday = 1};
ASSERT_EQ(725814000, mktime_z(seoul, &tm));
- // Just check that mktime()'s time zone didn't change.
+ // Just check that mktime()'s timezone didn't change.
tm = {.tm_year = 93, .tm_mday = 1};
ASSERT_EQ(725875200, mktime(&tm));
@@ -1416,3 +1416,39 @@
GTEST_SKIP() << "glibc doesn't have timezone_t";
#endif
}
+
+TEST(time, tzalloc_nullptr) {
+#if __BIONIC__
+ // tzalloc(nullptr) returns the system timezone.
+ timezone_t default_tz = tzalloc(nullptr);
+ ASSERT_NE(nullptr, default_tz);
+
+ // Check that mktime_z() with the default timezone matches mktime().
+ // This assumes that the system timezone doesn't change during the test,
+ // but that should be unlikely, and we don't have much choice if we
+ // want to write a test at all.
+ // We unset $TZ before calling mktime() because mktime() honors $TZ.
+ unsetenv("TZ");
+ struct tm tm = {.tm_year = 93, .tm_mday = 1};
+ time_t t = mktime(&tm);
+ ASSERT_EQ(t, mktime_z(default_tz, &tm));
+
+ // Check that changing $TZ doesn't affect the tzalloc() default in
+ // the same way it would the mktime() default.
+ setenv("TZ", "America/Los_Angeles", 1);
+ tzset();
+ ASSERT_EQ(t, mktime_z(default_tz, &tm));
+
+ setenv("TZ", "Europe/London", 1);
+ tzset();
+ ASSERT_EQ(t, mktime_z(default_tz, &tm));
+
+ setenv("TZ", "Asia/Seoul", 1);
+ tzset();
+ ASSERT_EQ(t, mktime_z(default_tz, &tm));
+
+ tzfree(default_tz);
+#else
+ GTEST_SKIP() << "glibc doesn't have timezone_t";
+#endif
+}