blob: ca8e2608c148e5e1f9cfb939dd848d6a16c58c09 [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 Hughes95646e62023-09-21 14:11:19 -0700153 ASSERT_ERRNO(EOVERFLOW);
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 Hughes95646e62023-09-21 14:11:19 -0700167 ASSERT_ERRNO(0);
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 Hughes7c9c1222024-01-12 23:46:29 +0000171TEST(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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700185 ASSERT_ERRNO(0);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700186
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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700193 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100194#else
195 ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700196 ASSERT_ERRNO(0);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100197#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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700207 ASSERT_ERRNO(EOVERFLOW);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700208}
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.
Elliott Hughes95646e62023-09-21 14:11:19 -0700225 EXPECT_ERRNO(EOVERFLOW);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000226}
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));
Elliott Hughes95646e62023-09-21 14:11:19 -0700236 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100237#else
238 setenv("TZ", "Europe/London", 1);
239 tzset();
240 errno = 0;
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100241 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700242 ASSERT_ERRNO(0);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100243#endif
244}
245
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700246TEST(time, strftime) {
247 setenv("TZ", "UTC", 1);
248
249 struct tm t;
250 memset(&t, 0, sizeof(tm));
251 t.tm_year = 200;
252 t.tm_mon = 2;
253 t.tm_mday = 10;
254
255 char buf[64];
256
257 // Seconds since the epoch.
258#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
259 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
260 EXPECT_STREQ("4108320000", buf);
261#endif
262
263 // Date and time as text.
264 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
265 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
266}
267
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000268TEST(time, strftime_second_before_epoch) {
269 setenv("TZ", "UTC", 1);
270
271 struct tm t;
272 memset(&t, 0, sizeof(tm));
273 t.tm_year = 1969 - 1900;
274 t.tm_mon = 11;
275 t.tm_mday = 31;
276 t.tm_hour = 23;
277 t.tm_min = 59;
278 t.tm_sec = 59;
279
280 char buf[64];
281
282 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
283 EXPECT_STREQ("-1", buf);
284}
285
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100286TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800287 // Netflix on Nexus Player wouldn't start (http://b/25170306).
288 struct tm t;
289 memset(&t, 0, sizeof(tm));
290
291 char buf[64];
292
293 setenv("TZ", "America/Los_Angeles", 1);
294 tzset();
295
296 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
297 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
298 EXPECT_STREQ("<PST>", buf);
299
300#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
301 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
302 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
303 EXPECT_STREQ("<PDT>", buf);
304
305 t.tm_isdst = -123; // "and negative if the information is not available".
306 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
307 EXPECT_STREQ("<>", buf);
308#endif
309
310 setenv("TZ", "UTC", 1);
311 tzset();
312
313 t.tm_isdst = 0;
314 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
315 EXPECT_STREQ("<UTC>", buf);
316
317#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
318 t.tm_isdst = 1; // UTC has no DST.
319 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
320 EXPECT_STREQ("<>", buf);
321#endif
322}
323
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100324// According to C language specification the only tm struct field needed to
325// find out replacement for %z and %Z in strftime is tm_isdst. Which is
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700326// wrong, as timezones change their standard offset and even DST savings.
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100327// tzcode deviates from C language specification and requires tm struct either
328// to be output of localtime-like functions or to be modified by mktime call
329// before passing to strftime. See tz mailing discussion for more details
330// https://mm.icann.org/pipermail/tz/2022-July/031674.html
331// But we are testing case when tm.tm_zone is null, which means that tm struct
332// is not coming from localtime and is neither modified by mktime. That's why
333// we are comparing against +0000, even though America/Los_Angeles never
334// observes it.
335TEST(time, strftime_z_null_tm_zone) {
336 char str[64];
337 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
338
339 setenv("TZ", "America/Los_Angeles", 1);
340 tzset();
341
342 tm.tm_zone = NULL;
343
344 size_t result = strftime(str, sizeof(str), "%z", &tm);
345
346 EXPECT_EQ(5U, result);
347 EXPECT_STREQ("+0000", str);
348
349 tm.tm_isdst = 1;
350
351 result = strftime(str, sizeof(str), "%z", &tm);
352
353 EXPECT_EQ(5U, result);
354 EXPECT_STREQ("+0000", str);
355
356 setenv("TZ", "UTC", 1);
357 tzset();
358
359 tm.tm_isdst = 0;
360
361 result = strftime(str, sizeof(str), "%z", &tm);
362
363 EXPECT_EQ(5U, result);
364 EXPECT_STREQ("+0000", str);
365
366 tm.tm_isdst = 1;
367
368 result = strftime(str, sizeof(str), "%z", &tm);
369
370 EXPECT_EQ(5U, result);
371 EXPECT_STREQ("+0000", str);
372}
373
374TEST(time, strftime_z_Europe_Lisbon) {
375 char str[64];
376 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
377 // tm_isdst is not set as it will be overridden by mktime call anyway.
378 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
379
380 setenv("TZ", "Europe/Lisbon", 1);
381 tzset();
382
383 // tzcode's strftime implementation for %z relies on prior mktime call.
384 // At the moment of writing %z value is taken from tm_gmtoff. So without
385 // mktime call %z is replaced with +0000.
386 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
387 mktime(&tm);
388
389 size_t result = strftime(str, sizeof(str), "%z", &tm);
390
391 EXPECT_EQ(5U, result);
392 EXPECT_STREQ("+0100", str);
393
394 // Now standard offset is 0.
395 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
396
397 mktime(&tm);
398 result = strftime(str, sizeof(str), "%z", &tm);
399
400 EXPECT_EQ(5U, result);
401 EXPECT_STREQ("+0000", str);
402}
403
Elliott Hughes0a610d02016-07-29 14:04:17 -0700404TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700405 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700406 locale_t old_locale = uselocale(cloc);
407
408 setenv("TZ", "UTC", 1);
409
410 struct tm t;
411 memset(&t, 0, sizeof(tm));
412 t.tm_year = 200;
413 t.tm_mon = 2;
414 t.tm_mday = 10;
415
416 // Date and time as text.
417 char buf[64];
418 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
419 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
420
421 uselocale(old_locale);
422 freelocale(cloc);
423}
424
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700425TEST(time, strptime) {
426 setenv("TZ", "UTC", 1);
427
428 struct tm t;
429 char buf[64];
430
431 memset(&t, 0, sizeof(t));
432 strptime("11:14", "%R", &t);
433 strftime(buf, sizeof(buf), "%H:%M", &t);
434 EXPECT_STREQ("11:14", buf);
435
436 memset(&t, 0, sizeof(t));
437 strptime("09:41:53", "%T", &t);
438 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
439 EXPECT_STREQ("09:41:53", buf);
440}
441
Elliott Hughes3376c232018-02-13 23:14:12 -0800442TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700443#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800444 setenv("TZ", "UTC", 1);
445
446 struct tm t;
447 char buf[64];
448
449 memset(&t, 0, sizeof(t));
450 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
451 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
452 EXPECT_STREQ("11:14", buf);
453
454 memset(&t, 0, sizeof(t));
455 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
456 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
457 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700458#else
459 GTEST_SKIP() << "musl doesn't support strptime_l";
460#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800461}
462
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700463TEST(time, strptime_F) {
464 setenv("TZ", "UTC", 1);
465
466 struct tm tm = {};
467 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
468 EXPECT_EQ(119, tm.tm_year);
469 EXPECT_EQ(2, tm.tm_mon);
470 EXPECT_EQ(26, tm.tm_mday);
471}
472
473TEST(time, strptime_P_p) {
474 setenv("TZ", "UTC", 1);
475
Elliott Hughes11678822019-03-27 08:56:49 -0700476 // For parsing, %P and %p are the same: case doesn't matter.
477
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700478 struct tm tm = {.tm_hour = 12};
479 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
480 EXPECT_EQ(0, tm.tm_hour);
481
482 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700483 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
484 EXPECT_EQ(0, tm.tm_hour);
485
486 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700487 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
488 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700489
490 tm = {.tm_hour = 12};
491 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
492 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700493}
494
495TEST(time, strptime_u) {
496 setenv("TZ", "UTC", 1);
497
498 struct tm tm = {};
499 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
500 EXPECT_EQ(2, tm.tm_wday);
501}
502
503TEST(time, strptime_v) {
504 setenv("TZ", "UTC", 1);
505
506 struct tm tm = {};
507 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
508 EXPECT_EQ(80, tm.tm_year);
509 EXPECT_EQ(2, tm.tm_mon);
510 EXPECT_EQ(26, tm.tm_mday);
511}
512
513TEST(time, strptime_V_G_g) {
514 setenv("TZ", "UTC", 1);
515
516 // %V (ISO-8601 week number), %G (year of week number, without century), and
517 // %g (year of week number) have no effect when parsed, and are supported
518 // solely so that it's possible for strptime(3) to parse everything that
519 // strftime(3) can output.
520 struct tm tm = {};
521 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
522 struct tm zero = {};
523 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
524}
525
Elliott Hughesd065c042020-09-01 19:02:44 -0700526TEST(time, strptime_Z) {
527#if defined(__BIONIC__)
528 // glibc doesn't handle %Z at all.
529 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
530 // are in the global `tzname` (which correspond to the current $TZ).
531 struct tm tm;
532 setenv("TZ", "Europe/Berlin", 1);
533
534 // "GMT" always works.
535 tm = {};
536 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
537 EXPECT_STREQ("GMT", tm.tm_zone);
538 EXPECT_EQ(0, tm.tm_isdst);
539 EXPECT_EQ(0, tm.tm_gmtoff);
540
541 // As does "UTC".
542 tm = {};
543 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
544 EXPECT_STREQ("UTC", tm.tm_zone);
545 EXPECT_EQ(0, tm.tm_isdst);
546 EXPECT_EQ(0, tm.tm_gmtoff);
547
548 // Europe/Berlin is known as "CET" when there's no DST.
549 tm = {};
550 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
551 EXPECT_STREQ("CET", tm.tm_zone);
552 EXPECT_EQ(0, tm.tm_isdst);
553 EXPECT_EQ(3600, tm.tm_gmtoff);
554
555 // Europe/Berlin is known as "CEST" when there's no DST.
556 tm = {};
557 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
558 EXPECT_STREQ("CEST", tm.tm_zone);
559 EXPECT_EQ(1, tm.tm_isdst);
560 EXPECT_EQ(3600, tm.tm_gmtoff);
561
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700562 // And as long as we're in Europe/Berlin, those are the only timezone
Elliott Hughesd065c042020-09-01 19:02:44 -0700563 // abbreviations that are recognized.
564 tm = {};
565 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
566#endif
567}
568
569TEST(time, strptime_z) {
570 struct tm tm;
571 setenv("TZ", "Europe/Berlin", 1);
572
573 // "UT" is what RFC822 called UTC.
574 tm = {};
575 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
576 EXPECT_STREQ("UTC", tm.tm_zone);
577 EXPECT_EQ(0, tm.tm_isdst);
578 EXPECT_EQ(0, tm.tm_gmtoff);
579 // "GMT" is RFC822's other name for UTC.
580 tm = {};
581 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
582 EXPECT_STREQ("UTC", tm.tm_zone);
583 EXPECT_EQ(0, tm.tm_isdst);
584 EXPECT_EQ(0, tm.tm_gmtoff);
585
586 // "Z" ("Zulu") is a synonym for UTC.
587 tm = {};
588 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
589 EXPECT_STREQ("UTC", tm.tm_zone);
590 EXPECT_EQ(0, tm.tm_isdst);
591 EXPECT_EQ(0, tm.tm_gmtoff);
592
593 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
594 tm = {};
595 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
596 EXPECT_STREQ("PST", tm.tm_zone);
597 EXPECT_EQ(0, tm.tm_isdst);
598 EXPECT_EQ(-28800, tm.tm_gmtoff);
599 tm = {};
600 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
601 EXPECT_STREQ("PDT", tm.tm_zone);
602 EXPECT_EQ(1, tm.tm_isdst);
603 EXPECT_EQ(-25200, tm.tm_gmtoff);
604
605 // +-hh
606 tm = {};
607 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
608 EXPECT_EQ(3600, tm.tm_gmtoff);
609 EXPECT_TRUE(tm.tm_zone == nullptr);
610 EXPECT_EQ(0, tm.tm_isdst);
611 // +-hhmm
612 tm = {};
613 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
614 EXPECT_EQ(5400, tm.tm_gmtoff);
615 EXPECT_TRUE(tm.tm_zone == nullptr);
616 EXPECT_EQ(0, tm.tm_isdst);
617 // +-hh:mm
618 tm = {};
619 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
620 EXPECT_EQ(5400, tm.tm_gmtoff);
621 EXPECT_TRUE(tm.tm_zone == nullptr);
622 EXPECT_EQ(0, tm.tm_isdst);
623}
624
Elliott Hughes4b558f52014-03-04 15:58:02 -0800625void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
626 itimerspec ts;
627 ts.it_value.tv_sec = value_s;
628 ts.it_value.tv_nsec = value_ns;
629 ts.it_interval.tv_sec = interval_s;
630 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700631 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800632}
633
Colin Cross7da20342021-07-28 11:18:11 -0700634static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800635}
636
637TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700638 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800639 memset(&se, 0, sizeof(se));
640 se.sigev_notify = SIGEV_THREAD;
641 se.sigev_notify_function = NoOpNotifyFunction;
642 timer_t timer_id;
643 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
644
Elliott Hughes33697a02016-01-26 13:04:57 -0800645 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800646 ASSERT_NE(-1, pid) << strerror(errno);
647
648 if (pid == 0) {
649 // Timers are not inherited by the child.
650 ASSERT_EQ(-1, timer_delete(timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700651 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800652 _exit(0);
653 }
654
Elliott Hughes33697a02016-01-26 13:04:57 -0800655 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800656
657 ASSERT_EQ(0, timer_delete(timer_id));
658}
659
Yabin Cui95f1ee22015-01-13 19:53:15 -0800660static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800661static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
662 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
663 ASSERT_EQ(SIGUSR1, signal_number);
664}
665
666TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700667 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800668 memset(&se, 0, sizeof(se));
669 se.sigev_notify = SIGEV_SIGNAL;
670 se.sigev_signo = SIGUSR1;
671
672 timer_t timer_id;
673 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
674
Yabin Cui95f1ee22015-01-13 19:53:15 -0800675 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800676 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
677
678 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
679
680 itimerspec ts;
681 ts.it_value.tv_sec = 0;
682 ts.it_value.tv_nsec = 1;
683 ts.it_interval.tv_sec = 0;
684 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700685 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800686
687 usleep(500000);
688 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
689}
690
691struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800692 private:
693 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800694 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700695 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700696 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800697
Elliott Hughes4b558f52014-03-04 15:58:02 -0800698 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700699 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800700 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700701 timer_valid = true;
702 }
703
Yabin Cui95f1ee22015-01-13 19:53:15 -0800704 public:
Colin Cross7da20342021-07-28 11:18:11 -0700705 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800706 memset(&se, 0, sizeof(se));
707 se.sigev_notify = SIGEV_THREAD;
708 se.sigev_notify_function = fn;
709 se.sigev_value.sival_ptr = this;
710 Create();
711 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700712 void DeleteTimer() {
713 ASSERT_TRUE(timer_valid);
714 ASSERT_EQ(0, timer_delete(timer_id));
715 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800716 }
717
718 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700719 if (timer_valid) {
720 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800721 }
722 }
723
Yabin Cui95f1ee22015-01-13 19:53:15 -0800724 int Value() const {
725 return value;
726 }
727
Christopher Ferris62d84b12014-10-20 19:09:19 -0700728 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
729 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
730 }
731
732 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800733 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700734 time_t start = time(nullptr);
735 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700736 }
737 return current_value != value;
738 }
739
Colin Cross7da20342021-07-28 11:18:11 -0700740 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800741 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
742 ++cd->value;
743 }
744
Colin Cross7da20342021-07-28 11:18:11 -0700745 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800746 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
747 ++cd->value;
748
749 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700750 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800751 }
752};
753
754TEST(time, timer_settime_0) {
755 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800756 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800757
Yabin Cuibf572d92015-08-11 11:23:16 -0700758 counter.SetTime(0, 500000000, 1, 0);
759 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800760
761 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800762 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800763}
764
765TEST(time, timer_settime_repeats) {
766 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800767 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800768
Christopher Ferris62d84b12014-10-20 19:09:19 -0700769 counter.SetTime(0, 1, 0, 10);
770 ASSERT_TRUE(counter.ValueUpdated());
771 ASSERT_TRUE(counter.ValueUpdated());
772 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800773 counter.DeleteTimer();
774 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
775 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800776}
777
Yabin Cui95f1ee22015-01-13 19:53:15 -0800778static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800779static void timer_create_NULL_signal_handler(int signal_number) {
780 ++timer_create_NULL_signal_handler_invocation_count;
781 ASSERT_EQ(SIGALRM, signal_number);
782}
783
784TEST(time, timer_create_NULL) {
785 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
786 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700787 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800788
Yabin Cui95f1ee22015-01-13 19:53:15 -0800789 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800790 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
791
792 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
793
794 SetTime(timer_id, 0, 1, 0, 0);
795 usleep(500000);
796
797 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
798}
799
800TEST(time, timer_create_EINVAL) {
801 clockid_t invalid_clock = 16;
802
803 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
804 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700805 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700806 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800807
808 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700809 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800810 memset(&se, 0, sizeof(se));
811 se.sigev_notify = SIGEV_THREAD;
812 se.sigev_notify_function = NoOpNotifyFunction;
813 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700814 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800815}
816
Elliott Hughes4b558f52014-03-04 15:58:02 -0800817TEST(time, timer_create_multiple) {
818 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800819 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800820 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800821
Yabin Cui95f1ee22015-01-13 19:53:15 -0800822 ASSERT_EQ(0, counter1.Value());
823 ASSERT_EQ(0, counter2.Value());
824 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800825
Yabin Cui410c1ad2015-06-18 16:19:02 -0700826 counter2.SetTime(0, 500000000, 0, 0);
827 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800828
Yabin Cui95f1ee22015-01-13 19:53:15 -0800829 EXPECT_EQ(0, counter1.Value());
830 EXPECT_EQ(1, counter2.Value());
831 EXPECT_EQ(0, counter3.Value());
832}
833
834// Test to verify that disarming a repeatable timer disables the callbacks.
835TEST(time, timer_disarm_terminates) {
836 Counter counter(Counter::CountNotifyFunction);
837 ASSERT_EQ(0, counter.Value());
838
839 counter.SetTime(0, 1, 0, 1);
840 ASSERT_TRUE(counter.ValueUpdated());
841 ASSERT_TRUE(counter.ValueUpdated());
842 ASSERT_TRUE(counter.ValueUpdated());
843
844 counter.SetTime(0, 0, 0, 0);
845 // Add a sleep as the kernel may have pending events when the timer is disarmed.
846 usleep(500000);
847 int value = counter.Value();
848 usleep(500000);
849
850 // Verify the counter has not been incremented.
851 ASSERT_EQ(value, counter.Value());
852}
853
854// Test to verify that deleting a repeatable timer disables the callbacks.
855TEST(time, timer_delete_terminates) {
856 Counter counter(Counter::CountNotifyFunction);
857 ASSERT_EQ(0, counter.Value());
858
859 counter.SetTime(0, 1, 0, 1);
860 ASSERT_TRUE(counter.ValueUpdated());
861 ASSERT_TRUE(counter.ValueUpdated());
862 ASSERT_TRUE(counter.ValueUpdated());
863
864 counter.DeleteTimer();
865 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
866 usleep(500000);
867 int value = counter.Value();
868 usleep(500000);
869
870 // Verify the counter has not been incremented.
871 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800872}
Christopher Ferris753ad772014-03-20 20:47:45 -0700873
874struct TimerDeleteData {
875 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800876 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700877 volatile bool complete;
878};
879
Colin Cross7da20342021-07-28 11:18:11 -0700880static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700881 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
882
Elliott Hughes11859d42017-02-13 17:59:29 -0800883 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700884 timer_delete(tdd->timer_id);
885 tdd->complete = true;
886}
887
888TEST(time, timer_delete_from_timer_thread) {
889 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700890 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700891
892 memset(&se, 0, sizeof(se));
893 se.sigev_notify = SIGEV_THREAD;
894 se.sigev_notify_function = TimerDeleteCallback;
895 se.sigev_value.sival_ptr = &tdd;
896
897 tdd.complete = false;
898 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
899
900 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800901 ts.it_value.tv_sec = 1;
902 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700903 ts.it_interval.tv_sec = 0;
904 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700905 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700906
Yi Kong32bc0fc2018-08-02 17:31:13 -0700907 time_t cur_time = time(nullptr);
908 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700909 ASSERT_TRUE(tdd.complete);
910
911#if defined(__BIONIC__)
912 // Since bionic timers are implemented by creating a thread to handle the
913 // callback, verify that the thread actually completes.
914 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800915 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
916 ASSERT_EQ(-1, kill(tdd.tid, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700917 ASSERT_ERRNO(ESRCH);
Christopher Ferris753ad772014-03-20 20:47:45 -0700918#endif
919}
Elliott Hughes625993d2014-07-15 16:53:13 -0700920
Colin Cross35d469b2021-08-16 15:26:28 -0700921// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
922#if !defined(__NR_clock_gettime)
923#define __NR_clock_gettime __NR_clock_gettime32
924#endif
925
Elliott Hughes625993d2014-07-15 16:53:13 -0700926TEST(time, clock_gettime) {
927 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100928 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700929 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700930 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100931 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
932 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
933 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700934
Giuliano Procida096f5952021-04-08 10:51:58 +0100935 // Check we have a nice monotonic timestamp sandwich.
936 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
937 if (ts0.tv_sec == ts1.tv_sec) {
938 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700939 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100940 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
941 if (ts1.tv_sec == ts2.tv_sec) {
942 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
943 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700944}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700945
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700946TEST(time, clock_gettime_CLOCK_REALTIME) {
947 timespec ts;
948 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
949}
950
951TEST(time, clock_gettime_CLOCK_MONOTONIC) {
952 timespec ts;
953 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
954}
955
956TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
957 timespec ts;
958 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
959}
960
961TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
962 timespec ts;
963 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
964}
965
966TEST(time, clock_gettime_CLOCK_BOOTTIME) {
967 timespec ts;
968 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
969}
970
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800971TEST(time, clock_gettime_unknown) {
972 errno = 0;
973 timespec ts;
974 ASSERT_EQ(-1, clock_gettime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700975 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800976}
977
978TEST(time, clock_getres_CLOCK_REALTIME) {
979 timespec ts;
980 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
981 ASSERT_EQ(1, ts.tv_nsec);
982 ASSERT_EQ(0, ts.tv_sec);
983}
984
985TEST(time, clock_getres_CLOCK_MONOTONIC) {
986 timespec ts;
987 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
988 ASSERT_EQ(1, ts.tv_nsec);
989 ASSERT_EQ(0, ts.tv_sec);
990}
991
992TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
993 timespec ts;
994 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
995}
996
997TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
998 timespec ts;
999 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
1000}
1001
1002TEST(time, clock_getres_CLOCK_BOOTTIME) {
1003 timespec ts;
1004 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
1005 ASSERT_EQ(1, ts.tv_nsec);
1006 ASSERT_EQ(0, ts.tv_sec);
1007}
1008
1009TEST(time, clock_getres_unknown) {
1010 errno = 0;
1011 timespec ts = { -1, -1 };
1012 ASSERT_EQ(-1, clock_getres(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001013 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -08001014 ASSERT_EQ(-1, ts.tv_nsec);
1015 ASSERT_EQ(-1, ts.tv_sec);
1016}
1017
zijunzhaoe6202662023-01-03 23:32:18 +00001018TEST(time, clock_getres_null_resolution) {
1019 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1020}
1021
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001022TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001023 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1024 static const clock_t N = 5;
1025 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001026 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001027 for (size_t i = 0; i < N; ++i) {
1028 sleep(1);
1029 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001030 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001031 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001032}
1033
Elliott Hughesb4413592017-11-29 18:17:06 -08001034static pid_t GetInvalidPid() {
1035 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001036 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001037 fscanf(fp.get(), "%ld", &pid_max);
1038 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001039}
1040
Elliott Hughesb4413592017-11-29 18:17:06 -08001041TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001042 clockid_t clockid;
1043 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001044 timespec ts;
1045 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001046}
Yabin Cuid5c65272014-11-26 14:04:26 -08001047
Elliott Hughesb4413592017-11-29 18:17:06 -08001048TEST(time, clock_getcpuclockid_parent) {
1049 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001050 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001051 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001052 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001053}
Yabin Cuid5c65272014-11-26 14:04:26 -08001054
Elliott Hughesb4413592017-11-29 18:17:06 -08001055TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001056 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1057 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001058 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001059 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001060 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1061 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1062 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1063 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1064 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1065 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Elliott Hughes95646e62023-09-21 14:11:19 -07001066 ASSERT_ERRNO(0);
Yabin Cuid5c65272014-11-26 14:04:26 -08001067}
1068
Haruki Hasegawa18160252014-10-13 00:50:47 +09001069TEST(time, clock_settime) {
1070 errno = 0;
1071 timespec ts;
1072 ASSERT_EQ(-1, clock_settime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001073 ASSERT_ERRNO(EINVAL);
Haruki Hasegawa18160252014-10-13 00:50:47 +09001074}
1075
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001076TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001077 timespec in;
1078 timespec out;
1079 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001080}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001081
1082TEST(time, clock_nanosleep_thread_cputime_id) {
1083 timespec in;
1084 in.tv_sec = 1;
1085 in.tv_nsec = 0;
1086 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1087}
Elliott Hughes12443702016-10-19 16:02:31 -07001088
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001089TEST(time, clock_nanosleep) {
1090 auto t0 = std::chrono::steady_clock::now();
1091 const timespec ts = {.tv_nsec = 5000000};
1092 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1093 auto t1 = std::chrono::steady_clock::now();
1094 ASSERT_GE(t1-t0, 5000000ns);
1095}
1096
1097TEST(time, nanosleep) {
1098 auto t0 = std::chrono::steady_clock::now();
1099 const timespec ts = {.tv_nsec = 5000000};
1100 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1101 auto t1 = std::chrono::steady_clock::now();
1102 ASSERT_GE(t1-t0, 5000000ns);
1103}
1104
1105TEST(time, nanosleep_EINVAL) {
1106 timespec ts = {.tv_sec = -1};
1107 errno = 0;
1108 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -07001109 ASSERT_ERRNO(EINVAL);
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001110}
1111
Elliott Hughes12443702016-10-19 16:02:31 -07001112TEST(time, bug_31938693) {
1113 // User-visible symptoms in N:
1114 // http://b/31938693
1115 // https://code.google.com/p/android/issues/detail?id=225132
1116
1117 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1118 // http://b/31848040
1119
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001120 // This isn't a great test, because very few timezones were actually affected, and there's
Elliott Hughes12443702016-10-19 16:02:31 -07001121 // no real logic to which ones were affected: it was just a coincidence of the data that came
1122 // after them in the tzdata file.
1123
1124 time_t t = 1475619727;
1125 struct tm tm;
1126
1127 setenv("TZ", "America/Los_Angeles", 1);
1128 tzset();
1129 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1130 EXPECT_EQ(15, tm.tm_hour);
1131
1132 setenv("TZ", "Europe/London", 1);
1133 tzset();
1134 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1135 EXPECT_EQ(23, tm.tm_hour);
1136
1137 setenv("TZ", "America/Atka", 1);
1138 tzset();
1139 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1140 EXPECT_EQ(13, tm.tm_hour);
1141
1142 setenv("TZ", "Pacific/Apia", 1);
1143 tzset();
1144 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1145 EXPECT_EQ(12, tm.tm_hour);
1146
1147 setenv("TZ", "Pacific/Honolulu", 1);
1148 tzset();
1149 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1150 EXPECT_EQ(12, tm.tm_hour);
1151
1152 setenv("TZ", "Asia/Magadan", 1);
1153 tzset();
1154 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1155 EXPECT_EQ(9, tm.tm_hour);
1156}
Elliott Hughesea877162017-01-11 14:34:16 -08001157
1158TEST(time, bug_31339449) {
1159 // POSIX says localtime acts as if it calls tzset.
1160 // tzset does two things:
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001161 // 1. it sets the timezone ctime/localtime/mktime/strftime will use.
Elliott Hughesea877162017-01-11 14:34:16 -08001162 // 2. it sets the global `tzname`.
1163 // POSIX says localtime_r need not set `tzname` (2).
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001164 // Q: should localtime_r set the timezone (1)?
Elliott Hughesea877162017-01-11 14:34:16 -08001165 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1166
1167 // Pick a time, any time...
1168 time_t t = 1475619727;
1169
1170 // Call tzset with a specific timezone.
1171 setenv("TZ", "America/Atka", 1);
1172 tzset();
1173
1174 // If we change the timezone and call localtime, localtime should use the new timezone.
1175 setenv("TZ", "America/Los_Angeles", 1);
1176 struct tm* tm_p = localtime(&t);
1177 EXPECT_EQ(15, tm_p->tm_hour);
1178
1179 // Reset the timezone back.
1180 setenv("TZ", "America/Atka", 1);
1181 tzset();
1182
1183#if defined(__BIONIC__)
1184 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1185 setenv("TZ", "America/Los_Angeles", 1);
1186 struct tm tm = {};
1187 localtime_r(&t, &tm);
1188 EXPECT_EQ(15, tm.tm_hour);
1189#else
1190 // The BSDs agree with us, but glibc gets this wrong.
1191#endif
1192}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001193
1194TEST(time, asctime) {
1195 const struct tm tm = {};
1196 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1197}
1198
1199TEST(time, asctime_r) {
1200 const struct tm tm = {};
1201 char buf[256];
1202 ASSERT_EQ(buf, asctime_r(&tm, buf));
1203 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1204}
1205
1206TEST(time, ctime) {
1207 setenv("TZ", "UTC", 1);
1208 const time_t t = 0;
1209 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1210}
1211
1212TEST(time, ctime_r) {
1213 setenv("TZ", "UTC", 1);
1214 const time_t t = 0;
1215 char buf[256];
1216 ASSERT_EQ(buf, ctime_r(&t, buf));
1217 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1218}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001219
1220// https://issuetracker.google.com/37128336
1221TEST(time, strftime_strptime_s) {
1222 char buf[32];
1223 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1224
1225 setenv("TZ", "America/Los_Angeles", 1);
1226 strftime(buf, sizeof(buf), "<%s>", &tm0);
1227 EXPECT_STREQ("<378720000>", buf);
1228
1229 setenv("TZ", "UTC", 1);
1230 strftime(buf, sizeof(buf), "<%s>", &tm0);
1231 EXPECT_STREQ("<378691200>", buf);
1232
1233 struct tm tm;
1234
1235 setenv("TZ", "America/Los_Angeles", 1);
1236 tzset();
1237 memset(&tm, 0xff, sizeof(tm));
1238 char* p = strptime("378720000x", "%s", &tm);
1239 ASSERT_EQ('x', *p);
1240 EXPECT_EQ(0, tm.tm_sec);
1241 EXPECT_EQ(0, tm.tm_min);
1242 EXPECT_EQ(0, tm.tm_hour);
1243 EXPECT_EQ(1, tm.tm_mday);
1244 EXPECT_EQ(0, tm.tm_mon);
1245 EXPECT_EQ(82, tm.tm_year);
1246 EXPECT_EQ(5, tm.tm_wday);
1247 EXPECT_EQ(0, tm.tm_yday);
1248 EXPECT_EQ(0, tm.tm_isdst);
1249
1250 setenv("TZ", "UTC", 1);
1251 tzset();
1252 memset(&tm, 0xff, sizeof(tm));
1253 p = strptime("378691200x", "%s", &tm);
1254 ASSERT_EQ('x', *p);
1255 EXPECT_EQ(0, tm.tm_sec);
1256 EXPECT_EQ(0, tm.tm_min);
1257 EXPECT_EQ(0, tm.tm_hour);
1258 EXPECT_EQ(1, tm.tm_mday);
1259 EXPECT_EQ(0, tm.tm_mon);
1260 EXPECT_EQ(82, tm.tm_year);
1261 EXPECT_EQ(5, tm.tm_wday);
1262 EXPECT_EQ(0, tm.tm_yday);
1263 EXPECT_EQ(0, tm.tm_isdst);
1264}
1265
1266TEST(time, strptime_s_nothing) {
1267 struct tm tm;
1268 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1269}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001270
1271TEST(time, timespec_get) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001272#if defined(__BIONIC__)
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001273 timespec ts = {};
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001274 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001275 ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC));
1276 ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE));
1277 ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE));
1278#else
1279 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1280#endif
1281}
1282
1283TEST(time, timespec_get_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001284#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001285 timespec ts = {};
Elliott Hughes7db0a6c2023-05-03 15:37:46 -07001286 ASSERT_EQ(0, timespec_get(&ts, 123));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001287#else
1288 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1289#endif
1290}
1291
1292TEST(time, timespec_getres) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001293#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001294 timespec ts = {};
1295 ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC));
1296 ASSERT_EQ(1, ts.tv_nsec);
1297 ASSERT_EQ(0, ts.tv_sec);
1298#else
1299 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1300#endif
1301}
1302
1303TEST(time, timespec_getres_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001304#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001305 timespec ts = {};
1306 ASSERT_EQ(0, timespec_getres(&ts, 123));
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001307#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001308 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001309#endif
1310}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001311
1312TEST(time, difftime) {
1313 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001314 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001315}
Elliott Hughes2bd43162023-06-15 13:17:08 -07001316
1317TEST(time, tzfree_null) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001318#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001319 tzfree(nullptr);
1320#else
1321 GTEST_SKIP() << "glibc doesn't have timezone_t";
1322#endif
1323}
1324
1325TEST(time, localtime_rz) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001326#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001327 setenv("TZ", "America/Los_Angeles", 1);
1328 tzset();
1329
1330 auto AssertTmEq = [](const struct tm& rhs, int hour) {
1331 ASSERT_EQ(93, rhs.tm_year);
1332 ASSERT_EQ(0, rhs.tm_mon);
1333 ASSERT_EQ(1, rhs.tm_mday);
1334 ASSERT_EQ(hour, rhs.tm_hour);
1335 ASSERT_EQ(0, rhs.tm_min);
1336 ASSERT_EQ(0, rhs.tm_sec);
1337 };
1338
1339 const time_t t = 725875200;
1340
1341 // Spam localtime_r() while we use localtime_rz().
1342 std::atomic<bool> done = false;
1343 std::thread thread{[&] {
1344 while (!done) {
1345 struct tm tm {};
1346 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1347 AssertTmEq(tm, 0);
1348 }
1349 }};
1350
1351 struct tm tm;
1352
1353 timezone_t london{tzalloc("Europe/London")};
1354 tm = {};
1355 ASSERT_EQ(&tm, localtime_rz(london, &t, &tm));
1356 AssertTmEq(tm, 8);
1357
1358 timezone_t seoul{tzalloc("Asia/Seoul")};
1359 tm = {};
1360 ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm));
1361 AssertTmEq(tm, 17);
1362
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001363 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001364 tm = {};
1365 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1366 ASSERT_EQ(0, tm.tm_hour);
1367 AssertTmEq(tm, 0);
1368
1369 done = true;
1370 thread.join();
1371
1372 tzfree(london);
1373 tzfree(seoul);
1374#else
1375 GTEST_SKIP() << "glibc doesn't have timezone_t";
1376#endif
1377}
1378
1379TEST(time, mktime_z) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001380#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001381 setenv("TZ", "America/Los_Angeles", 1);
1382 tzset();
1383
1384 // Spam mktime() while we use mktime_z().
1385 std::atomic<bool> done = false;
1386 std::thread thread{[&done] {
1387 while (!done) {
1388 struct tm tm {
1389 .tm_year = 93, .tm_mday = 1
1390 };
1391 ASSERT_EQ(725875200, mktime(&tm));
1392 }
1393 }};
1394
1395 struct tm tm;
1396
1397 timezone_t london{tzalloc("Europe/London")};
1398 tm = {.tm_year = 93, .tm_mday = 1};
1399 ASSERT_EQ(725846400, mktime_z(london, &tm));
1400
1401 timezone_t seoul{tzalloc("Asia/Seoul")};
1402 tm = {.tm_year = 93, .tm_mday = 1};
1403 ASSERT_EQ(725814000, mktime_z(seoul, &tm));
1404
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001405 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001406 tm = {.tm_year = 93, .tm_mday = 1};
1407 ASSERT_EQ(725875200, mktime(&tm));
1408
1409 done = true;
1410 thread.join();
1411
1412 tzfree(london);
1413 tzfree(seoul);
1414#else
1415 GTEST_SKIP() << "glibc doesn't have timezone_t";
1416#endif
1417}
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001418
1419TEST(time, tzalloc_nullptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001420#if defined(__BIONIC__)
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001421 // tzalloc(nullptr) returns the system timezone.
1422 timezone_t default_tz = tzalloc(nullptr);
1423 ASSERT_NE(nullptr, default_tz);
1424
1425 // Check that mktime_z() with the default timezone matches mktime().
1426 // This assumes that the system timezone doesn't change during the test,
1427 // but that should be unlikely, and we don't have much choice if we
1428 // want to write a test at all.
1429 // We unset $TZ before calling mktime() because mktime() honors $TZ.
1430 unsetenv("TZ");
1431 struct tm tm = {.tm_year = 93, .tm_mday = 1};
1432 time_t t = mktime(&tm);
1433 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1434
1435 // Check that changing $TZ doesn't affect the tzalloc() default in
1436 // the same way it would the mktime() default.
1437 setenv("TZ", "America/Los_Angeles", 1);
1438 tzset();
1439 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1440
1441 setenv("TZ", "Europe/London", 1);
1442 tzset();
1443 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1444
1445 setenv("TZ", "Asia/Seoul", 1);
1446 tzset();
1447 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1448
1449 tzfree(default_tz);
1450#else
1451 GTEST_SKIP() << "glibc doesn't have timezone_t";
1452#endif
1453}
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001454
1455TEST(time, tzalloc_unique_ptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001456#if defined(__BIONIC__)
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001457 std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"),
1458 tzfree};
1459#else
1460 GTEST_SKIP() << "glibc doesn't have timezone_t";
1461#endif
1462}