blob: 5f802aa80721414a26d7fd959634103b5790c2ed [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 Hughese0175ca2013-03-14 14:38:08 -070031
Elliott Hughes71ba5892018-02-07 12:44:45 -080032#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080033#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070034
Elliott Hughesca3f8e42019-10-28 15:59:38 -070035using namespace std::chrono_literals;
36
Mark Salyzyn0b846e82017-12-20 08:56:18 -080037TEST(time, time) {
38 // Acquire time
39 time_t p1, t1 = time(&p1);
40 // valid?
41 ASSERT_NE(static_cast<time_t>(0), t1);
42 ASSERT_NE(static_cast<time_t>(-1), t1);
43 ASSERT_EQ(p1, t1);
44
45 // Acquire time one+ second later
46 usleep(1010000);
47 time_t p2, t2 = time(&p2);
48 // valid?
49 ASSERT_NE(static_cast<time_t>(0), t2);
50 ASSERT_NE(static_cast<time_t>(-1), t2);
51 ASSERT_EQ(p2, t2);
52
53 // Expect time progression
54 ASSERT_LT(p1, p2);
55 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
56
57 // Expect nullptr call to produce same results
58 ASSERT_LE(t2, time(nullptr));
59 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
60}
61
Elliott Hughesee178bf2013-07-12 11:25:20 -070062TEST(time, gmtime) {
63 time_t t = 0;
64 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070065 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070066 ASSERT_EQ(0, broken_down->tm_sec);
67 ASSERT_EQ(0, broken_down->tm_min);
68 ASSERT_EQ(0, broken_down->tm_hour);
69 ASSERT_EQ(1, broken_down->tm_mday);
70 ASSERT_EQ(0, broken_down->tm_mon);
71 ASSERT_EQ(1970, broken_down->tm_year + 1900);
72}
Elliott Hughes7843d442013-08-22 11:37:32 -070073
Elliott Hughes5a29d542017-12-07 16:05:57 -080074TEST(time, gmtime_r) {
75 struct tm tm = {};
76 time_t t = 0;
77 struct tm* broken_down = gmtime_r(&t, &tm);
78 ASSERT_EQ(broken_down, &tm);
79 ASSERT_EQ(0, broken_down->tm_sec);
80 ASSERT_EQ(0, broken_down->tm_min);
81 ASSERT_EQ(0, broken_down->tm_hour);
82 ASSERT_EQ(1, broken_down->tm_mday);
83 ASSERT_EQ(0, broken_down->tm_mon);
84 ASSERT_EQ(1970, broken_down->tm_year + 1900);
85}
86
Almaz Mingaleevda75bb62022-06-29 15:47:37 +000087TEST(time, mktime_TZ_as_UTC_and_offset) {
88 struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1};
89
90 // This TZ value is not a valid Olson ID and is not present in tzdata file,
91 // but is a valid TZ string according to POSIX standard.
92 setenv("TZ", "UTC+08:00:00", 1);
93 tzset();
94 ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm));
95}
96
Elliott Hughes329103d2014-04-25 16:55:04 -070097static void* gmtime_no_stack_overflow_14313703_fn(void*) {
98 const char* original_tz = getenv("TZ");
99 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
100 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
101 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700102 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -0700103 setenv("TZ", original_tz, 1);
104 }
105 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700106 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -0700107}
108
109TEST(time, gmtime_no_stack_overflow_14313703) {
110 // Is it safe to call tzload on a thread with a small stack?
111 // http://b/14313703
112 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800113 pthread_attr_t a;
114 ASSERT_EQ(0, pthread_attr_init(&a));
115 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700116
117 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700118 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800119 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700120}
121
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900122TEST(time, mktime_empty_TZ) {
123 // tzcode used to have a bug where it didn't reinitialize some internal state.
124
125 // Choose a time where DST is set.
126 struct tm t;
127 memset(&t, 0, sizeof(tm));
128 t.tm_year = 1980 - 1900;
129 t.tm_mon = 6;
130 t.tm_mday = 2;
131
132 setenv("TZ", "America/Los_Angeles", 1);
133 tzset();
134 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
135
136 memset(&t, 0, sizeof(tm));
137 t.tm_year = 1980 - 1900;
138 t.tm_mon = 6;
139 t.tm_mday = 2;
140
141 setenv("TZ", "", 1); // Implies UTC.
142 tzset();
143 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
144}
145
Elliott Hughes7843d442013-08-22 11:37:32 -0700146TEST(time, mktime_10310929) {
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100147 struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10};
Elliott Hughes7843d442013-08-22 11:37:32 -0700148
Elliott Hughes0c401522013-10-18 16:21:54 -0700149#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700150 // 32-bit bionic has a signed 32-bit time_t.
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100151 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700152 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700153#else
154 // Everyone else should be using a signed 64-bit time_t.
155 ASSERT_GE(sizeof(time_t) * 8, 64U);
156
157 setenv("TZ", "America/Los_Angeles", 1);
158 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700159 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700160
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100161 // On the date/time specified by tm America/Los_Angeles
162 // follows DST. But tm_isdst is set to 0, which forces
163 // mktime to interpret that time as local standard, hence offset
164 // is 8 hours, not 7.
165 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700166 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700167#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800168}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800169
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700170TEST(time, mktime_EOVERFLOW) {
Elliott Hughesdb123a72023-05-19 16:09:47 -0700171 setenv("TZ", "UTC", 1);
172
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700173 struct tm t;
174 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700175
176 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
177 t.tm_year = 2016 - 1900;
178
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700179 t.tm_mon = 2;
180 t.tm_mday = 10;
181
182 errno = 0;
183 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
184 ASSERT_EQ(0, errno);
185
Elliott Hughes47126ed2016-09-06 13:25:53 -0700186 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700187 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700188
189 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700190 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
191 ASSERT_EQ(EOVERFLOW, errno);
192}
193
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000194TEST(time, mktime_invalid_tm_TZ_combination) {
195 setenv("TZ", "UTC", 1);
196
197 struct tm t;
198 memset(&t, 0, sizeof(tm));
199 t.tm_year = 2022 - 1900;
200 t.tm_mon = 11;
201 t.tm_mday = 31;
202 // UTC does not observe DST
203 t.tm_isdst = 1;
204
205 errno = 0;
206
207 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
208 // mktime sets errno to EOVERFLOW if result is unrepresentable.
209 EXPECT_EQ(EOVERFLOW, errno);
210}
211
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100212// Transitions in the tzdata file are generated up to the year 2100. Testing
213// that dates beyond that are handled properly too.
214TEST(time, mktime_after_2100) {
215 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
216
217#if !defined(__LP64__)
218 // 32-bit bionic has a signed 32-bit time_t.
219 ASSERT_EQ(-1, mktime(&tm));
220 ASSERT_EQ(EOVERFLOW, errno);
221#else
222 setenv("TZ", "Europe/London", 1);
223 tzset();
224 errno = 0;
225
226 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
227 ASSERT_EQ(0, errno);
228#endif
229}
230
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700231TEST(time, strftime) {
232 setenv("TZ", "UTC", 1);
233
234 struct tm t;
235 memset(&t, 0, sizeof(tm));
236 t.tm_year = 200;
237 t.tm_mon = 2;
238 t.tm_mday = 10;
239
240 char buf[64];
241
242 // Seconds since the epoch.
243#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
244 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
245 EXPECT_STREQ("4108320000", buf);
246#endif
247
248 // Date and time as text.
249 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
250 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
251}
252
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000253TEST(time, strftime_second_before_epoch) {
254 setenv("TZ", "UTC", 1);
255
256 struct tm t;
257 memset(&t, 0, sizeof(tm));
258 t.tm_year = 1969 - 1900;
259 t.tm_mon = 11;
260 t.tm_mday = 31;
261 t.tm_hour = 23;
262 t.tm_min = 59;
263 t.tm_sec = 59;
264
265 char buf[64];
266
267 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
268 EXPECT_STREQ("-1", buf);
269}
270
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100271TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800272 // Netflix on Nexus Player wouldn't start (http://b/25170306).
273 struct tm t;
274 memset(&t, 0, sizeof(tm));
275
276 char buf[64];
277
278 setenv("TZ", "America/Los_Angeles", 1);
279 tzset();
280
281 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
282 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
283 EXPECT_STREQ("<PST>", buf);
284
285#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
286 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
287 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
288 EXPECT_STREQ("<PDT>", buf);
289
290 t.tm_isdst = -123; // "and negative if the information is not available".
291 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
292 EXPECT_STREQ("<>", buf);
293#endif
294
295 setenv("TZ", "UTC", 1);
296 tzset();
297
298 t.tm_isdst = 0;
299 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
300 EXPECT_STREQ("<UTC>", buf);
301
302#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
303 t.tm_isdst = 1; // UTC has no DST.
304 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
305 EXPECT_STREQ("<>", buf);
306#endif
307}
308
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100309// According to C language specification the only tm struct field needed to
310// find out replacement for %z and %Z in strftime is tm_isdst. Which is
311// wrong, as time zones change their standard offset and even DST savings.
312// tzcode deviates from C language specification and requires tm struct either
313// to be output of localtime-like functions or to be modified by mktime call
314// before passing to strftime. See tz mailing discussion for more details
315// https://mm.icann.org/pipermail/tz/2022-July/031674.html
316// But we are testing case when tm.tm_zone is null, which means that tm struct
317// is not coming from localtime and is neither modified by mktime. That's why
318// we are comparing against +0000, even though America/Los_Angeles never
319// observes it.
320TEST(time, strftime_z_null_tm_zone) {
321 char str[64];
322 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
323
324 setenv("TZ", "America/Los_Angeles", 1);
325 tzset();
326
327 tm.tm_zone = NULL;
328
329 size_t result = strftime(str, sizeof(str), "%z", &tm);
330
331 EXPECT_EQ(5U, result);
332 EXPECT_STREQ("+0000", str);
333
334 tm.tm_isdst = 1;
335
336 result = strftime(str, sizeof(str), "%z", &tm);
337
338 EXPECT_EQ(5U, result);
339 EXPECT_STREQ("+0000", str);
340
341 setenv("TZ", "UTC", 1);
342 tzset();
343
344 tm.tm_isdst = 0;
345
346 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
359TEST(time, strftime_z_Europe_Lisbon) {
360 char str[64];
361 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
362 // tm_isdst is not set as it will be overridden by mktime call anyway.
363 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
364
365 setenv("TZ", "Europe/Lisbon", 1);
366 tzset();
367
368 // tzcode's strftime implementation for %z relies on prior mktime call.
369 // At the moment of writing %z value is taken from tm_gmtoff. So without
370 // mktime call %z is replaced with +0000.
371 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
372 mktime(&tm);
373
374 size_t result = strftime(str, sizeof(str), "%z", &tm);
375
376 EXPECT_EQ(5U, result);
377 EXPECT_STREQ("+0100", str);
378
379 // Now standard offset is 0.
380 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
381
382 mktime(&tm);
383 result = strftime(str, sizeof(str), "%z", &tm);
384
385 EXPECT_EQ(5U, result);
386 EXPECT_STREQ("+0000", str);
387}
388
Elliott Hughes0a610d02016-07-29 14:04:17 -0700389TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700390 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700391 locale_t old_locale = uselocale(cloc);
392
393 setenv("TZ", "UTC", 1);
394
395 struct tm t;
396 memset(&t, 0, sizeof(tm));
397 t.tm_year = 200;
398 t.tm_mon = 2;
399 t.tm_mday = 10;
400
401 // Date and time as text.
402 char buf[64];
403 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
404 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
405
406 uselocale(old_locale);
407 freelocale(cloc);
408}
409
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700410TEST(time, strptime) {
411 setenv("TZ", "UTC", 1);
412
413 struct tm t;
414 char buf[64];
415
416 memset(&t, 0, sizeof(t));
417 strptime("11:14", "%R", &t);
418 strftime(buf, sizeof(buf), "%H:%M", &t);
419 EXPECT_STREQ("11:14", buf);
420
421 memset(&t, 0, sizeof(t));
422 strptime("09:41:53", "%T", &t);
423 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
424 EXPECT_STREQ("09:41:53", buf);
425}
426
Elliott Hughes3376c232018-02-13 23:14:12 -0800427TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700428#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800429 setenv("TZ", "UTC", 1);
430
431 struct tm t;
432 char buf[64];
433
434 memset(&t, 0, sizeof(t));
435 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
436 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
437 EXPECT_STREQ("11:14", buf);
438
439 memset(&t, 0, sizeof(t));
440 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
441 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
442 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700443#else
444 GTEST_SKIP() << "musl doesn't support strptime_l";
445#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800446}
447
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700448TEST(time, strptime_F) {
449 setenv("TZ", "UTC", 1);
450
451 struct tm tm = {};
452 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
453 EXPECT_EQ(119, tm.tm_year);
454 EXPECT_EQ(2, tm.tm_mon);
455 EXPECT_EQ(26, tm.tm_mday);
456}
457
458TEST(time, strptime_P_p) {
459 setenv("TZ", "UTC", 1);
460
Elliott Hughes11678822019-03-27 08:56:49 -0700461 // For parsing, %P and %p are the same: case doesn't matter.
462
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700463 struct tm tm = {.tm_hour = 12};
464 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
465 EXPECT_EQ(0, tm.tm_hour);
466
467 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700468 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
469 EXPECT_EQ(0, tm.tm_hour);
470
471 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700472 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
473 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700474
475 tm = {.tm_hour = 12};
476 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
477 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700478}
479
480TEST(time, strptime_u) {
481 setenv("TZ", "UTC", 1);
482
483 struct tm tm = {};
484 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
485 EXPECT_EQ(2, tm.tm_wday);
486}
487
488TEST(time, strptime_v) {
489 setenv("TZ", "UTC", 1);
490
491 struct tm tm = {};
492 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
493 EXPECT_EQ(80, tm.tm_year);
494 EXPECT_EQ(2, tm.tm_mon);
495 EXPECT_EQ(26, tm.tm_mday);
496}
497
498TEST(time, strptime_V_G_g) {
499 setenv("TZ", "UTC", 1);
500
501 // %V (ISO-8601 week number), %G (year of week number, without century), and
502 // %g (year of week number) have no effect when parsed, and are supported
503 // solely so that it's possible for strptime(3) to parse everything that
504 // strftime(3) can output.
505 struct tm tm = {};
506 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
507 struct tm zero = {};
508 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
509}
510
Elliott Hughesd065c042020-09-01 19:02:44 -0700511TEST(time, strptime_Z) {
512#if defined(__BIONIC__)
513 // glibc doesn't handle %Z at all.
514 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
515 // are in the global `tzname` (which correspond to the current $TZ).
516 struct tm tm;
517 setenv("TZ", "Europe/Berlin", 1);
518
519 // "GMT" always works.
520 tm = {};
521 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
522 EXPECT_STREQ("GMT", tm.tm_zone);
523 EXPECT_EQ(0, tm.tm_isdst);
524 EXPECT_EQ(0, tm.tm_gmtoff);
525
526 // As does "UTC".
527 tm = {};
528 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
529 EXPECT_STREQ("UTC", tm.tm_zone);
530 EXPECT_EQ(0, tm.tm_isdst);
531 EXPECT_EQ(0, tm.tm_gmtoff);
532
533 // Europe/Berlin is known as "CET" when there's no DST.
534 tm = {};
535 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
536 EXPECT_STREQ("CET", tm.tm_zone);
537 EXPECT_EQ(0, tm.tm_isdst);
538 EXPECT_EQ(3600, tm.tm_gmtoff);
539
540 // Europe/Berlin is known as "CEST" when there's no DST.
541 tm = {};
542 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
543 EXPECT_STREQ("CEST", tm.tm_zone);
544 EXPECT_EQ(1, tm.tm_isdst);
545 EXPECT_EQ(3600, tm.tm_gmtoff);
546
547 // And as long as we're in Europe/Berlin, those are the only time zone
548 // abbreviations that are recognized.
549 tm = {};
550 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
551#endif
552}
553
554TEST(time, strptime_z) {
555 struct tm tm;
556 setenv("TZ", "Europe/Berlin", 1);
557
558 // "UT" is what RFC822 called UTC.
559 tm = {};
560 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
561 EXPECT_STREQ("UTC", tm.tm_zone);
562 EXPECT_EQ(0, tm.tm_isdst);
563 EXPECT_EQ(0, tm.tm_gmtoff);
564 // "GMT" is RFC822's other name for UTC.
565 tm = {};
566 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
567 EXPECT_STREQ("UTC", tm.tm_zone);
568 EXPECT_EQ(0, tm.tm_isdst);
569 EXPECT_EQ(0, tm.tm_gmtoff);
570
571 // "Z" ("Zulu") is a synonym for UTC.
572 tm = {};
573 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
574 EXPECT_STREQ("UTC", tm.tm_zone);
575 EXPECT_EQ(0, tm.tm_isdst);
576 EXPECT_EQ(0, tm.tm_gmtoff);
577
578 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
579 tm = {};
580 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
581 EXPECT_STREQ("PST", tm.tm_zone);
582 EXPECT_EQ(0, tm.tm_isdst);
583 EXPECT_EQ(-28800, tm.tm_gmtoff);
584 tm = {};
585 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
586 EXPECT_STREQ("PDT", tm.tm_zone);
587 EXPECT_EQ(1, tm.tm_isdst);
588 EXPECT_EQ(-25200, tm.tm_gmtoff);
589
590 // +-hh
591 tm = {};
592 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
593 EXPECT_EQ(3600, tm.tm_gmtoff);
594 EXPECT_TRUE(tm.tm_zone == nullptr);
595 EXPECT_EQ(0, tm.tm_isdst);
596 // +-hhmm
597 tm = {};
598 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
599 EXPECT_EQ(5400, tm.tm_gmtoff);
600 EXPECT_TRUE(tm.tm_zone == nullptr);
601 EXPECT_EQ(0, tm.tm_isdst);
602 // +-hh:mm
603 tm = {};
604 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
605 EXPECT_EQ(5400, tm.tm_gmtoff);
606 EXPECT_TRUE(tm.tm_zone == nullptr);
607 EXPECT_EQ(0, tm.tm_isdst);
608}
609
Elliott Hughes4b558f52014-03-04 15:58:02 -0800610void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
611 itimerspec ts;
612 ts.it_value.tv_sec = value_s;
613 ts.it_value.tv_nsec = value_ns;
614 ts.it_interval.tv_sec = interval_s;
615 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700616 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800617}
618
Colin Cross7da20342021-07-28 11:18:11 -0700619static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800620}
621
622TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700623 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800624 memset(&se, 0, sizeof(se));
625 se.sigev_notify = SIGEV_THREAD;
626 se.sigev_notify_function = NoOpNotifyFunction;
627 timer_t timer_id;
628 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
629
Elliott Hughes33697a02016-01-26 13:04:57 -0800630 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800631 ASSERT_NE(-1, pid) << strerror(errno);
632
633 if (pid == 0) {
634 // Timers are not inherited by the child.
635 ASSERT_EQ(-1, timer_delete(timer_id));
636 ASSERT_EQ(EINVAL, errno);
637 _exit(0);
638 }
639
Elliott Hughes33697a02016-01-26 13:04:57 -0800640 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800641
642 ASSERT_EQ(0, timer_delete(timer_id));
643}
644
Yabin Cui95f1ee22015-01-13 19:53:15 -0800645static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800646static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
647 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
648 ASSERT_EQ(SIGUSR1, signal_number);
649}
650
651TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700652 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800653 memset(&se, 0, sizeof(se));
654 se.sigev_notify = SIGEV_SIGNAL;
655 se.sigev_signo = SIGUSR1;
656
657 timer_t timer_id;
658 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
659
Yabin Cui95f1ee22015-01-13 19:53:15 -0800660 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800661 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
662
663 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
664
665 itimerspec ts;
666 ts.it_value.tv_sec = 0;
667 ts.it_value.tv_nsec = 1;
668 ts.it_interval.tv_sec = 0;
669 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700670 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800671
672 usleep(500000);
673 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
674}
675
676struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800677 private:
678 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800679 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700680 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700681 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800682
Elliott Hughes4b558f52014-03-04 15:58:02 -0800683 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700684 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800685 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700686 timer_valid = true;
687 }
688
Yabin Cui95f1ee22015-01-13 19:53:15 -0800689 public:
Colin Cross7da20342021-07-28 11:18:11 -0700690 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800691 memset(&se, 0, sizeof(se));
692 se.sigev_notify = SIGEV_THREAD;
693 se.sigev_notify_function = fn;
694 se.sigev_value.sival_ptr = this;
695 Create();
696 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700697 void DeleteTimer() {
698 ASSERT_TRUE(timer_valid);
699 ASSERT_EQ(0, timer_delete(timer_id));
700 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800701 }
702
703 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700704 if (timer_valid) {
705 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800706 }
707 }
708
Yabin Cui95f1ee22015-01-13 19:53:15 -0800709 int Value() const {
710 return value;
711 }
712
Christopher Ferris62d84b12014-10-20 19:09:19 -0700713 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
714 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
715 }
716
717 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800718 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700719 time_t start = time(nullptr);
720 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700721 }
722 return current_value != value;
723 }
724
Colin Cross7da20342021-07-28 11:18:11 -0700725 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800726 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
727 ++cd->value;
728 }
729
Colin Cross7da20342021-07-28 11:18:11 -0700730 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800731 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
732 ++cd->value;
733
734 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700735 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800736 }
737};
738
739TEST(time, timer_settime_0) {
740 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800741 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800742
Yabin Cuibf572d92015-08-11 11:23:16 -0700743 counter.SetTime(0, 500000000, 1, 0);
744 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800745
746 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800747 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800748}
749
750TEST(time, timer_settime_repeats) {
751 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800752 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800753
Christopher Ferris62d84b12014-10-20 19:09:19 -0700754 counter.SetTime(0, 1, 0, 10);
755 ASSERT_TRUE(counter.ValueUpdated());
756 ASSERT_TRUE(counter.ValueUpdated());
757 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800758 counter.DeleteTimer();
759 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
760 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800761}
762
Yabin Cui95f1ee22015-01-13 19:53:15 -0800763static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800764static void timer_create_NULL_signal_handler(int signal_number) {
765 ++timer_create_NULL_signal_handler_invocation_count;
766 ASSERT_EQ(SIGALRM, signal_number);
767}
768
769TEST(time, timer_create_NULL) {
770 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
771 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700772 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800773
Yabin Cui95f1ee22015-01-13 19:53:15 -0800774 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800775 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
776
777 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
778
779 SetTime(timer_id, 0, 1, 0, 0);
780 usleep(500000);
781
782 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
783}
784
785TEST(time, timer_create_EINVAL) {
786 clockid_t invalid_clock = 16;
787
788 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
789 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700790 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800791 ASSERT_EQ(EINVAL, errno);
792
793 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700794 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800795 memset(&se, 0, sizeof(se));
796 se.sigev_notify = SIGEV_THREAD;
797 se.sigev_notify_function = NoOpNotifyFunction;
798 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
799 ASSERT_EQ(EINVAL, errno);
800}
801
Elliott Hughes4b558f52014-03-04 15:58:02 -0800802TEST(time, timer_create_multiple) {
803 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800804 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800805 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800806
Yabin Cui95f1ee22015-01-13 19:53:15 -0800807 ASSERT_EQ(0, counter1.Value());
808 ASSERT_EQ(0, counter2.Value());
809 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800810
Yabin Cui410c1ad2015-06-18 16:19:02 -0700811 counter2.SetTime(0, 500000000, 0, 0);
812 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800813
Yabin Cui95f1ee22015-01-13 19:53:15 -0800814 EXPECT_EQ(0, counter1.Value());
815 EXPECT_EQ(1, counter2.Value());
816 EXPECT_EQ(0, counter3.Value());
817}
818
819// Test to verify that disarming a repeatable timer disables the callbacks.
820TEST(time, timer_disarm_terminates) {
821 Counter counter(Counter::CountNotifyFunction);
822 ASSERT_EQ(0, counter.Value());
823
824 counter.SetTime(0, 1, 0, 1);
825 ASSERT_TRUE(counter.ValueUpdated());
826 ASSERT_TRUE(counter.ValueUpdated());
827 ASSERT_TRUE(counter.ValueUpdated());
828
829 counter.SetTime(0, 0, 0, 0);
830 // Add a sleep as the kernel may have pending events when the timer is disarmed.
831 usleep(500000);
832 int value = counter.Value();
833 usleep(500000);
834
835 // Verify the counter has not been incremented.
836 ASSERT_EQ(value, counter.Value());
837}
838
839// Test to verify that deleting a repeatable timer disables the callbacks.
840TEST(time, timer_delete_terminates) {
841 Counter counter(Counter::CountNotifyFunction);
842 ASSERT_EQ(0, counter.Value());
843
844 counter.SetTime(0, 1, 0, 1);
845 ASSERT_TRUE(counter.ValueUpdated());
846 ASSERT_TRUE(counter.ValueUpdated());
847 ASSERT_TRUE(counter.ValueUpdated());
848
849 counter.DeleteTimer();
850 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
851 usleep(500000);
852 int value = counter.Value();
853 usleep(500000);
854
855 // Verify the counter has not been incremented.
856 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800857}
Christopher Ferris753ad772014-03-20 20:47:45 -0700858
859struct TimerDeleteData {
860 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800861 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700862 volatile bool complete;
863};
864
Colin Cross7da20342021-07-28 11:18:11 -0700865static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700866 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
867
Elliott Hughes11859d42017-02-13 17:59:29 -0800868 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700869 timer_delete(tdd->timer_id);
870 tdd->complete = true;
871}
872
873TEST(time, timer_delete_from_timer_thread) {
874 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700875 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700876
877 memset(&se, 0, sizeof(se));
878 se.sigev_notify = SIGEV_THREAD;
879 se.sigev_notify_function = TimerDeleteCallback;
880 se.sigev_value.sival_ptr = &tdd;
881
882 tdd.complete = false;
883 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
884
885 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800886 ts.it_value.tv_sec = 1;
887 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700888 ts.it_interval.tv_sec = 0;
889 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700890 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700891
Yi Kong32bc0fc2018-08-02 17:31:13 -0700892 time_t cur_time = time(nullptr);
893 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700894 ASSERT_TRUE(tdd.complete);
895
896#if defined(__BIONIC__)
897 // Since bionic timers are implemented by creating a thread to handle the
898 // callback, verify that the thread actually completes.
899 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800900 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
901 ASSERT_EQ(-1, kill(tdd.tid, 0));
902 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700903#endif
904}
Elliott Hughes625993d2014-07-15 16:53:13 -0700905
Colin Cross35d469b2021-08-16 15:26:28 -0700906// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
907#if !defined(__NR_clock_gettime)
908#define __NR_clock_gettime __NR_clock_gettime32
909#endif
910
Elliott Hughes625993d2014-07-15 16:53:13 -0700911TEST(time, clock_gettime) {
912 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100913 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700914 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700915 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100916 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
917 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
918 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700919
Giuliano Procida096f5952021-04-08 10:51:58 +0100920 // Check we have a nice monotonic timestamp sandwich.
921 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
922 if (ts0.tv_sec == ts1.tv_sec) {
923 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700924 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100925 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
926 if (ts1.tv_sec == ts2.tv_sec) {
927 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
928 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700929}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700930
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700931TEST(time, clock_gettime_CLOCK_REALTIME) {
932 timespec ts;
933 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
934}
935
936TEST(time, clock_gettime_CLOCK_MONOTONIC) {
937 timespec ts;
938 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
939}
940
941TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
942 timespec ts;
943 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
944}
945
946TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
947 timespec ts;
948 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
949}
950
951TEST(time, clock_gettime_CLOCK_BOOTTIME) {
952 timespec ts;
953 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
954}
955
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800956TEST(time, clock_gettime_unknown) {
957 errno = 0;
958 timespec ts;
959 ASSERT_EQ(-1, clock_gettime(-1, &ts));
960 ASSERT_EQ(EINVAL, errno);
961}
962
963TEST(time, clock_getres_CLOCK_REALTIME) {
964 timespec ts;
965 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
966 ASSERT_EQ(1, ts.tv_nsec);
967 ASSERT_EQ(0, ts.tv_sec);
968}
969
970TEST(time, clock_getres_CLOCK_MONOTONIC) {
971 timespec ts;
972 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
973 ASSERT_EQ(1, ts.tv_nsec);
974 ASSERT_EQ(0, ts.tv_sec);
975}
976
977TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
978 timespec ts;
979 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
980}
981
982TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
983 timespec ts;
984 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
985}
986
987TEST(time, clock_getres_CLOCK_BOOTTIME) {
988 timespec ts;
989 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
990 ASSERT_EQ(1, ts.tv_nsec);
991 ASSERT_EQ(0, ts.tv_sec);
992}
993
994TEST(time, clock_getres_unknown) {
995 errno = 0;
996 timespec ts = { -1, -1 };
997 ASSERT_EQ(-1, clock_getres(-1, &ts));
998 ASSERT_EQ(EINVAL, errno);
999 ASSERT_EQ(-1, ts.tv_nsec);
1000 ASSERT_EQ(-1, ts.tv_sec);
1001}
1002
zijunzhaoe6202662023-01-03 23:32:18 +00001003TEST(time, clock_getres_null_resolution) {
1004 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1005}
1006
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001007TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001008 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1009 static const clock_t N = 5;
1010 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001011 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001012 for (size_t i = 0; i < N; ++i) {
1013 sleep(1);
1014 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001015 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001016 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001017}
1018
Elliott Hughesb4413592017-11-29 18:17:06 -08001019static pid_t GetInvalidPid() {
1020 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001021 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001022 fscanf(fp.get(), "%ld", &pid_max);
1023 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001024}
1025
Elliott Hughesb4413592017-11-29 18:17:06 -08001026TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001027 clockid_t clockid;
1028 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001029 timespec ts;
1030 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001031}
Yabin Cuid5c65272014-11-26 14:04:26 -08001032
Elliott Hughesb4413592017-11-29 18:17:06 -08001033TEST(time, clock_getcpuclockid_parent) {
1034 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001035 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001036 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001037 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001038}
Yabin Cuid5c65272014-11-26 14:04:26 -08001039
Elliott Hughesb4413592017-11-29 18:17:06 -08001040TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001041 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1042 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001043 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001044 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001045 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1046 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1047 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1048 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1049 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1050 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -08001051 ASSERT_EQ(0, errno);
1052}
1053
Haruki Hasegawa18160252014-10-13 00:50:47 +09001054TEST(time, clock_settime) {
1055 errno = 0;
1056 timespec ts;
1057 ASSERT_EQ(-1, clock_settime(-1, &ts));
1058 ASSERT_EQ(EINVAL, errno);
1059}
1060
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001061TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001062 timespec in;
1063 timespec out;
1064 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001065}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001066
1067TEST(time, clock_nanosleep_thread_cputime_id) {
1068 timespec in;
1069 in.tv_sec = 1;
1070 in.tv_nsec = 0;
1071 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1072}
Elliott Hughes12443702016-10-19 16:02:31 -07001073
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001074TEST(time, clock_nanosleep) {
1075 auto t0 = std::chrono::steady_clock::now();
1076 const timespec ts = {.tv_nsec = 5000000};
1077 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1078 auto t1 = std::chrono::steady_clock::now();
1079 ASSERT_GE(t1-t0, 5000000ns);
1080}
1081
1082TEST(time, nanosleep) {
1083 auto t0 = std::chrono::steady_clock::now();
1084 const timespec ts = {.tv_nsec = 5000000};
1085 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1086 auto t1 = std::chrono::steady_clock::now();
1087 ASSERT_GE(t1-t0, 5000000ns);
1088}
1089
1090TEST(time, nanosleep_EINVAL) {
1091 timespec ts = {.tv_sec = -1};
1092 errno = 0;
1093 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
1094 ASSERT_EQ(EINVAL, errno);
1095}
1096
Elliott Hughes12443702016-10-19 16:02:31 -07001097TEST(time, bug_31938693) {
1098 // User-visible symptoms in N:
1099 // http://b/31938693
1100 // https://code.google.com/p/android/issues/detail?id=225132
1101
1102 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1103 // http://b/31848040
1104
1105 // This isn't a great test, because very few time zones were actually affected, and there's
1106 // no real logic to which ones were affected: it was just a coincidence of the data that came
1107 // after them in the tzdata file.
1108
1109 time_t t = 1475619727;
1110 struct tm tm;
1111
1112 setenv("TZ", "America/Los_Angeles", 1);
1113 tzset();
1114 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1115 EXPECT_EQ(15, tm.tm_hour);
1116
1117 setenv("TZ", "Europe/London", 1);
1118 tzset();
1119 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1120 EXPECT_EQ(23, tm.tm_hour);
1121
1122 setenv("TZ", "America/Atka", 1);
1123 tzset();
1124 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1125 EXPECT_EQ(13, tm.tm_hour);
1126
1127 setenv("TZ", "Pacific/Apia", 1);
1128 tzset();
1129 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1130 EXPECT_EQ(12, tm.tm_hour);
1131
1132 setenv("TZ", "Pacific/Honolulu", 1);
1133 tzset();
1134 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1135 EXPECT_EQ(12, tm.tm_hour);
1136
1137 setenv("TZ", "Asia/Magadan", 1);
1138 tzset();
1139 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1140 EXPECT_EQ(9, tm.tm_hour);
1141}
Elliott Hughesea877162017-01-11 14:34:16 -08001142
1143TEST(time, bug_31339449) {
1144 // POSIX says localtime acts as if it calls tzset.
1145 // tzset does two things:
1146 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1147 // 2. it sets the global `tzname`.
1148 // POSIX says localtime_r need not set `tzname` (2).
1149 // Q: should localtime_r set the time zone (1)?
1150 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1151
1152 // Pick a time, any time...
1153 time_t t = 1475619727;
1154
1155 // Call tzset with a specific timezone.
1156 setenv("TZ", "America/Atka", 1);
1157 tzset();
1158
1159 // If we change the timezone and call localtime, localtime should use the new timezone.
1160 setenv("TZ", "America/Los_Angeles", 1);
1161 struct tm* tm_p = localtime(&t);
1162 EXPECT_EQ(15, tm_p->tm_hour);
1163
1164 // Reset the timezone back.
1165 setenv("TZ", "America/Atka", 1);
1166 tzset();
1167
1168#if defined(__BIONIC__)
1169 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1170 setenv("TZ", "America/Los_Angeles", 1);
1171 struct tm tm = {};
1172 localtime_r(&t, &tm);
1173 EXPECT_EQ(15, tm.tm_hour);
1174#else
1175 // The BSDs agree with us, but glibc gets this wrong.
1176#endif
1177}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001178
1179TEST(time, asctime) {
1180 const struct tm tm = {};
1181 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1182}
1183
1184TEST(time, asctime_r) {
1185 const struct tm tm = {};
1186 char buf[256];
1187 ASSERT_EQ(buf, asctime_r(&tm, buf));
1188 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1189}
1190
1191TEST(time, ctime) {
1192 setenv("TZ", "UTC", 1);
1193 const time_t t = 0;
1194 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1195}
1196
1197TEST(time, ctime_r) {
1198 setenv("TZ", "UTC", 1);
1199 const time_t t = 0;
1200 char buf[256];
1201 ASSERT_EQ(buf, ctime_r(&t, buf));
1202 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1203}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001204
1205// https://issuetracker.google.com/37128336
1206TEST(time, strftime_strptime_s) {
1207 char buf[32];
1208 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1209
1210 setenv("TZ", "America/Los_Angeles", 1);
1211 strftime(buf, sizeof(buf), "<%s>", &tm0);
1212 EXPECT_STREQ("<378720000>", buf);
1213
1214 setenv("TZ", "UTC", 1);
1215 strftime(buf, sizeof(buf), "<%s>", &tm0);
1216 EXPECT_STREQ("<378691200>", buf);
1217
1218 struct tm tm;
1219
1220 setenv("TZ", "America/Los_Angeles", 1);
1221 tzset();
1222 memset(&tm, 0xff, sizeof(tm));
1223 char* p = strptime("378720000x", "%s", &tm);
1224 ASSERT_EQ('x', *p);
1225 EXPECT_EQ(0, tm.tm_sec);
1226 EXPECT_EQ(0, tm.tm_min);
1227 EXPECT_EQ(0, tm.tm_hour);
1228 EXPECT_EQ(1, tm.tm_mday);
1229 EXPECT_EQ(0, tm.tm_mon);
1230 EXPECT_EQ(82, tm.tm_year);
1231 EXPECT_EQ(5, tm.tm_wday);
1232 EXPECT_EQ(0, tm.tm_yday);
1233 EXPECT_EQ(0, tm.tm_isdst);
1234
1235 setenv("TZ", "UTC", 1);
1236 tzset();
1237 memset(&tm, 0xff, sizeof(tm));
1238 p = strptime("378691200x", "%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
1251TEST(time, strptime_s_nothing) {
1252 struct tm tm;
1253 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1254}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001255
1256TEST(time, timespec_get) {
1257#if __BIONIC__
1258 timespec ts = {};
1259 ASSERT_EQ(0, timespec_get(&ts, 123));
1260 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1261#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001262 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001263#endif
1264}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001265
1266TEST(time, difftime) {
1267 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001268 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001269}