blob: abf6ce08c77d32e8b59243435ed31d3f73bbb500 [file] [log] [blame]
Elliott Hughese0175ca2013-03-14 14:38:08 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes4b558f52014-03-04 15:58:02 -080017#include <time.h>
18
19#include <errno.h>
Elliott Hughese0175ca2013-03-14 14:38:08 -070020#include <gtest/gtest.h>
Elliott Hughes329103d2014-04-25 16:55:04 -070021#include <pthread.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080022#include <signal.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070023#include <sys/cdefs.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070024#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080025#include <sys/types.h>
26#include <sys/wait.h>
Yabin Cuid5c65272014-11-26 14:04:26 -080027#include <unistd.h>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070028
Yabin Cui95f1ee22015-01-13 19:53:15 -080029#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070030#include <chrono>
Elliott Hughes2bd43162023-06-15 13:17:08 -070031#include <thread>
Elliott Hughese0175ca2013-03-14 14:38:08 -070032
Elliott Hughes71ba5892018-02-07 12:44:45 -080033#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080034#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070035
Elliott Hughesca3f8e42019-10-28 15:59:38 -070036using namespace std::chrono_literals;
37
Mark Salyzyn0b846e82017-12-20 08:56:18 -080038TEST(time, time) {
39 // Acquire time
40 time_t p1, t1 = time(&p1);
41 // valid?
42 ASSERT_NE(static_cast<time_t>(0), t1);
43 ASSERT_NE(static_cast<time_t>(-1), t1);
44 ASSERT_EQ(p1, t1);
45
46 // Acquire time one+ second later
47 usleep(1010000);
48 time_t p2, t2 = time(&p2);
49 // valid?
50 ASSERT_NE(static_cast<time_t>(0), t2);
51 ASSERT_NE(static_cast<time_t>(-1), t2);
52 ASSERT_EQ(p2, t2);
53
54 // Expect time progression
55 ASSERT_LT(p1, p2);
56 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
57
58 // Expect nullptr call to produce same results
59 ASSERT_LE(t2, time(nullptr));
60 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
61}
62
Elliott Hughesee178bf2013-07-12 11:25:20 -070063TEST(time, gmtime) {
64 time_t t = 0;
65 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070066 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070067 ASSERT_EQ(0, broken_down->tm_sec);
68 ASSERT_EQ(0, broken_down->tm_min);
69 ASSERT_EQ(0, broken_down->tm_hour);
70 ASSERT_EQ(1, broken_down->tm_mday);
71 ASSERT_EQ(0, broken_down->tm_mon);
72 ASSERT_EQ(1970, broken_down->tm_year + 1900);
73}
Elliott Hughes7843d442013-08-22 11:37:32 -070074
Elliott Hughes5a29d542017-12-07 16:05:57 -080075TEST(time, gmtime_r) {
76 struct tm tm = {};
77 time_t t = 0;
78 struct tm* broken_down = gmtime_r(&t, &tm);
79 ASSERT_EQ(broken_down, &tm);
80 ASSERT_EQ(0, broken_down->tm_sec);
81 ASSERT_EQ(0, broken_down->tm_min);
82 ASSERT_EQ(0, broken_down->tm_hour);
83 ASSERT_EQ(1, broken_down->tm_mday);
84 ASSERT_EQ(0, broken_down->tm_mon);
85 ASSERT_EQ(1970, broken_down->tm_year + 1900);
86}
87
Almaz Mingaleevda75bb62022-06-29 15:47:37 +000088TEST(time, mktime_TZ_as_UTC_and_offset) {
89 struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1};
90
91 // This TZ value is not a valid Olson ID and is not present in tzdata file,
92 // but is a valid TZ string according to POSIX standard.
93 setenv("TZ", "UTC+08:00:00", 1);
94 tzset();
95 ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm));
96}
97
Elliott Hughes329103d2014-04-25 16:55:04 -070098static void* gmtime_no_stack_overflow_14313703_fn(void*) {
99 const char* original_tz = getenv("TZ");
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700100 // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist.
Elliott Hughes329103d2014-04-25 16:55:04 -0700101 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
102 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700103 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -0700104 setenv("TZ", original_tz, 1);
105 }
106 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700107 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -0700108}
109
110TEST(time, gmtime_no_stack_overflow_14313703) {
111 // Is it safe to call tzload on a thread with a small stack?
112 // http://b/14313703
113 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800114 pthread_attr_t a;
115 ASSERT_EQ(0, pthread_attr_init(&a));
116 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700117
118 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800120 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700121}
122
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900123TEST(time, mktime_empty_TZ) {
124 // tzcode used to have a bug where it didn't reinitialize some internal state.
125
126 // Choose a time where DST is set.
127 struct tm t;
128 memset(&t, 0, sizeof(tm));
129 t.tm_year = 1980 - 1900;
130 t.tm_mon = 6;
131 t.tm_mday = 2;
132
133 setenv("TZ", "America/Los_Angeles", 1);
134 tzset();
135 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
136
137 memset(&t, 0, sizeof(tm));
138 t.tm_year = 1980 - 1900;
139 t.tm_mon = 6;
140 t.tm_mday = 2;
141
142 setenv("TZ", "", 1); // Implies UTC.
143 tzset();
144 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
145}
146
Elliott Hughes7843d442013-08-22 11:37:32 -0700147TEST(time, mktime_10310929) {
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100148 struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10};
Elliott Hughes7843d442013-08-22 11:37:32 -0700149
Elliott Hughes0c401522013-10-18 16:21:54 -0700150#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700151 // 32-bit bionic has a signed 32-bit time_t.
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100152 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700153 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700154#else
155 // Everyone else should be using a signed 64-bit time_t.
156 ASSERT_GE(sizeof(time_t) * 8, 64U);
157
158 setenv("TZ", "America/Los_Angeles", 1);
159 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700160 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700161
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100162 // On the date/time specified by tm America/Los_Angeles
163 // follows DST. But tm_isdst is set to 0, which forces
164 // mktime to interpret that time as local standard, hence offset
165 // is 8 hours, not 7.
166 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700167 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700168#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800169}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800170
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700171TEST(time, mktime_EOVERFLOW) {
Elliott Hughesf52b2162023-05-19 16:09:47 -0700172 setenv("TZ", "UTC", 1);
173
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700174 struct tm t;
175 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700176
177 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
178 t.tm_year = 2016 - 1900;
179
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700180 t.tm_mon = 2;
181 t.tm_mday = 10;
182
183 errno = 0;
184 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
185 ASSERT_EQ(0, errno);
186
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100187 // This will overflow for LP32.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700188 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700189
190 errno = 0;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100191#if !defined(__LP64__)
192 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
193 ASSERT_EQ(EOVERFLOW, errno);
194#else
195 ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t));
196 ASSERT_EQ(0, errno);
197#endif
198
199 // This will overflow for LP32 or LP64.
200 // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is.
201 t.tm_year = INT_MAX;
202 t.tm_mon = 11;
203 t.tm_mday = 45;
204
205 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700206 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
207 ASSERT_EQ(EOVERFLOW, errno);
208}
209
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000210TEST(time, mktime_invalid_tm_TZ_combination) {
211 setenv("TZ", "UTC", 1);
212
213 struct tm t;
214 memset(&t, 0, sizeof(tm));
215 t.tm_year = 2022 - 1900;
216 t.tm_mon = 11;
217 t.tm_mday = 31;
218 // UTC does not observe DST
219 t.tm_isdst = 1;
220
221 errno = 0;
222
223 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
224 // mktime sets errno to EOVERFLOW if result is unrepresentable.
225 EXPECT_EQ(EOVERFLOW, errno);
226}
227
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100228// Transitions in the tzdata file are generated up to the year 2100. Testing
229// that dates beyond that are handled properly too.
230TEST(time, mktime_after_2100) {
231 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
232
233#if !defined(__LP64__)
234 // 32-bit bionic has a signed 32-bit time_t.
235 ASSERT_EQ(-1, mktime(&tm));
236 ASSERT_EQ(EOVERFLOW, errno);
237#else
238 setenv("TZ", "Europe/London", 1);
239 tzset();
240 errno = 0;
241
242 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
243 ASSERT_EQ(0, errno);
244#endif
245}
246
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700247TEST(time, strftime) {
248 setenv("TZ", "UTC", 1);
249
250 struct tm t;
251 memset(&t, 0, sizeof(tm));
252 t.tm_year = 200;
253 t.tm_mon = 2;
254 t.tm_mday = 10;
255
256 char buf[64];
257
258 // Seconds since the epoch.
259#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
260 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
261 EXPECT_STREQ("4108320000", buf);
262#endif
263
264 // Date and time as text.
265 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
266 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
267}
268
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000269TEST(time, strftime_second_before_epoch) {
270 setenv("TZ", "UTC", 1);
271
272 struct tm t;
273 memset(&t, 0, sizeof(tm));
274 t.tm_year = 1969 - 1900;
275 t.tm_mon = 11;
276 t.tm_mday = 31;
277 t.tm_hour = 23;
278 t.tm_min = 59;
279 t.tm_sec = 59;
280
281 char buf[64];
282
283 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
284 EXPECT_STREQ("-1", buf);
285}
286
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100287TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800288 // Netflix on Nexus Player wouldn't start (http://b/25170306).
289 struct tm t;
290 memset(&t, 0, sizeof(tm));
291
292 char buf[64];
293
294 setenv("TZ", "America/Los_Angeles", 1);
295 tzset();
296
297 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
298 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
299 EXPECT_STREQ("<PST>", buf);
300
301#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
302 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
303 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
304 EXPECT_STREQ("<PDT>", buf);
305
306 t.tm_isdst = -123; // "and negative if the information is not available".
307 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
308 EXPECT_STREQ("<>", buf);
309#endif
310
311 setenv("TZ", "UTC", 1);
312 tzset();
313
314 t.tm_isdst = 0;
315 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
316 EXPECT_STREQ("<UTC>", buf);
317
318#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
319 t.tm_isdst = 1; // UTC has no DST.
320 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
321 EXPECT_STREQ("<>", buf);
322#endif
323}
324
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100325// According to C language specification the only tm struct field needed to
326// find out replacement for %z and %Z in strftime is tm_isdst. Which is
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700327// wrong, as timezones change their standard offset and even DST savings.
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100328// tzcode deviates from C language specification and requires tm struct either
329// to be output of localtime-like functions or to be modified by mktime call
330// before passing to strftime. See tz mailing discussion for more details
331// https://mm.icann.org/pipermail/tz/2022-July/031674.html
332// But we are testing case when tm.tm_zone is null, which means that tm struct
333// is not coming from localtime and is neither modified by mktime. That's why
334// we are comparing against +0000, even though America/Los_Angeles never
335// observes it.
336TEST(time, strftime_z_null_tm_zone) {
337 char str[64];
338 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
339
340 setenv("TZ", "America/Los_Angeles", 1);
341 tzset();
342
343 tm.tm_zone = NULL;
344
345 size_t result = strftime(str, sizeof(str), "%z", &tm);
346
347 EXPECT_EQ(5U, result);
348 EXPECT_STREQ("+0000", str);
349
350 tm.tm_isdst = 1;
351
352 result = strftime(str, sizeof(str), "%z", &tm);
353
354 EXPECT_EQ(5U, result);
355 EXPECT_STREQ("+0000", str);
356
357 setenv("TZ", "UTC", 1);
358 tzset();
359
360 tm.tm_isdst = 0;
361
362 result = strftime(str, sizeof(str), "%z", &tm);
363
364 EXPECT_EQ(5U, result);
365 EXPECT_STREQ("+0000", str);
366
367 tm.tm_isdst = 1;
368
369 result = strftime(str, sizeof(str), "%z", &tm);
370
371 EXPECT_EQ(5U, result);
372 EXPECT_STREQ("+0000", str);
373}
374
375TEST(time, strftime_z_Europe_Lisbon) {
376 char str[64];
377 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
378 // tm_isdst is not set as it will be overridden by mktime call anyway.
379 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
380
381 setenv("TZ", "Europe/Lisbon", 1);
382 tzset();
383
384 // tzcode's strftime implementation for %z relies on prior mktime call.
385 // At the moment of writing %z value is taken from tm_gmtoff. So without
386 // mktime call %z is replaced with +0000.
387 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
388 mktime(&tm);
389
390 size_t result = strftime(str, sizeof(str), "%z", &tm);
391
392 EXPECT_EQ(5U, result);
393 EXPECT_STREQ("+0100", str);
394
395 // Now standard offset is 0.
396 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
397
398 mktime(&tm);
399 result = strftime(str, sizeof(str), "%z", &tm);
400
401 EXPECT_EQ(5U, result);
402 EXPECT_STREQ("+0000", str);
403}
404
Elliott Hughes0a610d02016-07-29 14:04:17 -0700405TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700406 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700407 locale_t old_locale = uselocale(cloc);
408
409 setenv("TZ", "UTC", 1);
410
411 struct tm t;
412 memset(&t, 0, sizeof(tm));
413 t.tm_year = 200;
414 t.tm_mon = 2;
415 t.tm_mday = 10;
416
417 // Date and time as text.
418 char buf[64];
419 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
420 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
421
422 uselocale(old_locale);
423 freelocale(cloc);
424}
425
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700426TEST(time, strptime) {
427 setenv("TZ", "UTC", 1);
428
429 struct tm t;
430 char buf[64];
431
432 memset(&t, 0, sizeof(t));
433 strptime("11:14", "%R", &t);
434 strftime(buf, sizeof(buf), "%H:%M", &t);
435 EXPECT_STREQ("11:14", buf);
436
437 memset(&t, 0, sizeof(t));
438 strptime("09:41:53", "%T", &t);
439 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
440 EXPECT_STREQ("09:41:53", buf);
441}
442
Elliott Hughes3376c232018-02-13 23:14:12 -0800443TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700444#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800445 setenv("TZ", "UTC", 1);
446
447 struct tm t;
448 char buf[64];
449
450 memset(&t, 0, sizeof(t));
451 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
452 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
453 EXPECT_STREQ("11:14", buf);
454
455 memset(&t, 0, sizeof(t));
456 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
457 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
458 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700459#else
460 GTEST_SKIP() << "musl doesn't support strptime_l";
461#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800462}
463
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700464TEST(time, strptime_F) {
465 setenv("TZ", "UTC", 1);
466
467 struct tm tm = {};
468 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
469 EXPECT_EQ(119, tm.tm_year);
470 EXPECT_EQ(2, tm.tm_mon);
471 EXPECT_EQ(26, tm.tm_mday);
472}
473
474TEST(time, strptime_P_p) {
475 setenv("TZ", "UTC", 1);
476
Elliott Hughes11678822019-03-27 08:56:49 -0700477 // For parsing, %P and %p are the same: case doesn't matter.
478
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700479 struct tm tm = {.tm_hour = 12};
480 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
481 EXPECT_EQ(0, tm.tm_hour);
482
483 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700484 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
485 EXPECT_EQ(0, tm.tm_hour);
486
487 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700488 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
489 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700490
491 tm = {.tm_hour = 12};
492 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
493 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700494}
495
496TEST(time, strptime_u) {
497 setenv("TZ", "UTC", 1);
498
499 struct tm tm = {};
500 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
501 EXPECT_EQ(2, tm.tm_wday);
502}
503
504TEST(time, strptime_v) {
505 setenv("TZ", "UTC", 1);
506
507 struct tm tm = {};
508 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
509 EXPECT_EQ(80, tm.tm_year);
510 EXPECT_EQ(2, tm.tm_mon);
511 EXPECT_EQ(26, tm.tm_mday);
512}
513
514TEST(time, strptime_V_G_g) {
515 setenv("TZ", "UTC", 1);
516
517 // %V (ISO-8601 week number), %G (year of week number, without century), and
518 // %g (year of week number) have no effect when parsed, and are supported
519 // solely so that it's possible for strptime(3) to parse everything that
520 // strftime(3) can output.
521 struct tm tm = {};
522 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
523 struct tm zero = {};
524 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
525}
526
Elliott Hughesd065c042020-09-01 19:02:44 -0700527TEST(time, strptime_Z) {
528#if defined(__BIONIC__)
529 // glibc doesn't handle %Z at all.
530 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
531 // are in the global `tzname` (which correspond to the current $TZ).
532 struct tm tm;
533 setenv("TZ", "Europe/Berlin", 1);
534
535 // "GMT" always works.
536 tm = {};
537 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
538 EXPECT_STREQ("GMT", tm.tm_zone);
539 EXPECT_EQ(0, tm.tm_isdst);
540 EXPECT_EQ(0, tm.tm_gmtoff);
541
542 // As does "UTC".
543 tm = {};
544 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
545 EXPECT_STREQ("UTC", tm.tm_zone);
546 EXPECT_EQ(0, tm.tm_isdst);
547 EXPECT_EQ(0, tm.tm_gmtoff);
548
549 // Europe/Berlin is known as "CET" when there's no DST.
550 tm = {};
551 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
552 EXPECT_STREQ("CET", tm.tm_zone);
553 EXPECT_EQ(0, tm.tm_isdst);
554 EXPECT_EQ(3600, tm.tm_gmtoff);
555
556 // Europe/Berlin is known as "CEST" when there's no DST.
557 tm = {};
558 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
559 EXPECT_STREQ("CEST", tm.tm_zone);
560 EXPECT_EQ(1, tm.tm_isdst);
561 EXPECT_EQ(3600, tm.tm_gmtoff);
562
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700563 // And as long as we're in Europe/Berlin, those are the only timezone
Elliott Hughesd065c042020-09-01 19:02:44 -0700564 // abbreviations that are recognized.
565 tm = {};
566 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
567#endif
568}
569
570TEST(time, strptime_z) {
571 struct tm tm;
572 setenv("TZ", "Europe/Berlin", 1);
573
574 // "UT" is what RFC822 called UTC.
575 tm = {};
576 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
577 EXPECT_STREQ("UTC", tm.tm_zone);
578 EXPECT_EQ(0, tm.tm_isdst);
579 EXPECT_EQ(0, tm.tm_gmtoff);
580 // "GMT" is RFC822's other name for UTC.
581 tm = {};
582 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
583 EXPECT_STREQ("UTC", tm.tm_zone);
584 EXPECT_EQ(0, tm.tm_isdst);
585 EXPECT_EQ(0, tm.tm_gmtoff);
586
587 // "Z" ("Zulu") is a synonym for UTC.
588 tm = {};
589 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
590 EXPECT_STREQ("UTC", tm.tm_zone);
591 EXPECT_EQ(0, tm.tm_isdst);
592 EXPECT_EQ(0, tm.tm_gmtoff);
593
594 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
595 tm = {};
596 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
597 EXPECT_STREQ("PST", tm.tm_zone);
598 EXPECT_EQ(0, tm.tm_isdst);
599 EXPECT_EQ(-28800, tm.tm_gmtoff);
600 tm = {};
601 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
602 EXPECT_STREQ("PDT", tm.tm_zone);
603 EXPECT_EQ(1, tm.tm_isdst);
604 EXPECT_EQ(-25200, tm.tm_gmtoff);
605
606 // +-hh
607 tm = {};
608 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
609 EXPECT_EQ(3600, tm.tm_gmtoff);
610 EXPECT_TRUE(tm.tm_zone == nullptr);
611 EXPECT_EQ(0, tm.tm_isdst);
612 // +-hhmm
613 tm = {};
614 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
615 EXPECT_EQ(5400, tm.tm_gmtoff);
616 EXPECT_TRUE(tm.tm_zone == nullptr);
617 EXPECT_EQ(0, tm.tm_isdst);
618 // +-hh:mm
619 tm = {};
620 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
621 EXPECT_EQ(5400, tm.tm_gmtoff);
622 EXPECT_TRUE(tm.tm_zone == nullptr);
623 EXPECT_EQ(0, tm.tm_isdst);
624}
625
Elliott Hughes4b558f52014-03-04 15:58:02 -0800626void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
627 itimerspec ts;
628 ts.it_value.tv_sec = value_s;
629 ts.it_value.tv_nsec = value_ns;
630 ts.it_interval.tv_sec = interval_s;
631 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700632 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800633}
634
Colin Cross7da20342021-07-28 11:18:11 -0700635static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800636}
637
638TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700639 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800640 memset(&se, 0, sizeof(se));
641 se.sigev_notify = SIGEV_THREAD;
642 se.sigev_notify_function = NoOpNotifyFunction;
643 timer_t timer_id;
644 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
645
Elliott Hughes33697a02016-01-26 13:04:57 -0800646 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800647 ASSERT_NE(-1, pid) << strerror(errno);
648
649 if (pid == 0) {
650 // Timers are not inherited by the child.
651 ASSERT_EQ(-1, timer_delete(timer_id));
652 ASSERT_EQ(EINVAL, errno);
653 _exit(0);
654 }
655
Elliott Hughes33697a02016-01-26 13:04:57 -0800656 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800657
658 ASSERT_EQ(0, timer_delete(timer_id));
659}
660
Yabin Cui95f1ee22015-01-13 19:53:15 -0800661static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800662static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
663 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
664 ASSERT_EQ(SIGUSR1, signal_number);
665}
666
667TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700668 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800669 memset(&se, 0, sizeof(se));
670 se.sigev_notify = SIGEV_SIGNAL;
671 se.sigev_signo = SIGUSR1;
672
673 timer_t timer_id;
674 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
675
Yabin Cui95f1ee22015-01-13 19:53:15 -0800676 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800677 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
678
679 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
680
681 itimerspec ts;
682 ts.it_value.tv_sec = 0;
683 ts.it_value.tv_nsec = 1;
684 ts.it_interval.tv_sec = 0;
685 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700686 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800687
688 usleep(500000);
689 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
690}
691
692struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800693 private:
694 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800695 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700696 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700697 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800698
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700700 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800701 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700702 timer_valid = true;
703 }
704
Yabin Cui95f1ee22015-01-13 19:53:15 -0800705 public:
Colin Cross7da20342021-07-28 11:18:11 -0700706 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800707 memset(&se, 0, sizeof(se));
708 se.sigev_notify = SIGEV_THREAD;
709 se.sigev_notify_function = fn;
710 se.sigev_value.sival_ptr = this;
711 Create();
712 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700713 void DeleteTimer() {
714 ASSERT_TRUE(timer_valid);
715 ASSERT_EQ(0, timer_delete(timer_id));
716 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800717 }
718
719 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700720 if (timer_valid) {
721 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800722 }
723 }
724
Yabin Cui95f1ee22015-01-13 19:53:15 -0800725 int Value() const {
726 return value;
727 }
728
Christopher Ferris62d84b12014-10-20 19:09:19 -0700729 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
730 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
731 }
732
733 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800734 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700735 time_t start = time(nullptr);
736 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700737 }
738 return current_value != value;
739 }
740
Colin Cross7da20342021-07-28 11:18:11 -0700741 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800742 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
743 ++cd->value;
744 }
745
Colin Cross7da20342021-07-28 11:18:11 -0700746 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800747 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
748 ++cd->value;
749
750 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700751 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800752 }
753};
754
755TEST(time, timer_settime_0) {
756 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800757 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800758
Yabin Cuibf572d92015-08-11 11:23:16 -0700759 counter.SetTime(0, 500000000, 1, 0);
760 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800761
762 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800763 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800764}
765
766TEST(time, timer_settime_repeats) {
767 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800768 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800769
Christopher Ferris62d84b12014-10-20 19:09:19 -0700770 counter.SetTime(0, 1, 0, 10);
771 ASSERT_TRUE(counter.ValueUpdated());
772 ASSERT_TRUE(counter.ValueUpdated());
773 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800774 counter.DeleteTimer();
775 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
776 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800777}
778
Yabin Cui95f1ee22015-01-13 19:53:15 -0800779static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800780static void timer_create_NULL_signal_handler(int signal_number) {
781 ++timer_create_NULL_signal_handler_invocation_count;
782 ASSERT_EQ(SIGALRM, signal_number);
783}
784
785TEST(time, timer_create_NULL) {
786 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
787 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700788 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800789
Yabin Cui95f1ee22015-01-13 19:53:15 -0800790 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800791 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
792
793 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
794
795 SetTime(timer_id, 0, 1, 0, 0);
796 usleep(500000);
797
798 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
799}
800
801TEST(time, timer_create_EINVAL) {
802 clockid_t invalid_clock = 16;
803
804 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
805 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700806 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800807 ASSERT_EQ(EINVAL, errno);
808
809 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700810 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800811 memset(&se, 0, sizeof(se));
812 se.sigev_notify = SIGEV_THREAD;
813 se.sigev_notify_function = NoOpNotifyFunction;
814 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
815 ASSERT_EQ(EINVAL, errno);
816}
817
Elliott Hughes4b558f52014-03-04 15:58:02 -0800818TEST(time, timer_create_multiple) {
819 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800820 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800821 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800822
Yabin Cui95f1ee22015-01-13 19:53:15 -0800823 ASSERT_EQ(0, counter1.Value());
824 ASSERT_EQ(0, counter2.Value());
825 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800826
Yabin Cui410c1ad2015-06-18 16:19:02 -0700827 counter2.SetTime(0, 500000000, 0, 0);
828 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800829
Yabin Cui95f1ee22015-01-13 19:53:15 -0800830 EXPECT_EQ(0, counter1.Value());
831 EXPECT_EQ(1, counter2.Value());
832 EXPECT_EQ(0, counter3.Value());
833}
834
835// Test to verify that disarming a repeatable timer disables the callbacks.
836TEST(time, timer_disarm_terminates) {
837 Counter counter(Counter::CountNotifyFunction);
838 ASSERT_EQ(0, counter.Value());
839
840 counter.SetTime(0, 1, 0, 1);
841 ASSERT_TRUE(counter.ValueUpdated());
842 ASSERT_TRUE(counter.ValueUpdated());
843 ASSERT_TRUE(counter.ValueUpdated());
844
845 counter.SetTime(0, 0, 0, 0);
846 // Add a sleep as the kernel may have pending events when the timer is disarmed.
847 usleep(500000);
848 int value = counter.Value();
849 usleep(500000);
850
851 // Verify the counter has not been incremented.
852 ASSERT_EQ(value, counter.Value());
853}
854
855// Test to verify that deleting a repeatable timer disables the callbacks.
856TEST(time, timer_delete_terminates) {
857 Counter counter(Counter::CountNotifyFunction);
858 ASSERT_EQ(0, counter.Value());
859
860 counter.SetTime(0, 1, 0, 1);
861 ASSERT_TRUE(counter.ValueUpdated());
862 ASSERT_TRUE(counter.ValueUpdated());
863 ASSERT_TRUE(counter.ValueUpdated());
864
865 counter.DeleteTimer();
866 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
867 usleep(500000);
868 int value = counter.Value();
869 usleep(500000);
870
871 // Verify the counter has not been incremented.
872 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800873}
Christopher Ferris753ad772014-03-20 20:47:45 -0700874
875struct TimerDeleteData {
876 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800877 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700878 volatile bool complete;
879};
880
Colin Cross7da20342021-07-28 11:18:11 -0700881static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700882 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
883
Elliott Hughes11859d42017-02-13 17:59:29 -0800884 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700885 timer_delete(tdd->timer_id);
886 tdd->complete = true;
887}
888
889TEST(time, timer_delete_from_timer_thread) {
890 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700891 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700892
893 memset(&se, 0, sizeof(se));
894 se.sigev_notify = SIGEV_THREAD;
895 se.sigev_notify_function = TimerDeleteCallback;
896 se.sigev_value.sival_ptr = &tdd;
897
898 tdd.complete = false;
899 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
900
901 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800902 ts.it_value.tv_sec = 1;
903 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700904 ts.it_interval.tv_sec = 0;
905 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700906 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700907
Yi Kong32bc0fc2018-08-02 17:31:13 -0700908 time_t cur_time = time(nullptr);
909 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700910 ASSERT_TRUE(tdd.complete);
911
912#if defined(__BIONIC__)
913 // Since bionic timers are implemented by creating a thread to handle the
914 // callback, verify that the thread actually completes.
915 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800916 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
917 ASSERT_EQ(-1, kill(tdd.tid, 0));
918 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700919#endif
920}
Elliott Hughes625993d2014-07-15 16:53:13 -0700921
Colin Cross35d469b2021-08-16 15:26:28 -0700922// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
923#if !defined(__NR_clock_gettime)
924#define __NR_clock_gettime __NR_clock_gettime32
925#endif
926
Elliott Hughes625993d2014-07-15 16:53:13 -0700927TEST(time, clock_gettime) {
928 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100929 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700930 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700931 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100932 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
933 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
934 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700935
Giuliano Procida096f5952021-04-08 10:51:58 +0100936 // Check we have a nice monotonic timestamp sandwich.
937 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
938 if (ts0.tv_sec == ts1.tv_sec) {
939 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700940 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100941 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
942 if (ts1.tv_sec == ts2.tv_sec) {
943 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
944 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700945}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700946
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700947TEST(time, clock_gettime_CLOCK_REALTIME) {
948 timespec ts;
949 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
950}
951
952TEST(time, clock_gettime_CLOCK_MONOTONIC) {
953 timespec ts;
954 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
955}
956
957TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
958 timespec ts;
959 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
960}
961
962TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
963 timespec ts;
964 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
965}
966
967TEST(time, clock_gettime_CLOCK_BOOTTIME) {
968 timespec ts;
969 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
970}
971
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800972TEST(time, clock_gettime_unknown) {
973 errno = 0;
974 timespec ts;
975 ASSERT_EQ(-1, clock_gettime(-1, &ts));
976 ASSERT_EQ(EINVAL, errno);
977}
978
979TEST(time, clock_getres_CLOCK_REALTIME) {
980 timespec ts;
981 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
982 ASSERT_EQ(1, ts.tv_nsec);
983 ASSERT_EQ(0, ts.tv_sec);
984}
985
986TEST(time, clock_getres_CLOCK_MONOTONIC) {
987 timespec ts;
988 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
989 ASSERT_EQ(1, ts.tv_nsec);
990 ASSERT_EQ(0, ts.tv_sec);
991}
992
993TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
994 timespec ts;
995 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
996}
997
998TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
999 timespec ts;
1000 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
1001}
1002
1003TEST(time, clock_getres_CLOCK_BOOTTIME) {
1004 timespec ts;
1005 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
1006 ASSERT_EQ(1, ts.tv_nsec);
1007 ASSERT_EQ(0, ts.tv_sec);
1008}
1009
1010TEST(time, clock_getres_unknown) {
1011 errno = 0;
1012 timespec ts = { -1, -1 };
1013 ASSERT_EQ(-1, clock_getres(-1, &ts));
1014 ASSERT_EQ(EINVAL, errno);
1015 ASSERT_EQ(-1, ts.tv_nsec);
1016 ASSERT_EQ(-1, ts.tv_sec);
1017}
1018
zijunzhaoe6202662023-01-03 23:32:18 +00001019TEST(time, clock_getres_null_resolution) {
1020 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1021}
1022
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001023TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001024 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1025 static const clock_t N = 5;
1026 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001027 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001028 for (size_t i = 0; i < N; ++i) {
1029 sleep(1);
1030 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001031 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001032 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001033}
1034
Elliott Hughesb4413592017-11-29 18:17:06 -08001035static pid_t GetInvalidPid() {
1036 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001037 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001038 fscanf(fp.get(), "%ld", &pid_max);
1039 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001040}
1041
Elliott Hughesb4413592017-11-29 18:17:06 -08001042TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001043 clockid_t clockid;
1044 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001045 timespec ts;
1046 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001047}
Yabin Cuid5c65272014-11-26 14:04:26 -08001048
Elliott Hughesb4413592017-11-29 18:17:06 -08001049TEST(time, clock_getcpuclockid_parent) {
1050 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001051 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001052 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001053 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001054}
Yabin Cuid5c65272014-11-26 14:04:26 -08001055
Elliott Hughesb4413592017-11-29 18:17:06 -08001056TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001057 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1058 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001059 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001060 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001061 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1062 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1063 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1064 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1065 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1066 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -08001067 ASSERT_EQ(0, errno);
1068}
1069
Haruki Hasegawa18160252014-10-13 00:50:47 +09001070TEST(time, clock_settime) {
1071 errno = 0;
1072 timespec ts;
1073 ASSERT_EQ(-1, clock_settime(-1, &ts));
1074 ASSERT_EQ(EINVAL, errno);
1075}
1076
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001077TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001078 timespec in;
1079 timespec out;
1080 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001081}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001082
1083TEST(time, clock_nanosleep_thread_cputime_id) {
1084 timespec in;
1085 in.tv_sec = 1;
1086 in.tv_nsec = 0;
1087 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1088}
Elliott Hughes12443702016-10-19 16:02:31 -07001089
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001090TEST(time, clock_nanosleep) {
1091 auto t0 = std::chrono::steady_clock::now();
1092 const timespec ts = {.tv_nsec = 5000000};
1093 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1094 auto t1 = std::chrono::steady_clock::now();
1095 ASSERT_GE(t1-t0, 5000000ns);
1096}
1097
1098TEST(time, nanosleep) {
1099 auto t0 = std::chrono::steady_clock::now();
1100 const timespec ts = {.tv_nsec = 5000000};
1101 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1102 auto t1 = std::chrono::steady_clock::now();
1103 ASSERT_GE(t1-t0, 5000000ns);
1104}
1105
1106TEST(time, nanosleep_EINVAL) {
1107 timespec ts = {.tv_sec = -1};
1108 errno = 0;
1109 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
1110 ASSERT_EQ(EINVAL, errno);
1111}
1112
Elliott Hughes12443702016-10-19 16:02:31 -07001113TEST(time, bug_31938693) {
1114 // User-visible symptoms in N:
1115 // http://b/31938693
1116 // https://code.google.com/p/android/issues/detail?id=225132
1117
1118 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1119 // http://b/31848040
1120
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001121 // This isn't a great test, because very few timezones were actually affected, and there's
Elliott Hughes12443702016-10-19 16:02:31 -07001122 // no real logic to which ones were affected: it was just a coincidence of the data that came
1123 // after them in the tzdata file.
1124
1125 time_t t = 1475619727;
1126 struct tm tm;
1127
1128 setenv("TZ", "America/Los_Angeles", 1);
1129 tzset();
1130 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1131 EXPECT_EQ(15, tm.tm_hour);
1132
1133 setenv("TZ", "Europe/London", 1);
1134 tzset();
1135 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1136 EXPECT_EQ(23, tm.tm_hour);
1137
1138 setenv("TZ", "America/Atka", 1);
1139 tzset();
1140 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1141 EXPECT_EQ(13, tm.tm_hour);
1142
1143 setenv("TZ", "Pacific/Apia", 1);
1144 tzset();
1145 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1146 EXPECT_EQ(12, tm.tm_hour);
1147
1148 setenv("TZ", "Pacific/Honolulu", 1);
1149 tzset();
1150 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1151 EXPECT_EQ(12, tm.tm_hour);
1152
1153 setenv("TZ", "Asia/Magadan", 1);
1154 tzset();
1155 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1156 EXPECT_EQ(9, tm.tm_hour);
1157}
Elliott Hughesea877162017-01-11 14:34:16 -08001158
1159TEST(time, bug_31339449) {
1160 // POSIX says localtime acts as if it calls tzset.
1161 // tzset does two things:
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001162 // 1. it sets the timezone ctime/localtime/mktime/strftime will use.
Elliott Hughesea877162017-01-11 14:34:16 -08001163 // 2. it sets the global `tzname`.
1164 // POSIX says localtime_r need not set `tzname` (2).
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001165 // Q: should localtime_r set the timezone (1)?
Elliott Hughesea877162017-01-11 14:34:16 -08001166 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1167
1168 // Pick a time, any time...
1169 time_t t = 1475619727;
1170
1171 // Call tzset with a specific timezone.
1172 setenv("TZ", "America/Atka", 1);
1173 tzset();
1174
1175 // If we change the timezone and call localtime, localtime should use the new timezone.
1176 setenv("TZ", "America/Los_Angeles", 1);
1177 struct tm* tm_p = localtime(&t);
1178 EXPECT_EQ(15, tm_p->tm_hour);
1179
1180 // Reset the timezone back.
1181 setenv("TZ", "America/Atka", 1);
1182 tzset();
1183
1184#if defined(__BIONIC__)
1185 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1186 setenv("TZ", "America/Los_Angeles", 1);
1187 struct tm tm = {};
1188 localtime_r(&t, &tm);
1189 EXPECT_EQ(15, tm.tm_hour);
1190#else
1191 // The BSDs agree with us, but glibc gets this wrong.
1192#endif
1193}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001194
1195TEST(time, asctime) {
1196 const struct tm tm = {};
1197 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1198}
1199
1200TEST(time, asctime_r) {
1201 const struct tm tm = {};
1202 char buf[256];
1203 ASSERT_EQ(buf, asctime_r(&tm, buf));
1204 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1205}
1206
1207TEST(time, ctime) {
1208 setenv("TZ", "UTC", 1);
1209 const time_t t = 0;
1210 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1211}
1212
1213TEST(time, ctime_r) {
1214 setenv("TZ", "UTC", 1);
1215 const time_t t = 0;
1216 char buf[256];
1217 ASSERT_EQ(buf, ctime_r(&t, buf));
1218 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1219}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001220
1221// https://issuetracker.google.com/37128336
1222TEST(time, strftime_strptime_s) {
1223 char buf[32];
1224 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1225
1226 setenv("TZ", "America/Los_Angeles", 1);
1227 strftime(buf, sizeof(buf), "<%s>", &tm0);
1228 EXPECT_STREQ("<378720000>", buf);
1229
1230 setenv("TZ", "UTC", 1);
1231 strftime(buf, sizeof(buf), "<%s>", &tm0);
1232 EXPECT_STREQ("<378691200>", buf);
1233
1234 struct tm tm;
1235
1236 setenv("TZ", "America/Los_Angeles", 1);
1237 tzset();
1238 memset(&tm, 0xff, sizeof(tm));
1239 char* p = strptime("378720000x", "%s", &tm);
1240 ASSERT_EQ('x', *p);
1241 EXPECT_EQ(0, tm.tm_sec);
1242 EXPECT_EQ(0, tm.tm_min);
1243 EXPECT_EQ(0, tm.tm_hour);
1244 EXPECT_EQ(1, tm.tm_mday);
1245 EXPECT_EQ(0, tm.tm_mon);
1246 EXPECT_EQ(82, tm.tm_year);
1247 EXPECT_EQ(5, tm.tm_wday);
1248 EXPECT_EQ(0, tm.tm_yday);
1249 EXPECT_EQ(0, tm.tm_isdst);
1250
1251 setenv("TZ", "UTC", 1);
1252 tzset();
1253 memset(&tm, 0xff, sizeof(tm));
1254 p = strptime("378691200x", "%s", &tm);
1255 ASSERT_EQ('x', *p);
1256 EXPECT_EQ(0, tm.tm_sec);
1257 EXPECT_EQ(0, tm.tm_min);
1258 EXPECT_EQ(0, tm.tm_hour);
1259 EXPECT_EQ(1, tm.tm_mday);
1260 EXPECT_EQ(0, tm.tm_mon);
1261 EXPECT_EQ(82, tm.tm_year);
1262 EXPECT_EQ(5, tm.tm_wday);
1263 EXPECT_EQ(0, tm.tm_yday);
1264 EXPECT_EQ(0, tm.tm_isdst);
1265}
1266
1267TEST(time, strptime_s_nothing) {
1268 struct tm tm;
1269 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1270}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001271
1272TEST(time, timespec_get) {
1273#if __BIONIC__
1274 timespec ts = {};
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001275 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001276 ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC));
1277 ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE));
1278 ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE));
1279#else
1280 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1281#endif
1282}
1283
1284TEST(time, timespec_get_invalid) {
1285#if __BIONIC__
1286 timespec ts = {};
Elliott Hughes7db0a6c2023-05-03 15:37:46 -07001287 ASSERT_EQ(0, timespec_get(&ts, 123));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001288#else
1289 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1290#endif
1291}
1292
1293TEST(time, timespec_getres) {
1294#if __BIONIC__
1295 timespec ts = {};
1296 ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC));
1297 ASSERT_EQ(1, ts.tv_nsec);
1298 ASSERT_EQ(0, ts.tv_sec);
1299#else
1300 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1301#endif
1302}
1303
1304TEST(time, timespec_getres_invalid) {
1305#if __BIONIC__
1306 timespec ts = {};
1307 ASSERT_EQ(0, timespec_getres(&ts, 123));
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001308#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001309 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001310#endif
1311}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001312
1313TEST(time, difftime) {
1314 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001315 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001316}
Elliott Hughes2bd43162023-06-15 13:17:08 -07001317
1318TEST(time, tzfree_null) {
1319#if __BIONIC__
1320 tzfree(nullptr);
1321#else
1322 GTEST_SKIP() << "glibc doesn't have timezone_t";
1323#endif
1324}
1325
1326TEST(time, localtime_rz) {
1327#if __BIONIC__
1328 setenv("TZ", "America/Los_Angeles", 1);
1329 tzset();
1330
1331 auto AssertTmEq = [](const struct tm& rhs, int hour) {
1332 ASSERT_EQ(93, rhs.tm_year);
1333 ASSERT_EQ(0, rhs.tm_mon);
1334 ASSERT_EQ(1, rhs.tm_mday);
1335 ASSERT_EQ(hour, rhs.tm_hour);
1336 ASSERT_EQ(0, rhs.tm_min);
1337 ASSERT_EQ(0, rhs.tm_sec);
1338 };
1339
1340 const time_t t = 725875200;
1341
1342 // Spam localtime_r() while we use localtime_rz().
1343 std::atomic<bool> done = false;
1344 std::thread thread{[&] {
1345 while (!done) {
1346 struct tm tm {};
1347 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1348 AssertTmEq(tm, 0);
1349 }
1350 }};
1351
1352 struct tm tm;
1353
1354 timezone_t london{tzalloc("Europe/London")};
1355 tm = {};
1356 ASSERT_EQ(&tm, localtime_rz(london, &t, &tm));
1357 AssertTmEq(tm, 8);
1358
1359 timezone_t seoul{tzalloc("Asia/Seoul")};
1360 tm = {};
1361 ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm));
1362 AssertTmEq(tm, 17);
1363
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001364 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001365 tm = {};
1366 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1367 ASSERT_EQ(0, tm.tm_hour);
1368 AssertTmEq(tm, 0);
1369
1370 done = true;
1371 thread.join();
1372
1373 tzfree(london);
1374 tzfree(seoul);
1375#else
1376 GTEST_SKIP() << "glibc doesn't have timezone_t";
1377#endif
1378}
1379
1380TEST(time, mktime_z) {
1381#if __BIONIC__
1382 setenv("TZ", "America/Los_Angeles", 1);
1383 tzset();
1384
1385 // Spam mktime() while we use mktime_z().
1386 std::atomic<bool> done = false;
1387 std::thread thread{[&done] {
1388 while (!done) {
1389 struct tm tm {
1390 .tm_year = 93, .tm_mday = 1
1391 };
1392 ASSERT_EQ(725875200, mktime(&tm));
1393 }
1394 }};
1395
1396 struct tm tm;
1397
1398 timezone_t london{tzalloc("Europe/London")};
1399 tm = {.tm_year = 93, .tm_mday = 1};
1400 ASSERT_EQ(725846400, mktime_z(london, &tm));
1401
1402 timezone_t seoul{tzalloc("Asia/Seoul")};
1403 tm = {.tm_year = 93, .tm_mday = 1};
1404 ASSERT_EQ(725814000, mktime_z(seoul, &tm));
1405
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001406 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001407 tm = {.tm_year = 93, .tm_mday = 1};
1408 ASSERT_EQ(725875200, mktime(&tm));
1409
1410 done = true;
1411 thread.join();
1412
1413 tzfree(london);
1414 tzfree(seoul);
1415#else
1416 GTEST_SKIP() << "glibc doesn't have timezone_t";
1417#endif
1418}
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001419
1420TEST(time, tzalloc_nullptr) {
1421#if __BIONIC__
1422 // tzalloc(nullptr) returns the system timezone.
1423 timezone_t default_tz = tzalloc(nullptr);
1424 ASSERT_NE(nullptr, default_tz);
1425
1426 // Check that mktime_z() with the default timezone matches mktime().
1427 // This assumes that the system timezone doesn't change during the test,
1428 // but that should be unlikely, and we don't have much choice if we
1429 // want to write a test at all.
1430 // We unset $TZ before calling mktime() because mktime() honors $TZ.
1431 unsetenv("TZ");
1432 struct tm tm = {.tm_year = 93, .tm_mday = 1};
1433 time_t t = mktime(&tm);
1434 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1435
1436 // Check that changing $TZ doesn't affect the tzalloc() default in
1437 // the same way it would the mktime() default.
1438 setenv("TZ", "America/Los_Angeles", 1);
1439 tzset();
1440 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1441
1442 setenv("TZ", "Europe/London", 1);
1443 tzset();
1444 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1445
1446 setenv("TZ", "Asia/Seoul", 1);
1447 tzset();
1448 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1449
1450 tzfree(default_tz);
1451#else
1452 GTEST_SKIP() << "glibc doesn't have timezone_t";
1453#endif
1454}