blob: 898496d2ab6caee1be8ee018f8b38145deca3106 [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
Elliott Hughes47126ed2016-09-06 13:25:53 -0700184 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700185 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700186
187 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700188 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
189 ASSERT_EQ(EOVERFLOW, errno);
190}
191
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000192TEST(time, mktime_invalid_tm_TZ_combination) {
193 setenv("TZ", "UTC", 1);
194
195 struct tm t;
196 memset(&t, 0, sizeof(tm));
197 t.tm_year = 2022 - 1900;
198 t.tm_mon = 11;
199 t.tm_mday = 31;
200 // UTC does not observe DST
201 t.tm_isdst = 1;
202
203 errno = 0;
204
205 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
206 // mktime sets errno to EOVERFLOW if result is unrepresentable.
207 EXPECT_EQ(EOVERFLOW, errno);
208}
209
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100210// Transitions in the tzdata file are generated up to the year 2100. Testing
211// that dates beyond that are handled properly too.
212TEST(time, mktime_after_2100) {
213 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
214
215#if !defined(__LP64__)
216 // 32-bit bionic has a signed 32-bit time_t.
217 ASSERT_EQ(-1, mktime(&tm));
218 ASSERT_EQ(EOVERFLOW, errno);
219#else
220 setenv("TZ", "Europe/London", 1);
221 tzset();
222 errno = 0;
223
224 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
225 ASSERT_EQ(0, errno);
226#endif
227}
228
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700229TEST(time, strftime) {
230 setenv("TZ", "UTC", 1);
231
232 struct tm t;
233 memset(&t, 0, sizeof(tm));
234 t.tm_year = 200;
235 t.tm_mon = 2;
236 t.tm_mday = 10;
237
238 char buf[64];
239
240 // Seconds since the epoch.
241#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
242 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
243 EXPECT_STREQ("4108320000", buf);
244#endif
245
246 // Date and time as text.
247 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
248 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
249}
250
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000251TEST(time, strftime_second_before_epoch) {
252 setenv("TZ", "UTC", 1);
253
254 struct tm t;
255 memset(&t, 0, sizeof(tm));
256 t.tm_year = 1969 - 1900;
257 t.tm_mon = 11;
258 t.tm_mday = 31;
259 t.tm_hour = 23;
260 t.tm_min = 59;
261 t.tm_sec = 59;
262
263 char buf[64];
264
265 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
266 EXPECT_STREQ("-1", buf);
267}
268
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800269TEST(time, strftime_null_tm_zone) {
270 // Netflix on Nexus Player wouldn't start (http://b/25170306).
271 struct tm t;
272 memset(&t, 0, sizeof(tm));
273
274 char buf[64];
275
276 setenv("TZ", "America/Los_Angeles", 1);
277 tzset();
278
279 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
280 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
281 EXPECT_STREQ("<PST>", buf);
282
283#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
284 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
285 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
286 EXPECT_STREQ("<PDT>", buf);
287
288 t.tm_isdst = -123; // "and negative if the information is not available".
289 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
290 EXPECT_STREQ("<>", buf);
291#endif
292
293 setenv("TZ", "UTC", 1);
294 tzset();
295
296 t.tm_isdst = 0;
297 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
298 EXPECT_STREQ("<UTC>", buf);
299
300#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
301 t.tm_isdst = 1; // UTC has no DST.
302 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
303 EXPECT_STREQ("<>", buf);
304#endif
305}
306
Elliott Hughes0a610d02016-07-29 14:04:17 -0700307TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700309 locale_t old_locale = uselocale(cloc);
310
311 setenv("TZ", "UTC", 1);
312
313 struct tm t;
314 memset(&t, 0, sizeof(tm));
315 t.tm_year = 200;
316 t.tm_mon = 2;
317 t.tm_mday = 10;
318
319 // Date and time as text.
320 char buf[64];
321 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
322 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
323
324 uselocale(old_locale);
325 freelocale(cloc);
326}
327
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700328TEST(time, strptime) {
329 setenv("TZ", "UTC", 1);
330
331 struct tm t;
332 char buf[64];
333
334 memset(&t, 0, sizeof(t));
335 strptime("11:14", "%R", &t);
336 strftime(buf, sizeof(buf), "%H:%M", &t);
337 EXPECT_STREQ("11:14", buf);
338
339 memset(&t, 0, sizeof(t));
340 strptime("09:41:53", "%T", &t);
341 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
342 EXPECT_STREQ("09:41:53", buf);
343}
344
Elliott Hughes3376c232018-02-13 23:14:12 -0800345TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700346#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800347 setenv("TZ", "UTC", 1);
348
349 struct tm t;
350 char buf[64];
351
352 memset(&t, 0, sizeof(t));
353 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
354 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
355 EXPECT_STREQ("11:14", buf);
356
357 memset(&t, 0, sizeof(t));
358 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
359 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
360 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700361#else
362 GTEST_SKIP() << "musl doesn't support strptime_l";
363#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800364}
365
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700366TEST(time, strptime_F) {
367 setenv("TZ", "UTC", 1);
368
369 struct tm tm = {};
370 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
371 EXPECT_EQ(119, tm.tm_year);
372 EXPECT_EQ(2, tm.tm_mon);
373 EXPECT_EQ(26, tm.tm_mday);
374}
375
376TEST(time, strptime_P_p) {
377 setenv("TZ", "UTC", 1);
378
Elliott Hughes11678822019-03-27 08:56:49 -0700379 // For parsing, %P and %p are the same: case doesn't matter.
380
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700381 struct tm tm = {.tm_hour = 12};
382 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
383 EXPECT_EQ(0, tm.tm_hour);
384
385 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700386 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
387 EXPECT_EQ(0, tm.tm_hour);
388
389 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700390 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
391 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700392
393 tm = {.tm_hour = 12};
394 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
395 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700396}
397
398TEST(time, strptime_u) {
399 setenv("TZ", "UTC", 1);
400
401 struct tm tm = {};
402 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
403 EXPECT_EQ(2, tm.tm_wday);
404}
405
406TEST(time, strptime_v) {
407 setenv("TZ", "UTC", 1);
408
409 struct tm tm = {};
410 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
411 EXPECT_EQ(80, tm.tm_year);
412 EXPECT_EQ(2, tm.tm_mon);
413 EXPECT_EQ(26, tm.tm_mday);
414}
415
416TEST(time, strptime_V_G_g) {
417 setenv("TZ", "UTC", 1);
418
419 // %V (ISO-8601 week number), %G (year of week number, without century), and
420 // %g (year of week number) have no effect when parsed, and are supported
421 // solely so that it's possible for strptime(3) to parse everything that
422 // strftime(3) can output.
423 struct tm tm = {};
424 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
425 struct tm zero = {};
426 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
427}
428
Elliott Hughesd065c042020-09-01 19:02:44 -0700429TEST(time, strptime_Z) {
430#if defined(__BIONIC__)
431 // glibc doesn't handle %Z at all.
432 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
433 // are in the global `tzname` (which correspond to the current $TZ).
434 struct tm tm;
435 setenv("TZ", "Europe/Berlin", 1);
436
437 // "GMT" always works.
438 tm = {};
439 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
440 EXPECT_STREQ("GMT", tm.tm_zone);
441 EXPECT_EQ(0, tm.tm_isdst);
442 EXPECT_EQ(0, tm.tm_gmtoff);
443
444 // As does "UTC".
445 tm = {};
446 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
447 EXPECT_STREQ("UTC", tm.tm_zone);
448 EXPECT_EQ(0, tm.tm_isdst);
449 EXPECT_EQ(0, tm.tm_gmtoff);
450
451 // Europe/Berlin is known as "CET" when there's no DST.
452 tm = {};
453 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
454 EXPECT_STREQ("CET", tm.tm_zone);
455 EXPECT_EQ(0, tm.tm_isdst);
456 EXPECT_EQ(3600, tm.tm_gmtoff);
457
458 // Europe/Berlin is known as "CEST" when there's no DST.
459 tm = {};
460 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
461 EXPECT_STREQ("CEST", tm.tm_zone);
462 EXPECT_EQ(1, tm.tm_isdst);
463 EXPECT_EQ(3600, tm.tm_gmtoff);
464
465 // And as long as we're in Europe/Berlin, those are the only time zone
466 // abbreviations that are recognized.
467 tm = {};
468 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
469#endif
470}
471
472TEST(time, strptime_z) {
473 struct tm tm;
474 setenv("TZ", "Europe/Berlin", 1);
475
476 // "UT" is what RFC822 called UTC.
477 tm = {};
478 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
479 EXPECT_STREQ("UTC", tm.tm_zone);
480 EXPECT_EQ(0, tm.tm_isdst);
481 EXPECT_EQ(0, tm.tm_gmtoff);
482 // "GMT" is RFC822's other name for UTC.
483 tm = {};
484 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
485 EXPECT_STREQ("UTC", tm.tm_zone);
486 EXPECT_EQ(0, tm.tm_isdst);
487 EXPECT_EQ(0, tm.tm_gmtoff);
488
489 // "Z" ("Zulu") is a synonym for UTC.
490 tm = {};
491 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
492 EXPECT_STREQ("UTC", tm.tm_zone);
493 EXPECT_EQ(0, tm.tm_isdst);
494 EXPECT_EQ(0, tm.tm_gmtoff);
495
496 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
497 tm = {};
498 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
499 EXPECT_STREQ("PST", tm.tm_zone);
500 EXPECT_EQ(0, tm.tm_isdst);
501 EXPECT_EQ(-28800, tm.tm_gmtoff);
502 tm = {};
503 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
504 EXPECT_STREQ("PDT", tm.tm_zone);
505 EXPECT_EQ(1, tm.tm_isdst);
506 EXPECT_EQ(-25200, tm.tm_gmtoff);
507
508 // +-hh
509 tm = {};
510 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
511 EXPECT_EQ(3600, tm.tm_gmtoff);
512 EXPECT_TRUE(tm.tm_zone == nullptr);
513 EXPECT_EQ(0, tm.tm_isdst);
514 // +-hhmm
515 tm = {};
516 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
517 EXPECT_EQ(5400, tm.tm_gmtoff);
518 EXPECT_TRUE(tm.tm_zone == nullptr);
519 EXPECT_EQ(0, tm.tm_isdst);
520 // +-hh:mm
521 tm = {};
522 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
523 EXPECT_EQ(5400, tm.tm_gmtoff);
524 EXPECT_TRUE(tm.tm_zone == nullptr);
525 EXPECT_EQ(0, tm.tm_isdst);
526}
527
Elliott Hughes4b558f52014-03-04 15:58:02 -0800528void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
529 itimerspec ts;
530 ts.it_value.tv_sec = value_s;
531 ts.it_value.tv_nsec = value_ns;
532 ts.it_interval.tv_sec = interval_s;
533 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700534 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800535}
536
Colin Cross7da20342021-07-28 11:18:11 -0700537static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800538}
539
540TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700541 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800542 memset(&se, 0, sizeof(se));
543 se.sigev_notify = SIGEV_THREAD;
544 se.sigev_notify_function = NoOpNotifyFunction;
545 timer_t timer_id;
546 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
547
Elliott Hughes33697a02016-01-26 13:04:57 -0800548 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800549 ASSERT_NE(-1, pid) << strerror(errno);
550
551 if (pid == 0) {
552 // Timers are not inherited by the child.
553 ASSERT_EQ(-1, timer_delete(timer_id));
554 ASSERT_EQ(EINVAL, errno);
555 _exit(0);
556 }
557
Elliott Hughes33697a02016-01-26 13:04:57 -0800558 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800559
560 ASSERT_EQ(0, timer_delete(timer_id));
561}
562
Yabin Cui95f1ee22015-01-13 19:53:15 -0800563static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800564static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
565 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
566 ASSERT_EQ(SIGUSR1, signal_number);
567}
568
569TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700570 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800571 memset(&se, 0, sizeof(se));
572 se.sigev_notify = SIGEV_SIGNAL;
573 se.sigev_signo = SIGUSR1;
574
575 timer_t timer_id;
576 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
577
Yabin Cui95f1ee22015-01-13 19:53:15 -0800578 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800579 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
580
581 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
582
583 itimerspec ts;
584 ts.it_value.tv_sec = 0;
585 ts.it_value.tv_nsec = 1;
586 ts.it_interval.tv_sec = 0;
587 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700588 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800589
590 usleep(500000);
591 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
592}
593
594struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800595 private:
596 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800597 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700598 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700599 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800600
Elliott Hughes4b558f52014-03-04 15:58:02 -0800601 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700602 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800603 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700604 timer_valid = true;
605 }
606
Yabin Cui95f1ee22015-01-13 19:53:15 -0800607 public:
Colin Cross7da20342021-07-28 11:18:11 -0700608 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800609 memset(&se, 0, sizeof(se));
610 se.sigev_notify = SIGEV_THREAD;
611 se.sigev_notify_function = fn;
612 se.sigev_value.sival_ptr = this;
613 Create();
614 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700615 void DeleteTimer() {
616 ASSERT_TRUE(timer_valid);
617 ASSERT_EQ(0, timer_delete(timer_id));
618 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800619 }
620
621 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700622 if (timer_valid) {
623 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800624 }
625 }
626
Yabin Cui95f1ee22015-01-13 19:53:15 -0800627 int Value() const {
628 return value;
629 }
630
Christopher Ferris62d84b12014-10-20 19:09:19 -0700631 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
632 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
633 }
634
635 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800636 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700637 time_t start = time(nullptr);
638 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700639 }
640 return current_value != value;
641 }
642
Colin Cross7da20342021-07-28 11:18:11 -0700643 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800644 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
645 ++cd->value;
646 }
647
Colin Cross7da20342021-07-28 11:18:11 -0700648 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800649 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
650 ++cd->value;
651
652 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700653 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800654 }
655};
656
657TEST(time, timer_settime_0) {
658 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800659 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800660
Yabin Cuibf572d92015-08-11 11:23:16 -0700661 counter.SetTime(0, 500000000, 1, 0);
662 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800663
664 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800665 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800666}
667
668TEST(time, timer_settime_repeats) {
669 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800670 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800671
Christopher Ferris62d84b12014-10-20 19:09:19 -0700672 counter.SetTime(0, 1, 0, 10);
673 ASSERT_TRUE(counter.ValueUpdated());
674 ASSERT_TRUE(counter.ValueUpdated());
675 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800676 counter.DeleteTimer();
677 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
678 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800679}
680
Yabin Cui95f1ee22015-01-13 19:53:15 -0800681static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800682static void timer_create_NULL_signal_handler(int signal_number) {
683 ++timer_create_NULL_signal_handler_invocation_count;
684 ASSERT_EQ(SIGALRM, signal_number);
685}
686
687TEST(time, timer_create_NULL) {
688 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
689 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700690 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800691
Yabin Cui95f1ee22015-01-13 19:53:15 -0800692 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800693 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
694
695 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
696
697 SetTime(timer_id, 0, 1, 0, 0);
698 usleep(500000);
699
700 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
701}
702
703TEST(time, timer_create_EINVAL) {
704 clockid_t invalid_clock = 16;
705
706 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
707 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700708 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800709 ASSERT_EQ(EINVAL, errno);
710
711 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700712 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800713 memset(&se, 0, sizeof(se));
714 se.sigev_notify = SIGEV_THREAD;
715 se.sigev_notify_function = NoOpNotifyFunction;
716 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
717 ASSERT_EQ(EINVAL, errno);
718}
719
Elliott Hughes4b558f52014-03-04 15:58:02 -0800720TEST(time, timer_create_multiple) {
721 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800722 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800723 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800724
Yabin Cui95f1ee22015-01-13 19:53:15 -0800725 ASSERT_EQ(0, counter1.Value());
726 ASSERT_EQ(0, counter2.Value());
727 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800728
Yabin Cui410c1ad2015-06-18 16:19:02 -0700729 counter2.SetTime(0, 500000000, 0, 0);
730 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800731
Yabin Cui95f1ee22015-01-13 19:53:15 -0800732 EXPECT_EQ(0, counter1.Value());
733 EXPECT_EQ(1, counter2.Value());
734 EXPECT_EQ(0, counter3.Value());
735}
736
737// Test to verify that disarming a repeatable timer disables the callbacks.
738TEST(time, timer_disarm_terminates) {
739 Counter counter(Counter::CountNotifyFunction);
740 ASSERT_EQ(0, counter.Value());
741
742 counter.SetTime(0, 1, 0, 1);
743 ASSERT_TRUE(counter.ValueUpdated());
744 ASSERT_TRUE(counter.ValueUpdated());
745 ASSERT_TRUE(counter.ValueUpdated());
746
747 counter.SetTime(0, 0, 0, 0);
748 // Add a sleep as the kernel may have pending events when the timer is disarmed.
749 usleep(500000);
750 int value = counter.Value();
751 usleep(500000);
752
753 // Verify the counter has not been incremented.
754 ASSERT_EQ(value, counter.Value());
755}
756
757// Test to verify that deleting a repeatable timer disables the callbacks.
758TEST(time, timer_delete_terminates) {
759 Counter counter(Counter::CountNotifyFunction);
760 ASSERT_EQ(0, counter.Value());
761
762 counter.SetTime(0, 1, 0, 1);
763 ASSERT_TRUE(counter.ValueUpdated());
764 ASSERT_TRUE(counter.ValueUpdated());
765 ASSERT_TRUE(counter.ValueUpdated());
766
767 counter.DeleteTimer();
768 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
769 usleep(500000);
770 int value = counter.Value();
771 usleep(500000);
772
773 // Verify the counter has not been incremented.
774 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800775}
Christopher Ferris753ad772014-03-20 20:47:45 -0700776
777struct TimerDeleteData {
778 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800779 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700780 volatile bool complete;
781};
782
Colin Cross7da20342021-07-28 11:18:11 -0700783static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700784 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
785
Elliott Hughes11859d42017-02-13 17:59:29 -0800786 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700787 timer_delete(tdd->timer_id);
788 tdd->complete = true;
789}
790
791TEST(time, timer_delete_from_timer_thread) {
792 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700793 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700794
795 memset(&se, 0, sizeof(se));
796 se.sigev_notify = SIGEV_THREAD;
797 se.sigev_notify_function = TimerDeleteCallback;
798 se.sigev_value.sival_ptr = &tdd;
799
800 tdd.complete = false;
801 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
802
803 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800804 ts.it_value.tv_sec = 1;
805 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700806 ts.it_interval.tv_sec = 0;
807 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700808 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700809
Yi Kong32bc0fc2018-08-02 17:31:13 -0700810 time_t cur_time = time(nullptr);
811 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700812 ASSERT_TRUE(tdd.complete);
813
814#if defined(__BIONIC__)
815 // Since bionic timers are implemented by creating a thread to handle the
816 // callback, verify that the thread actually completes.
817 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800818 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
819 ASSERT_EQ(-1, kill(tdd.tid, 0));
820 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700821#endif
822}
Elliott Hughes625993d2014-07-15 16:53:13 -0700823
Colin Cross35d469b2021-08-16 15:26:28 -0700824// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
825#if !defined(__NR_clock_gettime)
826#define __NR_clock_gettime __NR_clock_gettime32
827#endif
828
Elliott Hughes625993d2014-07-15 16:53:13 -0700829TEST(time, clock_gettime) {
830 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100831 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700832 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700833 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100834 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
835 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
836 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700837
Giuliano Procida096f5952021-04-08 10:51:58 +0100838 // Check we have a nice monotonic timestamp sandwich.
839 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
840 if (ts0.tv_sec == ts1.tv_sec) {
841 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700842 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100843 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
844 if (ts1.tv_sec == ts2.tv_sec) {
845 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
846 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700847}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700848
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700849TEST(time, clock_gettime_CLOCK_REALTIME) {
850 timespec ts;
851 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
852}
853
854TEST(time, clock_gettime_CLOCK_MONOTONIC) {
855 timespec ts;
856 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
857}
858
859TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
860 timespec ts;
861 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
862}
863
864TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
865 timespec ts;
866 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
867}
868
869TEST(time, clock_gettime_CLOCK_BOOTTIME) {
870 timespec ts;
871 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
872}
873
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800874TEST(time, clock_gettime_unknown) {
875 errno = 0;
876 timespec ts;
877 ASSERT_EQ(-1, clock_gettime(-1, &ts));
878 ASSERT_EQ(EINVAL, errno);
879}
880
881TEST(time, clock_getres_CLOCK_REALTIME) {
882 timespec ts;
883 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
884 ASSERT_EQ(1, ts.tv_nsec);
885 ASSERT_EQ(0, ts.tv_sec);
886}
887
888TEST(time, clock_getres_CLOCK_MONOTONIC) {
889 timespec ts;
890 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
891 ASSERT_EQ(1, ts.tv_nsec);
892 ASSERT_EQ(0, ts.tv_sec);
893}
894
895TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
896 timespec ts;
897 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
898}
899
900TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
901 timespec ts;
902 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
903}
904
905TEST(time, clock_getres_CLOCK_BOOTTIME) {
906 timespec ts;
907 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
908 ASSERT_EQ(1, ts.tv_nsec);
909 ASSERT_EQ(0, ts.tv_sec);
910}
911
912TEST(time, clock_getres_unknown) {
913 errno = 0;
914 timespec ts = { -1, -1 };
915 ASSERT_EQ(-1, clock_getres(-1, &ts));
916 ASSERT_EQ(EINVAL, errno);
917 ASSERT_EQ(-1, ts.tv_nsec);
918 ASSERT_EQ(-1, ts.tv_sec);
919}
920
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700921TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100922 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
923 static const clock_t N = 5;
924 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900925 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100926 for (size_t i = 0; i < N; ++i) {
927 sleep(1);
928 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900929 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100930 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900931}
932
Elliott Hughesb4413592017-11-29 18:17:06 -0800933static pid_t GetInvalidPid() {
934 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800935 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800936 fscanf(fp.get(), "%ld", &pid_max);
937 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800938}
939
Elliott Hughesb4413592017-11-29 18:17:06 -0800940TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800941 clockid_t clockid;
942 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800943 timespec ts;
944 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800945}
Yabin Cuid5c65272014-11-26 14:04:26 -0800946
Elliott Hughesb4413592017-11-29 18:17:06 -0800947TEST(time, clock_getcpuclockid_parent) {
948 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800949 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800950 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800951 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800952}
Yabin Cuid5c65272014-11-26 14:04:26 -0800953
Elliott Hughesb4413592017-11-29 18:17:06 -0800954TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800955 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
956 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800957 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800958 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800959 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
960 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
961 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
962 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
963 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
964 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800965 ASSERT_EQ(0, errno);
966}
967
Haruki Hasegawa18160252014-10-13 00:50:47 +0900968TEST(time, clock_settime) {
969 errno = 0;
970 timespec ts;
971 ASSERT_EQ(-1, clock_settime(-1, &ts));
972 ASSERT_EQ(EINVAL, errno);
973}
974
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700975TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900976 timespec in;
977 timespec out;
978 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700979}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700980
981TEST(time, clock_nanosleep_thread_cputime_id) {
982 timespec in;
983 in.tv_sec = 1;
984 in.tv_nsec = 0;
985 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
986}
Elliott Hughes12443702016-10-19 16:02:31 -0700987
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700988TEST(time, clock_nanosleep) {
989 auto t0 = std::chrono::steady_clock::now();
990 const timespec ts = {.tv_nsec = 5000000};
991 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
992 auto t1 = std::chrono::steady_clock::now();
993 ASSERT_GE(t1-t0, 5000000ns);
994}
995
996TEST(time, nanosleep) {
997 auto t0 = std::chrono::steady_clock::now();
998 const timespec ts = {.tv_nsec = 5000000};
999 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1000 auto t1 = std::chrono::steady_clock::now();
1001 ASSERT_GE(t1-t0, 5000000ns);
1002}
1003
1004TEST(time, nanosleep_EINVAL) {
1005 timespec ts = {.tv_sec = -1};
1006 errno = 0;
1007 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
1008 ASSERT_EQ(EINVAL, errno);
1009}
1010
Elliott Hughes12443702016-10-19 16:02:31 -07001011TEST(time, bug_31938693) {
1012 // User-visible symptoms in N:
1013 // http://b/31938693
1014 // https://code.google.com/p/android/issues/detail?id=225132
1015
1016 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1017 // http://b/31848040
1018
1019 // This isn't a great test, because very few time zones were actually affected, and there's
1020 // no real logic to which ones were affected: it was just a coincidence of the data that came
1021 // after them in the tzdata file.
1022
1023 time_t t = 1475619727;
1024 struct tm tm;
1025
1026 setenv("TZ", "America/Los_Angeles", 1);
1027 tzset();
1028 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1029 EXPECT_EQ(15, tm.tm_hour);
1030
1031 setenv("TZ", "Europe/London", 1);
1032 tzset();
1033 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1034 EXPECT_EQ(23, tm.tm_hour);
1035
1036 setenv("TZ", "America/Atka", 1);
1037 tzset();
1038 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1039 EXPECT_EQ(13, tm.tm_hour);
1040
1041 setenv("TZ", "Pacific/Apia", 1);
1042 tzset();
1043 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1044 EXPECT_EQ(12, tm.tm_hour);
1045
1046 setenv("TZ", "Pacific/Honolulu", 1);
1047 tzset();
1048 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1049 EXPECT_EQ(12, tm.tm_hour);
1050
1051 setenv("TZ", "Asia/Magadan", 1);
1052 tzset();
1053 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1054 EXPECT_EQ(9, tm.tm_hour);
1055}
Elliott Hughesea877162017-01-11 14:34:16 -08001056
1057TEST(time, bug_31339449) {
1058 // POSIX says localtime acts as if it calls tzset.
1059 // tzset does two things:
1060 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1061 // 2. it sets the global `tzname`.
1062 // POSIX says localtime_r need not set `tzname` (2).
1063 // Q: should localtime_r set the time zone (1)?
1064 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1065
1066 // Pick a time, any time...
1067 time_t t = 1475619727;
1068
1069 // Call tzset with a specific timezone.
1070 setenv("TZ", "America/Atka", 1);
1071 tzset();
1072
1073 // If we change the timezone and call localtime, localtime should use the new timezone.
1074 setenv("TZ", "America/Los_Angeles", 1);
1075 struct tm* tm_p = localtime(&t);
1076 EXPECT_EQ(15, tm_p->tm_hour);
1077
1078 // Reset the timezone back.
1079 setenv("TZ", "America/Atka", 1);
1080 tzset();
1081
1082#if defined(__BIONIC__)
1083 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1084 setenv("TZ", "America/Los_Angeles", 1);
1085 struct tm tm = {};
1086 localtime_r(&t, &tm);
1087 EXPECT_EQ(15, tm.tm_hour);
1088#else
1089 // The BSDs agree with us, but glibc gets this wrong.
1090#endif
1091}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001092
1093TEST(time, asctime) {
1094 const struct tm tm = {};
1095 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1096}
1097
1098TEST(time, asctime_r) {
1099 const struct tm tm = {};
1100 char buf[256];
1101 ASSERT_EQ(buf, asctime_r(&tm, buf));
1102 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1103}
1104
1105TEST(time, ctime) {
1106 setenv("TZ", "UTC", 1);
1107 const time_t t = 0;
1108 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1109}
1110
1111TEST(time, ctime_r) {
1112 setenv("TZ", "UTC", 1);
1113 const time_t t = 0;
1114 char buf[256];
1115 ASSERT_EQ(buf, ctime_r(&t, buf));
1116 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1117}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001118
1119// https://issuetracker.google.com/37128336
1120TEST(time, strftime_strptime_s) {
1121 char buf[32];
1122 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1123
1124 setenv("TZ", "America/Los_Angeles", 1);
1125 strftime(buf, sizeof(buf), "<%s>", &tm0);
1126 EXPECT_STREQ("<378720000>", buf);
1127
1128 setenv("TZ", "UTC", 1);
1129 strftime(buf, sizeof(buf), "<%s>", &tm0);
1130 EXPECT_STREQ("<378691200>", buf);
1131
1132 struct tm tm;
1133
1134 setenv("TZ", "America/Los_Angeles", 1);
1135 tzset();
1136 memset(&tm, 0xff, sizeof(tm));
1137 char* p = strptime("378720000x", "%s", &tm);
1138 ASSERT_EQ('x', *p);
1139 EXPECT_EQ(0, tm.tm_sec);
1140 EXPECT_EQ(0, tm.tm_min);
1141 EXPECT_EQ(0, tm.tm_hour);
1142 EXPECT_EQ(1, tm.tm_mday);
1143 EXPECT_EQ(0, tm.tm_mon);
1144 EXPECT_EQ(82, tm.tm_year);
1145 EXPECT_EQ(5, tm.tm_wday);
1146 EXPECT_EQ(0, tm.tm_yday);
1147 EXPECT_EQ(0, tm.tm_isdst);
1148
1149 setenv("TZ", "UTC", 1);
1150 tzset();
1151 memset(&tm, 0xff, sizeof(tm));
1152 p = strptime("378691200x", "%s", &tm);
1153 ASSERT_EQ('x', *p);
1154 EXPECT_EQ(0, tm.tm_sec);
1155 EXPECT_EQ(0, tm.tm_min);
1156 EXPECT_EQ(0, tm.tm_hour);
1157 EXPECT_EQ(1, tm.tm_mday);
1158 EXPECT_EQ(0, tm.tm_mon);
1159 EXPECT_EQ(82, tm.tm_year);
1160 EXPECT_EQ(5, tm.tm_wday);
1161 EXPECT_EQ(0, tm.tm_yday);
1162 EXPECT_EQ(0, tm.tm_isdst);
1163}
1164
1165TEST(time, strptime_s_nothing) {
1166 struct tm tm;
1167 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1168}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001169
1170TEST(time, timespec_get) {
1171#if __BIONIC__
1172 timespec ts = {};
1173 ASSERT_EQ(0, timespec_get(&ts, 123));
1174 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1175#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001176 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001177#endif
1178}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001179
1180TEST(time, difftime) {
1181 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001182 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001183}