blob: 483315d8241813757705c852319c4900a29ca0e1 [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) {
171 struct tm t;
172 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700173
174 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
175 t.tm_year = 2016 - 1900;
176
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700177 t.tm_mon = 2;
178 t.tm_mday = 10;
179
180 errno = 0;
181 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
182 ASSERT_EQ(0, errno);
183
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100184 // This will overflow for LP32.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700185 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700186
187 errno = 0;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100188#if !defined(__LP64__)
189 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
190 ASSERT_EQ(EOVERFLOW, errno);
191#else
192 ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t));
193 ASSERT_EQ(0, errno);
194#endif
195
196 // This will overflow for LP32 or LP64.
197 // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is.
198 t.tm_year = INT_MAX;
199 t.tm_mon = 11;
200 t.tm_mday = 45;
201
202 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700203 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
204 ASSERT_EQ(EOVERFLOW, errno);
205}
206
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000207TEST(time, mktime_invalid_tm_TZ_combination) {
208 setenv("TZ", "UTC", 1);
209
210 struct tm t;
211 memset(&t, 0, sizeof(tm));
212 t.tm_year = 2022 - 1900;
213 t.tm_mon = 11;
214 t.tm_mday = 31;
215 // UTC does not observe DST
216 t.tm_isdst = 1;
217
218 errno = 0;
219
220 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
221 // mktime sets errno to EOVERFLOW if result is unrepresentable.
222 EXPECT_EQ(EOVERFLOW, errno);
223}
224
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100225// Transitions in the tzdata file are generated up to the year 2100. Testing
226// that dates beyond that are handled properly too.
227TEST(time, mktime_after_2100) {
228 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
229
230#if !defined(__LP64__)
231 // 32-bit bionic has a signed 32-bit time_t.
232 ASSERT_EQ(-1, mktime(&tm));
233 ASSERT_EQ(EOVERFLOW, errno);
234#else
235 setenv("TZ", "Europe/London", 1);
236 tzset();
237 errno = 0;
238
239 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
240 ASSERT_EQ(0, errno);
241#endif
242}
243
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700244TEST(time, strftime) {
245 setenv("TZ", "UTC", 1);
246
247 struct tm t;
248 memset(&t, 0, sizeof(tm));
249 t.tm_year = 200;
250 t.tm_mon = 2;
251 t.tm_mday = 10;
252
253 char buf[64];
254
255 // Seconds since the epoch.
256#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
257 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
258 EXPECT_STREQ("4108320000", buf);
259#endif
260
261 // Date and time as text.
262 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
263 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
264}
265
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000266TEST(time, strftime_second_before_epoch) {
267 setenv("TZ", "UTC", 1);
268
269 struct tm t;
270 memset(&t, 0, sizeof(tm));
271 t.tm_year = 1969 - 1900;
272 t.tm_mon = 11;
273 t.tm_mday = 31;
274 t.tm_hour = 23;
275 t.tm_min = 59;
276 t.tm_sec = 59;
277
278 char buf[64];
279
280 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
281 EXPECT_STREQ("-1", buf);
282}
283
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100284TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800285 // Netflix on Nexus Player wouldn't start (http://b/25170306).
286 struct tm t;
287 memset(&t, 0, sizeof(tm));
288
289 char buf[64];
290
291 setenv("TZ", "America/Los_Angeles", 1);
292 tzset();
293
294 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
295 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
296 EXPECT_STREQ("<PST>", buf);
297
298#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
299 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
300 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
301 EXPECT_STREQ("<PDT>", buf);
302
303 t.tm_isdst = -123; // "and negative if the information is not available".
304 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
305 EXPECT_STREQ("<>", buf);
306#endif
307
308 setenv("TZ", "UTC", 1);
309 tzset();
310
311 t.tm_isdst = 0;
312 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
313 EXPECT_STREQ("<UTC>", buf);
314
315#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
316 t.tm_isdst = 1; // UTC has no DST.
317 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
318 EXPECT_STREQ("<>", buf);
319#endif
320}
321
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100322// According to C language specification the only tm struct field needed to
323// find out replacement for %z and %Z in strftime is tm_isdst. Which is
324// wrong, as time zones change their standard offset and even DST savings.
325// tzcode deviates from C language specification and requires tm struct either
326// to be output of localtime-like functions or to be modified by mktime call
327// before passing to strftime. See tz mailing discussion for more details
328// https://mm.icann.org/pipermail/tz/2022-July/031674.html
329// But we are testing case when tm.tm_zone is null, which means that tm struct
330// is not coming from localtime and is neither modified by mktime. That's why
331// we are comparing against +0000, even though America/Los_Angeles never
332// observes it.
333TEST(time, strftime_z_null_tm_zone) {
334 char str[64];
335 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
336
337 setenv("TZ", "America/Los_Angeles", 1);
338 tzset();
339
340 tm.tm_zone = NULL;
341
342 size_t result = strftime(str, sizeof(str), "%z", &tm);
343
344 EXPECT_EQ(5U, result);
345 EXPECT_STREQ("+0000", str);
346
347 tm.tm_isdst = 1;
348
349 result = strftime(str, sizeof(str), "%z", &tm);
350
351 EXPECT_EQ(5U, result);
352 EXPECT_STREQ("+0000", str);
353
354 setenv("TZ", "UTC", 1);
355 tzset();
356
357 tm.tm_isdst = 0;
358
359 result = strftime(str, sizeof(str), "%z", &tm);
360
361 EXPECT_EQ(5U, result);
362 EXPECT_STREQ("+0000", str);
363
364 tm.tm_isdst = 1;
365
366 result = strftime(str, sizeof(str), "%z", &tm);
367
368 EXPECT_EQ(5U, result);
369 EXPECT_STREQ("+0000", str);
370}
371
372TEST(time, strftime_z_Europe_Lisbon) {
373 char str[64];
374 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
375 // tm_isdst is not set as it will be overridden by mktime call anyway.
376 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
377
378 setenv("TZ", "Europe/Lisbon", 1);
379 tzset();
380
381 // tzcode's strftime implementation for %z relies on prior mktime call.
382 // At the moment of writing %z value is taken from tm_gmtoff. So without
383 // mktime call %z is replaced with +0000.
384 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
385 mktime(&tm);
386
387 size_t result = strftime(str, sizeof(str), "%z", &tm);
388
389 EXPECT_EQ(5U, result);
390 EXPECT_STREQ("+0100", str);
391
392 // Now standard offset is 0.
393 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
394
395 mktime(&tm);
396 result = strftime(str, sizeof(str), "%z", &tm);
397
398 EXPECT_EQ(5U, result);
399 EXPECT_STREQ("+0000", str);
400}
401
Elliott Hughes0a610d02016-07-29 14:04:17 -0700402TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700403 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700404 locale_t old_locale = uselocale(cloc);
405
406 setenv("TZ", "UTC", 1);
407
408 struct tm t;
409 memset(&t, 0, sizeof(tm));
410 t.tm_year = 200;
411 t.tm_mon = 2;
412 t.tm_mday = 10;
413
414 // Date and time as text.
415 char buf[64];
416 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
417 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
418
419 uselocale(old_locale);
420 freelocale(cloc);
421}
422
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700423TEST(time, strptime) {
424 setenv("TZ", "UTC", 1);
425
426 struct tm t;
427 char buf[64];
428
429 memset(&t, 0, sizeof(t));
430 strptime("11:14", "%R", &t);
431 strftime(buf, sizeof(buf), "%H:%M", &t);
432 EXPECT_STREQ("11:14", buf);
433
434 memset(&t, 0, sizeof(t));
435 strptime("09:41:53", "%T", &t);
436 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
437 EXPECT_STREQ("09:41:53", buf);
438}
439
Elliott Hughes3376c232018-02-13 23:14:12 -0800440TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700441#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800442 setenv("TZ", "UTC", 1);
443
444 struct tm t;
445 char buf[64];
446
447 memset(&t, 0, sizeof(t));
448 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
449 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
450 EXPECT_STREQ("11:14", buf);
451
452 memset(&t, 0, sizeof(t));
453 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
454 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
455 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700456#else
457 GTEST_SKIP() << "musl doesn't support strptime_l";
458#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800459}
460
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700461TEST(time, strptime_F) {
462 setenv("TZ", "UTC", 1);
463
464 struct tm tm = {};
465 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
466 EXPECT_EQ(119, tm.tm_year);
467 EXPECT_EQ(2, tm.tm_mon);
468 EXPECT_EQ(26, tm.tm_mday);
469}
470
471TEST(time, strptime_P_p) {
472 setenv("TZ", "UTC", 1);
473
Elliott Hughes11678822019-03-27 08:56:49 -0700474 // For parsing, %P and %p are the same: case doesn't matter.
475
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700476 struct tm tm = {.tm_hour = 12};
477 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
478 EXPECT_EQ(0, tm.tm_hour);
479
480 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700481 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
482 EXPECT_EQ(0, tm.tm_hour);
483
484 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700485 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
486 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700487
488 tm = {.tm_hour = 12};
489 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
490 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700491}
492
493TEST(time, strptime_u) {
494 setenv("TZ", "UTC", 1);
495
496 struct tm tm = {};
497 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
498 EXPECT_EQ(2, tm.tm_wday);
499}
500
501TEST(time, strptime_v) {
502 setenv("TZ", "UTC", 1);
503
504 struct tm tm = {};
505 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
506 EXPECT_EQ(80, tm.tm_year);
507 EXPECT_EQ(2, tm.tm_mon);
508 EXPECT_EQ(26, tm.tm_mday);
509}
510
511TEST(time, strptime_V_G_g) {
512 setenv("TZ", "UTC", 1);
513
514 // %V (ISO-8601 week number), %G (year of week number, without century), and
515 // %g (year of week number) have no effect when parsed, and are supported
516 // solely so that it's possible for strptime(3) to parse everything that
517 // strftime(3) can output.
518 struct tm tm = {};
519 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
520 struct tm zero = {};
521 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
522}
523
Elliott Hughesd065c042020-09-01 19:02:44 -0700524TEST(time, strptime_Z) {
525#if defined(__BIONIC__)
526 // glibc doesn't handle %Z at all.
527 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
528 // are in the global `tzname` (which correspond to the current $TZ).
529 struct tm tm;
530 setenv("TZ", "Europe/Berlin", 1);
531
532 // "GMT" always works.
533 tm = {};
534 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
535 EXPECT_STREQ("GMT", tm.tm_zone);
536 EXPECT_EQ(0, tm.tm_isdst);
537 EXPECT_EQ(0, tm.tm_gmtoff);
538
539 // As does "UTC".
540 tm = {};
541 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
542 EXPECT_STREQ("UTC", tm.tm_zone);
543 EXPECT_EQ(0, tm.tm_isdst);
544 EXPECT_EQ(0, tm.tm_gmtoff);
545
546 // Europe/Berlin is known as "CET" when there's no DST.
547 tm = {};
548 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
549 EXPECT_STREQ("CET", tm.tm_zone);
550 EXPECT_EQ(0, tm.tm_isdst);
551 EXPECT_EQ(3600, tm.tm_gmtoff);
552
553 // Europe/Berlin is known as "CEST" when there's no DST.
554 tm = {};
555 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
556 EXPECT_STREQ("CEST", tm.tm_zone);
557 EXPECT_EQ(1, tm.tm_isdst);
558 EXPECT_EQ(3600, tm.tm_gmtoff);
559
560 // And as long as we're in Europe/Berlin, those are the only time zone
561 // abbreviations that are recognized.
562 tm = {};
563 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
564#endif
565}
566
567TEST(time, strptime_z) {
568 struct tm tm;
569 setenv("TZ", "Europe/Berlin", 1);
570
571 // "UT" is what RFC822 called UTC.
572 tm = {};
573 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
574 EXPECT_STREQ("UTC", tm.tm_zone);
575 EXPECT_EQ(0, tm.tm_isdst);
576 EXPECT_EQ(0, tm.tm_gmtoff);
577 // "GMT" is RFC822's other name for UTC.
578 tm = {};
579 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
580 EXPECT_STREQ("UTC", tm.tm_zone);
581 EXPECT_EQ(0, tm.tm_isdst);
582 EXPECT_EQ(0, tm.tm_gmtoff);
583
584 // "Z" ("Zulu") is a synonym for UTC.
585 tm = {};
586 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
587 EXPECT_STREQ("UTC", tm.tm_zone);
588 EXPECT_EQ(0, tm.tm_isdst);
589 EXPECT_EQ(0, tm.tm_gmtoff);
590
591 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
592 tm = {};
593 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
594 EXPECT_STREQ("PST", tm.tm_zone);
595 EXPECT_EQ(0, tm.tm_isdst);
596 EXPECT_EQ(-28800, tm.tm_gmtoff);
597 tm = {};
598 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
599 EXPECT_STREQ("PDT", tm.tm_zone);
600 EXPECT_EQ(1, tm.tm_isdst);
601 EXPECT_EQ(-25200, tm.tm_gmtoff);
602
603 // +-hh
604 tm = {};
605 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
606 EXPECT_EQ(3600, tm.tm_gmtoff);
607 EXPECT_TRUE(tm.tm_zone == nullptr);
608 EXPECT_EQ(0, tm.tm_isdst);
609 // +-hhmm
610 tm = {};
611 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
612 EXPECT_EQ(5400, tm.tm_gmtoff);
613 EXPECT_TRUE(tm.tm_zone == nullptr);
614 EXPECT_EQ(0, tm.tm_isdst);
615 // +-hh:mm
616 tm = {};
617 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
618 EXPECT_EQ(5400, tm.tm_gmtoff);
619 EXPECT_TRUE(tm.tm_zone == nullptr);
620 EXPECT_EQ(0, tm.tm_isdst);
621}
622
Elliott Hughes4b558f52014-03-04 15:58:02 -0800623void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
624 itimerspec ts;
625 ts.it_value.tv_sec = value_s;
626 ts.it_value.tv_nsec = value_ns;
627 ts.it_interval.tv_sec = interval_s;
628 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700629 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800630}
631
Colin Cross7da20342021-07-28 11:18:11 -0700632static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800633}
634
635TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700636 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800637 memset(&se, 0, sizeof(se));
638 se.sigev_notify = SIGEV_THREAD;
639 se.sigev_notify_function = NoOpNotifyFunction;
640 timer_t timer_id;
641 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
642
Elliott Hughes33697a02016-01-26 13:04:57 -0800643 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800644 ASSERT_NE(-1, pid) << strerror(errno);
645
646 if (pid == 0) {
647 // Timers are not inherited by the child.
648 ASSERT_EQ(-1, timer_delete(timer_id));
649 ASSERT_EQ(EINVAL, errno);
650 _exit(0);
651 }
652
Elliott Hughes33697a02016-01-26 13:04:57 -0800653 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800654
655 ASSERT_EQ(0, timer_delete(timer_id));
656}
657
Yabin Cui95f1ee22015-01-13 19:53:15 -0800658static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800659static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
660 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
661 ASSERT_EQ(SIGUSR1, signal_number);
662}
663
664TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700665 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800666 memset(&se, 0, sizeof(se));
667 se.sigev_notify = SIGEV_SIGNAL;
668 se.sigev_signo = SIGUSR1;
669
670 timer_t timer_id;
671 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
672
Yabin Cui95f1ee22015-01-13 19:53:15 -0800673 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800674 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
675
676 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
677
678 itimerspec ts;
679 ts.it_value.tv_sec = 0;
680 ts.it_value.tv_nsec = 1;
681 ts.it_interval.tv_sec = 0;
682 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700683 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800684
685 usleep(500000);
686 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
687}
688
689struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800690 private:
691 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800692 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700693 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700694 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800695
Elliott Hughes4b558f52014-03-04 15:58:02 -0800696 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700697 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800698 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700699 timer_valid = true;
700 }
701
Yabin Cui95f1ee22015-01-13 19:53:15 -0800702 public:
Colin Cross7da20342021-07-28 11:18:11 -0700703 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800704 memset(&se, 0, sizeof(se));
705 se.sigev_notify = SIGEV_THREAD;
706 se.sigev_notify_function = fn;
707 se.sigev_value.sival_ptr = this;
708 Create();
709 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700710 void DeleteTimer() {
711 ASSERT_TRUE(timer_valid);
712 ASSERT_EQ(0, timer_delete(timer_id));
713 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800714 }
715
716 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700717 if (timer_valid) {
718 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800719 }
720 }
721
Yabin Cui95f1ee22015-01-13 19:53:15 -0800722 int Value() const {
723 return value;
724 }
725
Christopher Ferris62d84b12014-10-20 19:09:19 -0700726 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
727 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
728 }
729
730 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800731 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700732 time_t start = time(nullptr);
733 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700734 }
735 return current_value != value;
736 }
737
Colin Cross7da20342021-07-28 11:18:11 -0700738 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800739 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
740 ++cd->value;
741 }
742
Colin Cross7da20342021-07-28 11:18:11 -0700743 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800744 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
745 ++cd->value;
746
747 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700748 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800749 }
750};
751
752TEST(time, timer_settime_0) {
753 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800754 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800755
Yabin Cuibf572d92015-08-11 11:23:16 -0700756 counter.SetTime(0, 500000000, 1, 0);
757 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800758
759 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800760 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800761}
762
763TEST(time, timer_settime_repeats) {
764 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800765 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800766
Christopher Ferris62d84b12014-10-20 19:09:19 -0700767 counter.SetTime(0, 1, 0, 10);
768 ASSERT_TRUE(counter.ValueUpdated());
769 ASSERT_TRUE(counter.ValueUpdated());
770 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800771 counter.DeleteTimer();
772 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
773 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800774}
775
Yabin Cui95f1ee22015-01-13 19:53:15 -0800776static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800777static void timer_create_NULL_signal_handler(int signal_number) {
778 ++timer_create_NULL_signal_handler_invocation_count;
779 ASSERT_EQ(SIGALRM, signal_number);
780}
781
782TEST(time, timer_create_NULL) {
783 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
784 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700785 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800786
Yabin Cui95f1ee22015-01-13 19:53:15 -0800787 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800788 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
789
790 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
791
792 SetTime(timer_id, 0, 1, 0, 0);
793 usleep(500000);
794
795 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
796}
797
798TEST(time, timer_create_EINVAL) {
799 clockid_t invalid_clock = 16;
800
801 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
802 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700803 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800804 ASSERT_EQ(EINVAL, errno);
805
806 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700807 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800808 memset(&se, 0, sizeof(se));
809 se.sigev_notify = SIGEV_THREAD;
810 se.sigev_notify_function = NoOpNotifyFunction;
811 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
812 ASSERT_EQ(EINVAL, errno);
813}
814
Elliott Hughes4b558f52014-03-04 15:58:02 -0800815TEST(time, timer_create_multiple) {
816 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800817 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800818 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800819
Yabin Cui95f1ee22015-01-13 19:53:15 -0800820 ASSERT_EQ(0, counter1.Value());
821 ASSERT_EQ(0, counter2.Value());
822 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800823
Yabin Cui410c1ad2015-06-18 16:19:02 -0700824 counter2.SetTime(0, 500000000, 0, 0);
825 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800826
Yabin Cui95f1ee22015-01-13 19:53:15 -0800827 EXPECT_EQ(0, counter1.Value());
828 EXPECT_EQ(1, counter2.Value());
829 EXPECT_EQ(0, counter3.Value());
830}
831
832// Test to verify that disarming a repeatable timer disables the callbacks.
833TEST(time, timer_disarm_terminates) {
834 Counter counter(Counter::CountNotifyFunction);
835 ASSERT_EQ(0, counter.Value());
836
837 counter.SetTime(0, 1, 0, 1);
838 ASSERT_TRUE(counter.ValueUpdated());
839 ASSERT_TRUE(counter.ValueUpdated());
840 ASSERT_TRUE(counter.ValueUpdated());
841
842 counter.SetTime(0, 0, 0, 0);
843 // Add a sleep as the kernel may have pending events when the timer is disarmed.
844 usleep(500000);
845 int value = counter.Value();
846 usleep(500000);
847
848 // Verify the counter has not been incremented.
849 ASSERT_EQ(value, counter.Value());
850}
851
852// Test to verify that deleting a repeatable timer disables the callbacks.
853TEST(time, timer_delete_terminates) {
854 Counter counter(Counter::CountNotifyFunction);
855 ASSERT_EQ(0, counter.Value());
856
857 counter.SetTime(0, 1, 0, 1);
858 ASSERT_TRUE(counter.ValueUpdated());
859 ASSERT_TRUE(counter.ValueUpdated());
860 ASSERT_TRUE(counter.ValueUpdated());
861
862 counter.DeleteTimer();
863 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
864 usleep(500000);
865 int value = counter.Value();
866 usleep(500000);
867
868 // Verify the counter has not been incremented.
869 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800870}
Christopher Ferris753ad772014-03-20 20:47:45 -0700871
872struct TimerDeleteData {
873 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800874 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700875 volatile bool complete;
876};
877
Colin Cross7da20342021-07-28 11:18:11 -0700878static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700879 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
880
Elliott Hughes11859d42017-02-13 17:59:29 -0800881 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700882 timer_delete(tdd->timer_id);
883 tdd->complete = true;
884}
885
886TEST(time, timer_delete_from_timer_thread) {
887 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700888 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700889
890 memset(&se, 0, sizeof(se));
891 se.sigev_notify = SIGEV_THREAD;
892 se.sigev_notify_function = TimerDeleteCallback;
893 se.sigev_value.sival_ptr = &tdd;
894
895 tdd.complete = false;
896 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
897
898 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800899 ts.it_value.tv_sec = 1;
900 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700901 ts.it_interval.tv_sec = 0;
902 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700903 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700904
Yi Kong32bc0fc2018-08-02 17:31:13 -0700905 time_t cur_time = time(nullptr);
906 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700907 ASSERT_TRUE(tdd.complete);
908
909#if defined(__BIONIC__)
910 // Since bionic timers are implemented by creating a thread to handle the
911 // callback, verify that the thread actually completes.
912 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800913 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
914 ASSERT_EQ(-1, kill(tdd.tid, 0));
915 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700916#endif
917}
Elliott Hughes625993d2014-07-15 16:53:13 -0700918
Colin Cross35d469b2021-08-16 15:26:28 -0700919// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
920#if !defined(__NR_clock_gettime)
921#define __NR_clock_gettime __NR_clock_gettime32
922#endif
923
Elliott Hughes625993d2014-07-15 16:53:13 -0700924TEST(time, clock_gettime) {
925 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100926 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700927 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700928 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100929 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
930 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
931 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700932
Giuliano Procida096f5952021-04-08 10:51:58 +0100933 // Check we have a nice monotonic timestamp sandwich.
934 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
935 if (ts0.tv_sec == ts1.tv_sec) {
936 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700937 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100938 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
939 if (ts1.tv_sec == ts2.tv_sec) {
940 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
941 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700942}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700943
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700944TEST(time, clock_gettime_CLOCK_REALTIME) {
945 timespec ts;
946 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
947}
948
949TEST(time, clock_gettime_CLOCK_MONOTONIC) {
950 timespec ts;
951 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
952}
953
954TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
955 timespec ts;
956 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
957}
958
959TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
960 timespec ts;
961 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
962}
963
964TEST(time, clock_gettime_CLOCK_BOOTTIME) {
965 timespec ts;
966 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
967}
968
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800969TEST(time, clock_gettime_unknown) {
970 errno = 0;
971 timespec ts;
972 ASSERT_EQ(-1, clock_gettime(-1, &ts));
973 ASSERT_EQ(EINVAL, errno);
974}
975
976TEST(time, clock_getres_CLOCK_REALTIME) {
977 timespec ts;
978 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
979 ASSERT_EQ(1, ts.tv_nsec);
980 ASSERT_EQ(0, ts.tv_sec);
981}
982
983TEST(time, clock_getres_CLOCK_MONOTONIC) {
984 timespec ts;
985 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
986 ASSERT_EQ(1, ts.tv_nsec);
987 ASSERT_EQ(0, ts.tv_sec);
988}
989
990TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
991 timespec ts;
992 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
993}
994
995TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
996 timespec ts;
997 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
998}
999
1000TEST(time, clock_getres_CLOCK_BOOTTIME) {
1001 timespec ts;
1002 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
1003 ASSERT_EQ(1, ts.tv_nsec);
1004 ASSERT_EQ(0, ts.tv_sec);
1005}
1006
1007TEST(time, clock_getres_unknown) {
1008 errno = 0;
1009 timespec ts = { -1, -1 };
1010 ASSERT_EQ(-1, clock_getres(-1, &ts));
1011 ASSERT_EQ(EINVAL, errno);
1012 ASSERT_EQ(-1, ts.tv_nsec);
1013 ASSERT_EQ(-1, ts.tv_sec);
1014}
1015
zijunzhaoe6202662023-01-03 23:32:18 +00001016TEST(time, clock_getres_null_resolution) {
1017 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1018}
1019
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001020TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001021 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1022 static const clock_t N = 5;
1023 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001024 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001025 for (size_t i = 0; i < N; ++i) {
1026 sleep(1);
1027 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001028 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001029 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001030}
1031
Elliott Hughesb4413592017-11-29 18:17:06 -08001032static pid_t GetInvalidPid() {
1033 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001034 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001035 fscanf(fp.get(), "%ld", &pid_max);
1036 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001037}
1038
Elliott Hughesb4413592017-11-29 18:17:06 -08001039TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001040 clockid_t clockid;
1041 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001042 timespec ts;
1043 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001044}
Yabin Cuid5c65272014-11-26 14:04:26 -08001045
Elliott Hughesb4413592017-11-29 18:17:06 -08001046TEST(time, clock_getcpuclockid_parent) {
1047 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001048 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001049 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001050 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001051}
Yabin Cuid5c65272014-11-26 14:04:26 -08001052
Elliott Hughesb4413592017-11-29 18:17:06 -08001053TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001054 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1055 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001056 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001057 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001058 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1059 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1060 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1061 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1062 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1063 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -08001064 ASSERT_EQ(0, errno);
1065}
1066
Haruki Hasegawa18160252014-10-13 00:50:47 +09001067TEST(time, clock_settime) {
1068 errno = 0;
1069 timespec ts;
1070 ASSERT_EQ(-1, clock_settime(-1, &ts));
1071 ASSERT_EQ(EINVAL, errno);
1072}
1073
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001074TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001075 timespec in;
1076 timespec out;
1077 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001078}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001079
1080TEST(time, clock_nanosleep_thread_cputime_id) {
1081 timespec in;
1082 in.tv_sec = 1;
1083 in.tv_nsec = 0;
1084 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1085}
Elliott Hughes12443702016-10-19 16:02:31 -07001086
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001087TEST(time, clock_nanosleep) {
1088 auto t0 = std::chrono::steady_clock::now();
1089 const timespec ts = {.tv_nsec = 5000000};
1090 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1091 auto t1 = std::chrono::steady_clock::now();
1092 ASSERT_GE(t1-t0, 5000000ns);
1093}
1094
1095TEST(time, nanosleep) {
1096 auto t0 = std::chrono::steady_clock::now();
1097 const timespec ts = {.tv_nsec = 5000000};
1098 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1099 auto t1 = std::chrono::steady_clock::now();
1100 ASSERT_GE(t1-t0, 5000000ns);
1101}
1102
1103TEST(time, nanosleep_EINVAL) {
1104 timespec ts = {.tv_sec = -1};
1105 errno = 0;
1106 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
1107 ASSERT_EQ(EINVAL, errno);
1108}
1109
Elliott Hughes12443702016-10-19 16:02:31 -07001110TEST(time, bug_31938693) {
1111 // User-visible symptoms in N:
1112 // http://b/31938693
1113 // https://code.google.com/p/android/issues/detail?id=225132
1114
1115 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1116 // http://b/31848040
1117
1118 // This isn't a great test, because very few time zones were actually affected, and there's
1119 // no real logic to which ones were affected: it was just a coincidence of the data that came
1120 // after them in the tzdata file.
1121
1122 time_t t = 1475619727;
1123 struct tm tm;
1124
1125 setenv("TZ", "America/Los_Angeles", 1);
1126 tzset();
1127 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1128 EXPECT_EQ(15, tm.tm_hour);
1129
1130 setenv("TZ", "Europe/London", 1);
1131 tzset();
1132 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1133 EXPECT_EQ(23, tm.tm_hour);
1134
1135 setenv("TZ", "America/Atka", 1);
1136 tzset();
1137 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1138 EXPECT_EQ(13, tm.tm_hour);
1139
1140 setenv("TZ", "Pacific/Apia", 1);
1141 tzset();
1142 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1143 EXPECT_EQ(12, tm.tm_hour);
1144
1145 setenv("TZ", "Pacific/Honolulu", 1);
1146 tzset();
1147 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1148 EXPECT_EQ(12, tm.tm_hour);
1149
1150 setenv("TZ", "Asia/Magadan", 1);
1151 tzset();
1152 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1153 EXPECT_EQ(9, tm.tm_hour);
1154}
Elliott Hughesea877162017-01-11 14:34:16 -08001155
1156TEST(time, bug_31339449) {
1157 // POSIX says localtime acts as if it calls tzset.
1158 // tzset does two things:
1159 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1160 // 2. it sets the global `tzname`.
1161 // POSIX says localtime_r need not set `tzname` (2).
1162 // Q: should localtime_r set the time zone (1)?
1163 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1164
1165 // Pick a time, any time...
1166 time_t t = 1475619727;
1167
1168 // Call tzset with a specific timezone.
1169 setenv("TZ", "America/Atka", 1);
1170 tzset();
1171
1172 // If we change the timezone and call localtime, localtime should use the new timezone.
1173 setenv("TZ", "America/Los_Angeles", 1);
1174 struct tm* tm_p = localtime(&t);
1175 EXPECT_EQ(15, tm_p->tm_hour);
1176
1177 // Reset the timezone back.
1178 setenv("TZ", "America/Atka", 1);
1179 tzset();
1180
1181#if defined(__BIONIC__)
1182 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1183 setenv("TZ", "America/Los_Angeles", 1);
1184 struct tm tm = {};
1185 localtime_r(&t, &tm);
1186 EXPECT_EQ(15, tm.tm_hour);
1187#else
1188 // The BSDs agree with us, but glibc gets this wrong.
1189#endif
1190}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001191
1192TEST(time, asctime) {
1193 const struct tm tm = {};
1194 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1195}
1196
1197TEST(time, asctime_r) {
1198 const struct tm tm = {};
1199 char buf[256];
1200 ASSERT_EQ(buf, asctime_r(&tm, buf));
1201 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1202}
1203
1204TEST(time, ctime) {
1205 setenv("TZ", "UTC", 1);
1206 const time_t t = 0;
1207 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1208}
1209
1210TEST(time, ctime_r) {
1211 setenv("TZ", "UTC", 1);
1212 const time_t t = 0;
1213 char buf[256];
1214 ASSERT_EQ(buf, ctime_r(&t, buf));
1215 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1216}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001217
1218// https://issuetracker.google.com/37128336
1219TEST(time, strftime_strptime_s) {
1220 char buf[32];
1221 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1222
1223 setenv("TZ", "America/Los_Angeles", 1);
1224 strftime(buf, sizeof(buf), "<%s>", &tm0);
1225 EXPECT_STREQ("<378720000>", buf);
1226
1227 setenv("TZ", "UTC", 1);
1228 strftime(buf, sizeof(buf), "<%s>", &tm0);
1229 EXPECT_STREQ("<378691200>", buf);
1230
1231 struct tm tm;
1232
1233 setenv("TZ", "America/Los_Angeles", 1);
1234 tzset();
1235 memset(&tm, 0xff, sizeof(tm));
1236 char* p = strptime("378720000x", "%s", &tm);
1237 ASSERT_EQ('x', *p);
1238 EXPECT_EQ(0, tm.tm_sec);
1239 EXPECT_EQ(0, tm.tm_min);
1240 EXPECT_EQ(0, tm.tm_hour);
1241 EXPECT_EQ(1, tm.tm_mday);
1242 EXPECT_EQ(0, tm.tm_mon);
1243 EXPECT_EQ(82, tm.tm_year);
1244 EXPECT_EQ(5, tm.tm_wday);
1245 EXPECT_EQ(0, tm.tm_yday);
1246 EXPECT_EQ(0, tm.tm_isdst);
1247
1248 setenv("TZ", "UTC", 1);
1249 tzset();
1250 memset(&tm, 0xff, sizeof(tm));
1251 p = strptime("378691200x", "%s", &tm);
1252 ASSERT_EQ('x', *p);
1253 EXPECT_EQ(0, tm.tm_sec);
1254 EXPECT_EQ(0, tm.tm_min);
1255 EXPECT_EQ(0, tm.tm_hour);
1256 EXPECT_EQ(1, tm.tm_mday);
1257 EXPECT_EQ(0, tm.tm_mon);
1258 EXPECT_EQ(82, tm.tm_year);
1259 EXPECT_EQ(5, tm.tm_wday);
1260 EXPECT_EQ(0, tm.tm_yday);
1261 EXPECT_EQ(0, tm.tm_isdst);
1262}
1263
1264TEST(time, strptime_s_nothing) {
1265 struct tm tm;
1266 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1267}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001268
1269TEST(time, timespec_get) {
1270#if __BIONIC__
1271 timespec ts = {};
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001272 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001273 ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC));
1274 ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE));
1275 ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE));
1276#else
1277 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1278#endif
1279}
1280
1281TEST(time, timespec_get_invalid) {
1282#if __BIONIC__
1283 timespec ts = {};
1284 ASSERT_EQ(0, timespec_get(&ts, -1));
1285#else
1286 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1287#endif
1288}
1289
1290TEST(time, timespec_getres) {
1291#if __BIONIC__
1292 timespec ts = {};
1293 ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC));
1294 ASSERT_EQ(1, ts.tv_nsec);
1295 ASSERT_EQ(0, ts.tv_sec);
1296#else
1297 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1298#endif
1299}
1300
1301TEST(time, timespec_getres_invalid) {
1302#if __BIONIC__
1303 timespec ts = {};
1304 ASSERT_EQ(0, timespec_getres(&ts, 123));
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001305#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001306 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001307#endif
1308}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001309
1310TEST(time, difftime) {
1311 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001312 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001313}