blob: baafbf6663f69a3d39eb37e4bf5fbe110a235dcd [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 Hughesc793bc02024-05-21 21:35:49 +000034#include "android-base/file.h"
35#include "android-base/strings.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080036#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070037
Elliott Hughesca3f8e42019-10-28 15:59:38 -070038using namespace std::chrono_literals;
39
Mark Salyzyn0b846e82017-12-20 08:56:18 -080040TEST(time, time) {
41 // Acquire time
42 time_t p1, t1 = time(&p1);
43 // valid?
44 ASSERT_NE(static_cast<time_t>(0), t1);
45 ASSERT_NE(static_cast<time_t>(-1), t1);
46 ASSERT_EQ(p1, t1);
47
48 // Acquire time one+ second later
49 usleep(1010000);
50 time_t p2, t2 = time(&p2);
51 // valid?
52 ASSERT_NE(static_cast<time_t>(0), t2);
53 ASSERT_NE(static_cast<time_t>(-1), t2);
54 ASSERT_EQ(p2, t2);
55
56 // Expect time progression
57 ASSERT_LT(p1, p2);
58 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
59
60 // Expect nullptr call to produce same results
61 ASSERT_LE(t2, time(nullptr));
62 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
63}
64
Elliott Hughesee178bf2013-07-12 11:25:20 -070065TEST(time, gmtime) {
66 time_t t = 0;
67 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070068 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070069 ASSERT_EQ(0, broken_down->tm_sec);
70 ASSERT_EQ(0, broken_down->tm_min);
71 ASSERT_EQ(0, broken_down->tm_hour);
72 ASSERT_EQ(1, broken_down->tm_mday);
73 ASSERT_EQ(0, broken_down->tm_mon);
74 ASSERT_EQ(1970, broken_down->tm_year + 1900);
75}
Elliott Hughes7843d442013-08-22 11:37:32 -070076
Elliott Hughes5a29d542017-12-07 16:05:57 -080077TEST(time, gmtime_r) {
78 struct tm tm = {};
79 time_t t = 0;
80 struct tm* broken_down = gmtime_r(&t, &tm);
81 ASSERT_EQ(broken_down, &tm);
82 ASSERT_EQ(0, broken_down->tm_sec);
83 ASSERT_EQ(0, broken_down->tm_min);
84 ASSERT_EQ(0, broken_down->tm_hour);
85 ASSERT_EQ(1, broken_down->tm_mday);
86 ASSERT_EQ(0, broken_down->tm_mon);
87 ASSERT_EQ(1970, broken_down->tm_year + 1900);
88}
89
Almaz Mingaleevda75bb62022-06-29 15:47:37 +000090TEST(time, mktime_TZ_as_UTC_and_offset) {
91 struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1};
92
93 // This TZ value is not a valid Olson ID and is not present in tzdata file,
94 // but is a valid TZ string according to POSIX standard.
95 setenv("TZ", "UTC+08:00:00", 1);
96 tzset();
97 ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm));
98}
99
Elliott Hughes329103d2014-04-25 16:55:04 -0700100static void* gmtime_no_stack_overflow_14313703_fn(void*) {
101 const char* original_tz = getenv("TZ");
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700102 // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist.
Elliott Hughes329103d2014-04-25 16:55:04 -0700103 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
104 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700105 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -0700106 setenv("TZ", original_tz, 1);
107 }
108 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -0700110}
111
112TEST(time, gmtime_no_stack_overflow_14313703) {
113 // Is it safe to call tzload on a thread with a small stack?
114 // http://b/14313703
115 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800116 pthread_attr_t a;
117 ASSERT_EQ(0, pthread_attr_init(&a));
118 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700119
120 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800122 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700123}
124
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900125TEST(time, mktime_empty_TZ) {
126 // tzcode used to have a bug where it didn't reinitialize some internal state.
127
128 // Choose a time where DST is set.
129 struct tm t;
130 memset(&t, 0, sizeof(tm));
131 t.tm_year = 1980 - 1900;
132 t.tm_mon = 6;
133 t.tm_mday = 2;
134
135 setenv("TZ", "America/Los_Angeles", 1);
136 tzset();
137 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
138
139 memset(&t, 0, sizeof(tm));
140 t.tm_year = 1980 - 1900;
141 t.tm_mon = 6;
142 t.tm_mday = 2;
143
144 setenv("TZ", "", 1); // Implies UTC.
145 tzset();
146 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
147}
148
Elliott Hughes7843d442013-08-22 11:37:32 -0700149TEST(time, mktime_10310929) {
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100150 struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10};
Elliott Hughes7843d442013-08-22 11:37:32 -0700151
Elliott Hughes0c401522013-10-18 16:21:54 -0700152#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700153 // 32-bit bionic has a signed 32-bit time_t.
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100154 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700155 ASSERT_ERRNO(EOVERFLOW);
Elliott Hughes0c401522013-10-18 16:21:54 -0700156#else
157 // Everyone else should be using a signed 64-bit time_t.
158 ASSERT_GE(sizeof(time_t) * 8, 64U);
159
160 setenv("TZ", "America/Los_Angeles", 1);
161 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700162 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700163
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100164 // On the date/time specified by tm America/Los_Angeles
165 // follows DST. But tm_isdst is set to 0, which forces
166 // mktime to interpret that time as local standard, hence offset
167 // is 8 hours, not 7.
168 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700169 ASSERT_ERRNO(0);
Elliott Hughes7843d442013-08-22 11:37:32 -0700170#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800171}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800172
Elliott Hughes7c9c1222024-01-12 23:46:29 +0000173TEST(time, mktime_EOVERFLOW) {
Elliott Hughesf52b2162023-05-19 16:09:47 -0700174 setenv("TZ", "UTC", 1);
175
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700176 struct tm t;
177 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700178
179 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
180 t.tm_year = 2016 - 1900;
181
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700182 t.tm_mon = 2;
183 t.tm_mday = 10;
184
185 errno = 0;
186 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700187 ASSERT_ERRNO(0);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700188
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100189 // This will overflow for LP32.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700190 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700191
192 errno = 0;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100193#if !defined(__LP64__)
194 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700195 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100196#else
197 ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700198 ASSERT_ERRNO(0);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100199#endif
200
201 // This will overflow for LP32 or LP64.
202 // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is.
203 t.tm_year = INT_MAX;
204 t.tm_mon = 11;
205 t.tm_mday = 45;
206
207 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700208 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700209 ASSERT_ERRNO(EOVERFLOW);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700210}
211
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000212TEST(time, mktime_invalid_tm_TZ_combination) {
213 setenv("TZ", "UTC", 1);
214
215 struct tm t;
216 memset(&t, 0, sizeof(tm));
217 t.tm_year = 2022 - 1900;
218 t.tm_mon = 11;
219 t.tm_mday = 31;
220 // UTC does not observe DST
221 t.tm_isdst = 1;
222
223 errno = 0;
224
225 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
226 // mktime sets errno to EOVERFLOW if result is unrepresentable.
Elliott Hughes95646e62023-09-21 14:11:19 -0700227 EXPECT_ERRNO(EOVERFLOW);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000228}
229
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100230// Transitions in the tzdata file are generated up to the year 2100. Testing
231// that dates beyond that are handled properly too.
232TEST(time, mktime_after_2100) {
233 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
234
235#if !defined(__LP64__)
236 // 32-bit bionic has a signed 32-bit time_t.
237 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700238 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100239#else
240 setenv("TZ", "Europe/London", 1);
241 tzset();
242 errno = 0;
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100243 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700244 ASSERT_ERRNO(0);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100245#endif
246}
247
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700248TEST(time, strftime) {
249 setenv("TZ", "UTC", 1);
250
251 struct tm t;
252 memset(&t, 0, sizeof(tm));
253 t.tm_year = 200;
254 t.tm_mon = 2;
255 t.tm_mday = 10;
256
257 char buf[64];
258
259 // Seconds since the epoch.
260#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
261 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
262 EXPECT_STREQ("4108320000", buf);
263#endif
264
265 // Date and time as text.
266 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
267 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
268}
269
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000270TEST(time, strftime_second_before_epoch) {
271 setenv("TZ", "UTC", 1);
272
273 struct tm t;
274 memset(&t, 0, sizeof(tm));
275 t.tm_year = 1969 - 1900;
276 t.tm_mon = 11;
277 t.tm_mday = 31;
278 t.tm_hour = 23;
279 t.tm_min = 59;
280 t.tm_sec = 59;
281
282 char buf[64];
283
284 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
285 EXPECT_STREQ("-1", buf);
286}
287
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100288TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800289 // Netflix on Nexus Player wouldn't start (http://b/25170306).
290 struct tm t;
291 memset(&t, 0, sizeof(tm));
292
293 char buf[64];
294
295 setenv("TZ", "America/Los_Angeles", 1);
296 tzset();
297
298 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
299 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
300 EXPECT_STREQ("<PST>", buf);
301
302#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
303 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
304 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
305 EXPECT_STREQ("<PDT>", buf);
306
307 t.tm_isdst = -123; // "and negative if the information is not available".
308 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
309 EXPECT_STREQ("<>", buf);
310#endif
311
312 setenv("TZ", "UTC", 1);
313 tzset();
314
315 t.tm_isdst = 0;
316 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
317 EXPECT_STREQ("<UTC>", buf);
318
319#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
320 t.tm_isdst = 1; // UTC has no DST.
321 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
322 EXPECT_STREQ("<>", buf);
323#endif
324}
325
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100326// According to C language specification the only tm struct field needed to
327// find out replacement for %z and %Z in strftime is tm_isdst. Which is
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700328// wrong, as timezones change their standard offset and even DST savings.
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100329// tzcode deviates from C language specification and requires tm struct either
330// to be output of localtime-like functions or to be modified by mktime call
331// before passing to strftime. See tz mailing discussion for more details
332// https://mm.icann.org/pipermail/tz/2022-July/031674.html
333// But we are testing case when tm.tm_zone is null, which means that tm struct
334// is not coming from localtime and is neither modified by mktime. That's why
335// we are comparing against +0000, even though America/Los_Angeles never
336// observes it.
337TEST(time, strftime_z_null_tm_zone) {
338 char str[64];
339 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
340
341 setenv("TZ", "America/Los_Angeles", 1);
342 tzset();
343
344 tm.tm_zone = NULL;
345
346 size_t result = strftime(str, sizeof(str), "%z", &tm);
347
348 EXPECT_EQ(5U, result);
349 EXPECT_STREQ("+0000", str);
350
351 tm.tm_isdst = 1;
352
353 result = strftime(str, sizeof(str), "%z", &tm);
354
355 EXPECT_EQ(5U, result);
356 EXPECT_STREQ("+0000", str);
357
358 setenv("TZ", "UTC", 1);
359 tzset();
360
361 tm.tm_isdst = 0;
362
363 result = strftime(str, sizeof(str), "%z", &tm);
364
365 EXPECT_EQ(5U, result);
366 EXPECT_STREQ("+0000", str);
367
368 tm.tm_isdst = 1;
369
370 result = strftime(str, sizeof(str), "%z", &tm);
371
372 EXPECT_EQ(5U, result);
373 EXPECT_STREQ("+0000", str);
374}
375
376TEST(time, strftime_z_Europe_Lisbon) {
377 char str[64];
378 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
379 // tm_isdst is not set as it will be overridden by mktime call anyway.
380 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
381
382 setenv("TZ", "Europe/Lisbon", 1);
383 tzset();
384
385 // tzcode's strftime implementation for %z relies on prior mktime call.
386 // At the moment of writing %z value is taken from tm_gmtoff. So without
387 // mktime call %z is replaced with +0000.
388 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
389 mktime(&tm);
390
391 size_t result = strftime(str, sizeof(str), "%z", &tm);
392
393 EXPECT_EQ(5U, result);
394 EXPECT_STREQ("+0100", str);
395
396 // Now standard offset is 0.
397 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
398
399 mktime(&tm);
400 result = strftime(str, sizeof(str), "%z", &tm);
401
402 EXPECT_EQ(5U, result);
403 EXPECT_STREQ("+0000", str);
404}
405
Elliott Hughes0a610d02016-07-29 14:04:17 -0700406TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700407 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700408 locale_t old_locale = uselocale(cloc);
409
410 setenv("TZ", "UTC", 1);
411
412 struct tm t;
413 memset(&t, 0, sizeof(tm));
414 t.tm_year = 200;
415 t.tm_mon = 2;
416 t.tm_mday = 10;
417
418 // Date and time as text.
419 char buf[64];
420 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
421 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
422
423 uselocale(old_locale);
424 freelocale(cloc);
425}
426
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700427TEST(time, strptime) {
428 setenv("TZ", "UTC", 1);
429
430 struct tm t;
431 char buf[64];
432
433 memset(&t, 0, sizeof(t));
434 strptime("11:14", "%R", &t);
435 strftime(buf, sizeof(buf), "%H:%M", &t);
436 EXPECT_STREQ("11:14", buf);
437
438 memset(&t, 0, sizeof(t));
439 strptime("09:41:53", "%T", &t);
440 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
441 EXPECT_STREQ("09:41:53", buf);
442}
443
Elliott Hughes3376c232018-02-13 23:14:12 -0800444TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700445#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800446 setenv("TZ", "UTC", 1);
447
448 struct tm t;
449 char buf[64];
450
451 memset(&t, 0, sizeof(t));
452 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
453 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
454 EXPECT_STREQ("11:14", buf);
455
456 memset(&t, 0, sizeof(t));
457 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
458 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
459 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700460#else
461 GTEST_SKIP() << "musl doesn't support strptime_l";
462#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800463}
464
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700465TEST(time, strptime_F) {
466 setenv("TZ", "UTC", 1);
467
468 struct tm tm = {};
469 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
470 EXPECT_EQ(119, tm.tm_year);
471 EXPECT_EQ(2, tm.tm_mon);
472 EXPECT_EQ(26, tm.tm_mday);
473}
474
475TEST(time, strptime_P_p) {
476 setenv("TZ", "UTC", 1);
477
Elliott Hughes11678822019-03-27 08:56:49 -0700478 // For parsing, %P and %p are the same: case doesn't matter.
479
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700480 struct tm tm = {.tm_hour = 12};
481 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
482 EXPECT_EQ(0, tm.tm_hour);
483
484 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700485 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
486 EXPECT_EQ(0, tm.tm_hour);
487
488 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700489 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
490 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700491
492 tm = {.tm_hour = 12};
493 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
494 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700495}
496
497TEST(time, strptime_u) {
498 setenv("TZ", "UTC", 1);
499
500 struct tm tm = {};
501 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
502 EXPECT_EQ(2, tm.tm_wday);
503}
504
505TEST(time, strptime_v) {
506 setenv("TZ", "UTC", 1);
507
508 struct tm tm = {};
509 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
510 EXPECT_EQ(80, tm.tm_year);
511 EXPECT_EQ(2, tm.tm_mon);
512 EXPECT_EQ(26, tm.tm_mday);
513}
514
515TEST(time, strptime_V_G_g) {
516 setenv("TZ", "UTC", 1);
517
518 // %V (ISO-8601 week number), %G (year of week number, without century), and
519 // %g (year of week number) have no effect when parsed, and are supported
520 // solely so that it's possible for strptime(3) to parse everything that
521 // strftime(3) can output.
522 struct tm tm = {};
523 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
524 struct tm zero = {};
525 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
526}
527
Elliott Hughesd065c042020-09-01 19:02:44 -0700528TEST(time, strptime_Z) {
529#if defined(__BIONIC__)
530 // glibc doesn't handle %Z at all.
531 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
532 // are in the global `tzname` (which correspond to the current $TZ).
533 struct tm tm;
534 setenv("TZ", "Europe/Berlin", 1);
535
536 // "GMT" always works.
537 tm = {};
538 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
539 EXPECT_STREQ("GMT", tm.tm_zone);
540 EXPECT_EQ(0, tm.tm_isdst);
541 EXPECT_EQ(0, tm.tm_gmtoff);
542
543 // As does "UTC".
544 tm = {};
545 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
546 EXPECT_STREQ("UTC", tm.tm_zone);
547 EXPECT_EQ(0, tm.tm_isdst);
548 EXPECT_EQ(0, tm.tm_gmtoff);
549
550 // Europe/Berlin is known as "CET" when there's no DST.
551 tm = {};
552 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
553 EXPECT_STREQ("CET", tm.tm_zone);
554 EXPECT_EQ(0, tm.tm_isdst);
555 EXPECT_EQ(3600, tm.tm_gmtoff);
556
557 // Europe/Berlin is known as "CEST" when there's no DST.
558 tm = {};
559 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
560 EXPECT_STREQ("CEST", tm.tm_zone);
561 EXPECT_EQ(1, tm.tm_isdst);
562 EXPECT_EQ(3600, tm.tm_gmtoff);
563
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700564 // And as long as we're in Europe/Berlin, those are the only timezone
Elliott Hughesd065c042020-09-01 19:02:44 -0700565 // abbreviations that are recognized.
566 tm = {};
567 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
568#endif
569}
570
571TEST(time, strptime_z) {
572 struct tm tm;
573 setenv("TZ", "Europe/Berlin", 1);
574
575 // "UT" is what RFC822 called UTC.
576 tm = {};
577 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
578 EXPECT_STREQ("UTC", tm.tm_zone);
579 EXPECT_EQ(0, tm.tm_isdst);
580 EXPECT_EQ(0, tm.tm_gmtoff);
581 // "GMT" is RFC822's other name for UTC.
582 tm = {};
583 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
584 EXPECT_STREQ("UTC", tm.tm_zone);
585 EXPECT_EQ(0, tm.tm_isdst);
586 EXPECT_EQ(0, tm.tm_gmtoff);
587
588 // "Z" ("Zulu") is a synonym for UTC.
589 tm = {};
590 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
591 EXPECT_STREQ("UTC", tm.tm_zone);
592 EXPECT_EQ(0, tm.tm_isdst);
593 EXPECT_EQ(0, tm.tm_gmtoff);
594
595 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
596 tm = {};
597 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
598 EXPECT_STREQ("PST", tm.tm_zone);
599 EXPECT_EQ(0, tm.tm_isdst);
600 EXPECT_EQ(-28800, tm.tm_gmtoff);
601 tm = {};
602 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
603 EXPECT_STREQ("PDT", tm.tm_zone);
604 EXPECT_EQ(1, tm.tm_isdst);
605 EXPECT_EQ(-25200, tm.tm_gmtoff);
606
607 // +-hh
608 tm = {};
609 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
610 EXPECT_EQ(3600, tm.tm_gmtoff);
611 EXPECT_TRUE(tm.tm_zone == nullptr);
612 EXPECT_EQ(0, tm.tm_isdst);
613 // +-hhmm
614 tm = {};
615 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
616 EXPECT_EQ(5400, tm.tm_gmtoff);
617 EXPECT_TRUE(tm.tm_zone == nullptr);
618 EXPECT_EQ(0, tm.tm_isdst);
619 // +-hh:mm
620 tm = {};
621 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
622 EXPECT_EQ(5400, tm.tm_gmtoff);
623 EXPECT_TRUE(tm.tm_zone == nullptr);
624 EXPECT_EQ(0, tm.tm_isdst);
625}
626
Elliott Hughes4b558f52014-03-04 15:58:02 -0800627void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
628 itimerspec ts;
629 ts.it_value.tv_sec = value_s;
630 ts.it_value.tv_nsec = value_ns;
631 ts.it_interval.tv_sec = interval_s;
632 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700633 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800634}
635
Colin Cross7da20342021-07-28 11:18:11 -0700636static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800637}
638
639TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700640 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800641 memset(&se, 0, sizeof(se));
642 se.sigev_notify = SIGEV_THREAD;
643 se.sigev_notify_function = NoOpNotifyFunction;
644 timer_t timer_id;
645 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
646
Elliott Hughes33697a02016-01-26 13:04:57 -0800647 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800648 ASSERT_NE(-1, pid) << strerror(errno);
649
650 if (pid == 0) {
651 // Timers are not inherited by the child.
652 ASSERT_EQ(-1, timer_delete(timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700653 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800654 _exit(0);
655 }
656
Elliott Hughes33697a02016-01-26 13:04:57 -0800657 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800658
659 ASSERT_EQ(0, timer_delete(timer_id));
660}
661
Yabin Cui95f1ee22015-01-13 19:53:15 -0800662static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800663static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
664 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
665 ASSERT_EQ(SIGUSR1, signal_number);
666}
667
668TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700669 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800670 memset(&se, 0, sizeof(se));
671 se.sigev_notify = SIGEV_SIGNAL;
672 se.sigev_signo = SIGUSR1;
673
674 timer_t timer_id;
675 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
676
Yabin Cui95f1ee22015-01-13 19:53:15 -0800677 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800678 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
679
680 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
681
682 itimerspec ts;
683 ts.it_value.tv_sec = 0;
684 ts.it_value.tv_nsec = 1;
685 ts.it_interval.tv_sec = 0;
686 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700687 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800688
689 usleep(500000);
690 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
691}
692
693struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800694 private:
695 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800696 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700697 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700698 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699
Elliott Hughes4b558f52014-03-04 15:58:02 -0800700 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700701 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800702 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700703 timer_valid = true;
704 }
705
Yabin Cui95f1ee22015-01-13 19:53:15 -0800706 public:
Colin Cross7da20342021-07-28 11:18:11 -0700707 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800708 memset(&se, 0, sizeof(se));
709 se.sigev_notify = SIGEV_THREAD;
710 se.sigev_notify_function = fn;
711 se.sigev_value.sival_ptr = this;
712 Create();
713 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700714 void DeleteTimer() {
715 ASSERT_TRUE(timer_valid);
716 ASSERT_EQ(0, timer_delete(timer_id));
717 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800718 }
719
720 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700721 if (timer_valid) {
722 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800723 }
724 }
725
Yabin Cui95f1ee22015-01-13 19:53:15 -0800726 int Value() const {
727 return value;
728 }
729
Christopher Ferris62d84b12014-10-20 19:09:19 -0700730 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
731 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
732 }
733
734 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800735 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700736 time_t start = time(nullptr);
737 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700738 }
739 return current_value != value;
740 }
741
Colin Cross7da20342021-07-28 11:18:11 -0700742 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800743 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
744 ++cd->value;
745 }
746
Colin Cross7da20342021-07-28 11:18:11 -0700747 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800748 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
749 ++cd->value;
750
751 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700752 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800753 }
754};
755
756TEST(time, timer_settime_0) {
757 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800758 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800759
Yabin Cuibf572d92015-08-11 11:23:16 -0700760 counter.SetTime(0, 500000000, 1, 0);
761 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800762
763 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800764 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800765}
766
767TEST(time, timer_settime_repeats) {
768 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800769 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800770
Christopher Ferris62d84b12014-10-20 19:09:19 -0700771 counter.SetTime(0, 1, 0, 10);
772 ASSERT_TRUE(counter.ValueUpdated());
773 ASSERT_TRUE(counter.ValueUpdated());
774 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800775 counter.DeleteTimer();
776 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
777 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800778}
779
Yabin Cui95f1ee22015-01-13 19:53:15 -0800780static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800781static void timer_create_NULL_signal_handler(int signal_number) {
782 ++timer_create_NULL_signal_handler_invocation_count;
783 ASSERT_EQ(SIGALRM, signal_number);
784}
785
786TEST(time, timer_create_NULL) {
787 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
788 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700789 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800790
Yabin Cui95f1ee22015-01-13 19:53:15 -0800791 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800792 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
793
794 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
795
796 SetTime(timer_id, 0, 1, 0, 0);
797 usleep(500000);
798
799 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
800}
801
Elliott Hughesc793bc02024-05-21 21:35:49 +0000802static int GetThreadCount() {
803 std::string status;
804 if (android::base::ReadFileToString("/proc/self/status", &status)) {
805 for (const auto& line : android::base::Split(status, "\n")) {
806 int thread_count;
807 if (sscanf(line.c_str(), "Threads: %d", &thread_count) == 1) {
808 return thread_count;
809 }
810 }
811 }
812 return -1;
813}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800814
Elliott Hughesc793bc02024-05-21 21:35:49 +0000815TEST(time, timer_create_EINVAL) {
816 const clockid_t kInvalidClock = 16;
817
818 // A SIGEV_SIGNAL timer failure is easy; that's the kernel's problem.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800819 timer_t timer_id;
Elliott Hughesc793bc02024-05-21 21:35:49 +0000820 ASSERT_EQ(-1, timer_create(kInvalidClock, nullptr, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700821 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800822
Elliott Hughesc793bc02024-05-21 21:35:49 +0000823 // A SIGEV_THREAD timer failure is more interesting because we have a thread
824 // to clean up (https://issuetracker.google.com/340125671).
825 sigevent se = {};
Elliott Hughes4b558f52014-03-04 15:58:02 -0800826 se.sigev_notify = SIGEV_THREAD;
827 se.sigev_notify_function = NoOpNotifyFunction;
Elliott Hughesc793bc02024-05-21 21:35:49 +0000828 ASSERT_EQ(-1, timer_create(kInvalidClock, &se, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700829 ASSERT_ERRNO(EINVAL);
Elliott Hughesc793bc02024-05-21 21:35:49 +0000830
831 // timer_create() doesn't guarantee that the thread will be dead _before_
832 // it returns because that would require extra synchronization that's
833 // unnecessary in the normal (successful) case. A timeout here means we
834 // leaked a thread.
835 while (GetThreadCount() > 1) {
836 }
Elliott Hughes4b558f52014-03-04 15:58:02 -0800837}
838
Elliott Hughes4b558f52014-03-04 15:58:02 -0800839TEST(time, timer_create_multiple) {
840 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800841 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800842 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800843
Yabin Cui95f1ee22015-01-13 19:53:15 -0800844 ASSERT_EQ(0, counter1.Value());
845 ASSERT_EQ(0, counter2.Value());
846 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800847
Yabin Cui410c1ad2015-06-18 16:19:02 -0700848 counter2.SetTime(0, 500000000, 0, 0);
849 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800850
Yabin Cui95f1ee22015-01-13 19:53:15 -0800851 EXPECT_EQ(0, counter1.Value());
852 EXPECT_EQ(1, counter2.Value());
853 EXPECT_EQ(0, counter3.Value());
854}
855
856// Test to verify that disarming a repeatable timer disables the callbacks.
857TEST(time, timer_disarm_terminates) {
858 Counter counter(Counter::CountNotifyFunction);
859 ASSERT_EQ(0, counter.Value());
860
861 counter.SetTime(0, 1, 0, 1);
862 ASSERT_TRUE(counter.ValueUpdated());
863 ASSERT_TRUE(counter.ValueUpdated());
864 ASSERT_TRUE(counter.ValueUpdated());
865
866 counter.SetTime(0, 0, 0, 0);
867 // Add a sleep as the kernel may have pending events when the timer is disarmed.
868 usleep(500000);
869 int value = counter.Value();
870 usleep(500000);
871
872 // Verify the counter has not been incremented.
873 ASSERT_EQ(value, counter.Value());
874}
875
876// Test to verify that deleting a repeatable timer disables the callbacks.
877TEST(time, timer_delete_terminates) {
878 Counter counter(Counter::CountNotifyFunction);
879 ASSERT_EQ(0, counter.Value());
880
881 counter.SetTime(0, 1, 0, 1);
882 ASSERT_TRUE(counter.ValueUpdated());
883 ASSERT_TRUE(counter.ValueUpdated());
884 ASSERT_TRUE(counter.ValueUpdated());
885
886 counter.DeleteTimer();
887 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
888 usleep(500000);
889 int value = counter.Value();
890 usleep(500000);
891
892 // Verify the counter has not been incremented.
893 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800894}
Christopher Ferris753ad772014-03-20 20:47:45 -0700895
896struct TimerDeleteData {
897 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800898 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700899 volatile bool complete;
900};
901
Colin Cross7da20342021-07-28 11:18:11 -0700902static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700903 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
904
Elliott Hughes11859d42017-02-13 17:59:29 -0800905 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700906 timer_delete(tdd->timer_id);
907 tdd->complete = true;
908}
909
910TEST(time, timer_delete_from_timer_thread) {
911 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700912 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700913
914 memset(&se, 0, sizeof(se));
915 se.sigev_notify = SIGEV_THREAD;
916 se.sigev_notify_function = TimerDeleteCallback;
917 se.sigev_value.sival_ptr = &tdd;
918
919 tdd.complete = false;
920 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
921
922 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800923 ts.it_value.tv_sec = 1;
924 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700925 ts.it_interval.tv_sec = 0;
926 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700927 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700928
Yi Kong32bc0fc2018-08-02 17:31:13 -0700929 time_t cur_time = time(nullptr);
930 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700931 ASSERT_TRUE(tdd.complete);
932
933#if defined(__BIONIC__)
934 // Since bionic timers are implemented by creating a thread to handle the
935 // callback, verify that the thread actually completes.
936 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800937 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
938 ASSERT_EQ(-1, kill(tdd.tid, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700939 ASSERT_ERRNO(ESRCH);
Christopher Ferris753ad772014-03-20 20:47:45 -0700940#endif
941}
Elliott Hughes625993d2014-07-15 16:53:13 -0700942
Colin Cross35d469b2021-08-16 15:26:28 -0700943// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
944#if !defined(__NR_clock_gettime)
945#define __NR_clock_gettime __NR_clock_gettime32
946#endif
947
Elliott Hughes625993d2014-07-15 16:53:13 -0700948TEST(time, clock_gettime) {
949 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100950 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700951 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700952 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100953 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
954 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
955 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700956
Giuliano Procida096f5952021-04-08 10:51:58 +0100957 // Check we have a nice monotonic timestamp sandwich.
958 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
959 if (ts0.tv_sec == ts1.tv_sec) {
960 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700961 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100962 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
963 if (ts1.tv_sec == ts2.tv_sec) {
964 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
965 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700966}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700967
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700968TEST(time, clock_gettime_CLOCK_REALTIME) {
969 timespec ts;
970 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
971}
972
973TEST(time, clock_gettime_CLOCK_MONOTONIC) {
974 timespec ts;
975 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
976}
977
978TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
979 timespec ts;
980 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
981}
982
983TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
984 timespec ts;
985 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
986}
987
988TEST(time, clock_gettime_CLOCK_BOOTTIME) {
989 timespec ts;
990 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
991}
992
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800993TEST(time, clock_gettime_unknown) {
994 errno = 0;
995 timespec ts;
996 ASSERT_EQ(-1, clock_gettime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700997 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800998}
999
1000TEST(time, clock_getres_CLOCK_REALTIME) {
1001 timespec ts;
1002 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
1003 ASSERT_EQ(1, ts.tv_nsec);
1004 ASSERT_EQ(0, ts.tv_sec);
1005}
1006
1007TEST(time, clock_getres_CLOCK_MONOTONIC) {
1008 timespec ts;
1009 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
1010 ASSERT_EQ(1, ts.tv_nsec);
1011 ASSERT_EQ(0, ts.tv_sec);
1012}
1013
1014TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
1015 timespec ts;
1016 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
1017}
1018
1019TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
1020 timespec ts;
1021 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
1022}
1023
1024TEST(time, clock_getres_CLOCK_BOOTTIME) {
1025 timespec ts;
1026 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
1027 ASSERT_EQ(1, ts.tv_nsec);
1028 ASSERT_EQ(0, ts.tv_sec);
1029}
1030
1031TEST(time, clock_getres_unknown) {
1032 errno = 0;
1033 timespec ts = { -1, -1 };
1034 ASSERT_EQ(-1, clock_getres(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001035 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -08001036 ASSERT_EQ(-1, ts.tv_nsec);
1037 ASSERT_EQ(-1, ts.tv_sec);
1038}
1039
zijunzhaoe6202662023-01-03 23:32:18 +00001040TEST(time, clock_getres_null_resolution) {
1041 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1042}
1043
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001044TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001045 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1046 static const clock_t N = 5;
1047 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001048 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001049 for (size_t i = 0; i < N; ++i) {
1050 sleep(1);
1051 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001052 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001053 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001054}
1055
Elliott Hughesb4413592017-11-29 18:17:06 -08001056static pid_t GetInvalidPid() {
1057 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001058 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001059 fscanf(fp.get(), "%ld", &pid_max);
1060 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001061}
1062
Elliott Hughesb4413592017-11-29 18:17:06 -08001063TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001064 clockid_t clockid;
1065 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001066 timespec ts;
1067 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001068}
Yabin Cuid5c65272014-11-26 14:04:26 -08001069
Elliott Hughesb4413592017-11-29 18:17:06 -08001070TEST(time, clock_getcpuclockid_parent) {
1071 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001072 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001073 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001074 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001075}
Yabin Cuid5c65272014-11-26 14:04:26 -08001076
Elliott Hughesb4413592017-11-29 18:17:06 -08001077TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001078 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1079 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001080 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001081 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001082 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1083 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1084 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1085 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1086 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1087 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Elliott Hughes95646e62023-09-21 14:11:19 -07001088 ASSERT_ERRNO(0);
Yabin Cuid5c65272014-11-26 14:04:26 -08001089}
1090
Haruki Hasegawa18160252014-10-13 00:50:47 +09001091TEST(time, clock_settime) {
1092 errno = 0;
1093 timespec ts;
1094 ASSERT_EQ(-1, clock_settime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001095 ASSERT_ERRNO(EINVAL);
Haruki Hasegawa18160252014-10-13 00:50:47 +09001096}
1097
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001098TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001099 timespec in;
1100 timespec out;
1101 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001102}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001103
1104TEST(time, clock_nanosleep_thread_cputime_id) {
1105 timespec in;
1106 in.tv_sec = 1;
1107 in.tv_nsec = 0;
1108 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1109}
Elliott Hughes12443702016-10-19 16:02:31 -07001110
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001111TEST(time, clock_nanosleep) {
1112 auto t0 = std::chrono::steady_clock::now();
1113 const timespec ts = {.tv_nsec = 5000000};
1114 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1115 auto t1 = std::chrono::steady_clock::now();
1116 ASSERT_GE(t1-t0, 5000000ns);
1117}
1118
1119TEST(time, nanosleep) {
1120 auto t0 = std::chrono::steady_clock::now();
1121 const timespec ts = {.tv_nsec = 5000000};
1122 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1123 auto t1 = std::chrono::steady_clock::now();
1124 ASSERT_GE(t1-t0, 5000000ns);
1125}
1126
1127TEST(time, nanosleep_EINVAL) {
1128 timespec ts = {.tv_sec = -1};
1129 errno = 0;
1130 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -07001131 ASSERT_ERRNO(EINVAL);
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001132}
1133
Elliott Hughes12443702016-10-19 16:02:31 -07001134TEST(time, bug_31938693) {
1135 // User-visible symptoms in N:
1136 // http://b/31938693
1137 // https://code.google.com/p/android/issues/detail?id=225132
1138
1139 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1140 // http://b/31848040
1141
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001142 // This isn't a great test, because very few timezones were actually affected, and there's
Elliott Hughes12443702016-10-19 16:02:31 -07001143 // no real logic to which ones were affected: it was just a coincidence of the data that came
1144 // after them in the tzdata file.
1145
1146 time_t t = 1475619727;
1147 struct tm tm;
1148
1149 setenv("TZ", "America/Los_Angeles", 1);
1150 tzset();
1151 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1152 EXPECT_EQ(15, tm.tm_hour);
1153
1154 setenv("TZ", "Europe/London", 1);
1155 tzset();
1156 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1157 EXPECT_EQ(23, tm.tm_hour);
1158
1159 setenv("TZ", "America/Atka", 1);
1160 tzset();
1161 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1162 EXPECT_EQ(13, tm.tm_hour);
1163
1164 setenv("TZ", "Pacific/Apia", 1);
1165 tzset();
1166 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1167 EXPECT_EQ(12, tm.tm_hour);
1168
1169 setenv("TZ", "Pacific/Honolulu", 1);
1170 tzset();
1171 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1172 EXPECT_EQ(12, tm.tm_hour);
1173
1174 setenv("TZ", "Asia/Magadan", 1);
1175 tzset();
1176 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1177 EXPECT_EQ(9, tm.tm_hour);
1178}
Elliott Hughesea877162017-01-11 14:34:16 -08001179
1180TEST(time, bug_31339449) {
1181 // POSIX says localtime acts as if it calls tzset.
1182 // tzset does two things:
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001183 // 1. it sets the timezone ctime/localtime/mktime/strftime will use.
Elliott Hughesea877162017-01-11 14:34:16 -08001184 // 2. it sets the global `tzname`.
1185 // POSIX says localtime_r need not set `tzname` (2).
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001186 // Q: should localtime_r set the timezone (1)?
Elliott Hughesea877162017-01-11 14:34:16 -08001187 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1188
1189 // Pick a time, any time...
1190 time_t t = 1475619727;
1191
1192 // Call tzset with a specific timezone.
1193 setenv("TZ", "America/Atka", 1);
1194 tzset();
1195
1196 // If we change the timezone and call localtime, localtime should use the new timezone.
1197 setenv("TZ", "America/Los_Angeles", 1);
1198 struct tm* tm_p = localtime(&t);
1199 EXPECT_EQ(15, tm_p->tm_hour);
1200
1201 // Reset the timezone back.
1202 setenv("TZ", "America/Atka", 1);
1203 tzset();
1204
1205#if defined(__BIONIC__)
1206 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1207 setenv("TZ", "America/Los_Angeles", 1);
1208 struct tm tm = {};
1209 localtime_r(&t, &tm);
1210 EXPECT_EQ(15, tm.tm_hour);
1211#else
1212 // The BSDs agree with us, but glibc gets this wrong.
1213#endif
1214}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001215
1216TEST(time, asctime) {
1217 const struct tm tm = {};
1218 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1219}
1220
1221TEST(time, asctime_r) {
1222 const struct tm tm = {};
1223 char buf[256];
1224 ASSERT_EQ(buf, asctime_r(&tm, buf));
1225 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1226}
1227
1228TEST(time, ctime) {
1229 setenv("TZ", "UTC", 1);
1230 const time_t t = 0;
1231 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1232}
1233
1234TEST(time, ctime_r) {
1235 setenv("TZ", "UTC", 1);
1236 const time_t t = 0;
1237 char buf[256];
1238 ASSERT_EQ(buf, ctime_r(&t, buf));
1239 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1240}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001241
1242// https://issuetracker.google.com/37128336
1243TEST(time, strftime_strptime_s) {
1244 char buf[32];
1245 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1246
1247 setenv("TZ", "America/Los_Angeles", 1);
1248 strftime(buf, sizeof(buf), "<%s>", &tm0);
1249 EXPECT_STREQ("<378720000>", buf);
1250
1251 setenv("TZ", "UTC", 1);
1252 strftime(buf, sizeof(buf), "<%s>", &tm0);
1253 EXPECT_STREQ("<378691200>", buf);
1254
1255 struct tm tm;
1256
1257 setenv("TZ", "America/Los_Angeles", 1);
1258 tzset();
1259 memset(&tm, 0xff, sizeof(tm));
1260 char* p = strptime("378720000x", "%s", &tm);
1261 ASSERT_EQ('x', *p);
1262 EXPECT_EQ(0, tm.tm_sec);
1263 EXPECT_EQ(0, tm.tm_min);
1264 EXPECT_EQ(0, tm.tm_hour);
1265 EXPECT_EQ(1, tm.tm_mday);
1266 EXPECT_EQ(0, tm.tm_mon);
1267 EXPECT_EQ(82, tm.tm_year);
1268 EXPECT_EQ(5, tm.tm_wday);
1269 EXPECT_EQ(0, tm.tm_yday);
1270 EXPECT_EQ(0, tm.tm_isdst);
1271
1272 setenv("TZ", "UTC", 1);
1273 tzset();
1274 memset(&tm, 0xff, sizeof(tm));
1275 p = strptime("378691200x", "%s", &tm);
1276 ASSERT_EQ('x', *p);
1277 EXPECT_EQ(0, tm.tm_sec);
1278 EXPECT_EQ(0, tm.tm_min);
1279 EXPECT_EQ(0, tm.tm_hour);
1280 EXPECT_EQ(1, tm.tm_mday);
1281 EXPECT_EQ(0, tm.tm_mon);
1282 EXPECT_EQ(82, tm.tm_year);
1283 EXPECT_EQ(5, tm.tm_wday);
1284 EXPECT_EQ(0, tm.tm_yday);
1285 EXPECT_EQ(0, tm.tm_isdst);
1286}
1287
1288TEST(time, strptime_s_nothing) {
1289 struct tm tm;
1290 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1291}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001292
1293TEST(time, timespec_get) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001294#if defined(__BIONIC__)
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001295 timespec ts = {};
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001296 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001297 ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC));
1298 ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE));
1299 ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE));
1300#else
1301 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1302#endif
1303}
1304
1305TEST(time, timespec_get_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001306#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001307 timespec ts = {};
Elliott Hughes7db0a6c2023-05-03 15:37:46 -07001308 ASSERT_EQ(0, timespec_get(&ts, 123));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001309#else
1310 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1311#endif
1312}
1313
1314TEST(time, timespec_getres) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001315#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001316 timespec ts = {};
1317 ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC));
1318 ASSERT_EQ(1, ts.tv_nsec);
1319 ASSERT_EQ(0, ts.tv_sec);
1320#else
1321 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1322#endif
1323}
1324
1325TEST(time, timespec_getres_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001326#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001327 timespec ts = {};
1328 ASSERT_EQ(0, timespec_getres(&ts, 123));
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001329#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001330 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001331#endif
1332}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001333
1334TEST(time, difftime) {
1335 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001336 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001337}
Elliott Hughes2bd43162023-06-15 13:17:08 -07001338
1339TEST(time, tzfree_null) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001340#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001341 tzfree(nullptr);
1342#else
1343 GTEST_SKIP() << "glibc doesn't have timezone_t";
1344#endif
1345}
1346
1347TEST(time, localtime_rz) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001348#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001349 setenv("TZ", "America/Los_Angeles", 1);
1350 tzset();
1351
1352 auto AssertTmEq = [](const struct tm& rhs, int hour) {
1353 ASSERT_EQ(93, rhs.tm_year);
1354 ASSERT_EQ(0, rhs.tm_mon);
1355 ASSERT_EQ(1, rhs.tm_mday);
1356 ASSERT_EQ(hour, rhs.tm_hour);
1357 ASSERT_EQ(0, rhs.tm_min);
1358 ASSERT_EQ(0, rhs.tm_sec);
1359 };
1360
1361 const time_t t = 725875200;
1362
1363 // Spam localtime_r() while we use localtime_rz().
1364 std::atomic<bool> done = false;
1365 std::thread thread{[&] {
1366 while (!done) {
1367 struct tm tm {};
1368 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1369 AssertTmEq(tm, 0);
1370 }
1371 }};
1372
1373 struct tm tm;
1374
1375 timezone_t london{tzalloc("Europe/London")};
1376 tm = {};
1377 ASSERT_EQ(&tm, localtime_rz(london, &t, &tm));
1378 AssertTmEq(tm, 8);
1379
1380 timezone_t seoul{tzalloc("Asia/Seoul")};
1381 tm = {};
1382 ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm));
1383 AssertTmEq(tm, 17);
1384
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001385 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001386 tm = {};
1387 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1388 ASSERT_EQ(0, tm.tm_hour);
1389 AssertTmEq(tm, 0);
1390
1391 done = true;
1392 thread.join();
1393
1394 tzfree(london);
1395 tzfree(seoul);
1396#else
1397 GTEST_SKIP() << "glibc doesn't have timezone_t";
1398#endif
1399}
1400
1401TEST(time, mktime_z) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001402#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001403 setenv("TZ", "America/Los_Angeles", 1);
1404 tzset();
1405
1406 // Spam mktime() while we use mktime_z().
1407 std::atomic<bool> done = false;
1408 std::thread thread{[&done] {
1409 while (!done) {
1410 struct tm tm {
1411 .tm_year = 93, .tm_mday = 1
1412 };
1413 ASSERT_EQ(725875200, mktime(&tm));
1414 }
1415 }};
1416
1417 struct tm tm;
1418
1419 timezone_t london{tzalloc("Europe/London")};
1420 tm = {.tm_year = 93, .tm_mday = 1};
1421 ASSERT_EQ(725846400, mktime_z(london, &tm));
1422
1423 timezone_t seoul{tzalloc("Asia/Seoul")};
1424 tm = {.tm_year = 93, .tm_mday = 1};
1425 ASSERT_EQ(725814000, mktime_z(seoul, &tm));
1426
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001427 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001428 tm = {.tm_year = 93, .tm_mday = 1};
1429 ASSERT_EQ(725875200, mktime(&tm));
1430
1431 done = true;
1432 thread.join();
1433
1434 tzfree(london);
1435 tzfree(seoul);
1436#else
1437 GTEST_SKIP() << "glibc doesn't have timezone_t";
1438#endif
1439}
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001440
1441TEST(time, tzalloc_nullptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001442#if defined(__BIONIC__)
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001443 // tzalloc(nullptr) returns the system timezone.
1444 timezone_t default_tz = tzalloc(nullptr);
1445 ASSERT_NE(nullptr, default_tz);
1446
1447 // Check that mktime_z() with the default timezone matches mktime().
1448 // This assumes that the system timezone doesn't change during the test,
1449 // but that should be unlikely, and we don't have much choice if we
1450 // want to write a test at all.
1451 // We unset $TZ before calling mktime() because mktime() honors $TZ.
1452 unsetenv("TZ");
1453 struct tm tm = {.tm_year = 93, .tm_mday = 1};
1454 time_t t = mktime(&tm);
1455 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1456
1457 // Check that changing $TZ doesn't affect the tzalloc() default in
1458 // the same way it would the mktime() default.
1459 setenv("TZ", "America/Los_Angeles", 1);
1460 tzset();
1461 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1462
1463 setenv("TZ", "Europe/London", 1);
1464 tzset();
1465 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1466
1467 setenv("TZ", "Asia/Seoul", 1);
1468 tzset();
1469 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1470
1471 tzfree(default_tz);
1472#else
1473 GTEST_SKIP() << "glibc doesn't have timezone_t";
1474#endif
1475}
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001476
1477TEST(time, tzalloc_unique_ptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001478#if defined(__BIONIC__)
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001479 std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"),
1480 tzfree};
1481#else
1482 GTEST_SKIP() << "glibc doesn't have timezone_t";
1483#endif
1484}