blob: 73590fa97d1c910770e8a704fb2b64c428aac171 [file] [log] [blame]
Elliott Hughese0175ca2013-03-14 14:38:08 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes4b558f52014-03-04 15:58:02 -080017#include <time.h>
18
19#include <errno.h>
Elliott Hughese0175ca2013-03-14 14:38:08 -070020#include <gtest/gtest.h>
Elliott Hughes329103d2014-04-25 16:55:04 -070021#include <pthread.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080022#include <signal.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070023#include <sys/cdefs.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070024#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080025#include <sys/types.h>
26#include <sys/wait.h>
Yabin Cuid5c65272014-11-26 14:04:26 -080027#include <unistd.h>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070028
Yabin Cui95f1ee22015-01-13 19:53:15 -080029#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070030#include <chrono>
Elliott Hughes2bd43162023-06-15 13:17:08 -070031#include <thread>
Elliott Hughese0175ca2013-03-14 14:38:08 -070032
Elliott Hughes71ba5892018-02-07 12:44:45 -080033#include "SignalUtils.h"
Elliott Hughesc793bc02024-05-21 21:35:49 +000034#include "android-base/file.h"
35#include "android-base/strings.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080036#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070037
Elliott Hughesca3f8e42019-10-28 15:59:38 -070038using namespace std::chrono_literals;
39
Mark Salyzyn0b846e82017-12-20 08:56:18 -080040TEST(time, time) {
41 // Acquire time
42 time_t p1, t1 = time(&p1);
43 // valid?
44 ASSERT_NE(static_cast<time_t>(0), t1);
45 ASSERT_NE(static_cast<time_t>(-1), t1);
46 ASSERT_EQ(p1, t1);
47
48 // Acquire time one+ second later
49 usleep(1010000);
50 time_t p2, t2 = time(&p2);
51 // valid?
52 ASSERT_NE(static_cast<time_t>(0), t2);
53 ASSERT_NE(static_cast<time_t>(-1), t2);
54 ASSERT_EQ(p2, t2);
55
56 // Expect time progression
57 ASSERT_LT(p1, p2);
58 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
59
60 // Expect nullptr call to produce same results
61 ASSERT_LE(t2, time(nullptr));
62 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
63}
64
Elliott Hughesee178bf2013-07-12 11:25:20 -070065TEST(time, gmtime) {
66 time_t t = 0;
67 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070068 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070069 ASSERT_EQ(0, broken_down->tm_sec);
70 ASSERT_EQ(0, broken_down->tm_min);
71 ASSERT_EQ(0, broken_down->tm_hour);
72 ASSERT_EQ(1, broken_down->tm_mday);
73 ASSERT_EQ(0, broken_down->tm_mon);
74 ASSERT_EQ(1970, broken_down->tm_year + 1900);
75}
Elliott Hughes7843d442013-08-22 11:37:32 -070076
Elliott Hughes5a29d542017-12-07 16:05:57 -080077TEST(time, gmtime_r) {
78 struct tm tm = {};
79 time_t t = 0;
80 struct tm* broken_down = gmtime_r(&t, &tm);
81 ASSERT_EQ(broken_down, &tm);
82 ASSERT_EQ(0, broken_down->tm_sec);
83 ASSERT_EQ(0, broken_down->tm_min);
84 ASSERT_EQ(0, broken_down->tm_hour);
85 ASSERT_EQ(1, broken_down->tm_mday);
86 ASSERT_EQ(0, broken_down->tm_mon);
87 ASSERT_EQ(1970, broken_down->tm_year + 1900);
88}
89
Almaz Mingaleevda75bb62022-06-29 15:47:37 +000090TEST(time, mktime_TZ_as_UTC_and_offset) {
91 struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1};
92
93 // This TZ value is not a valid Olson ID and is not present in tzdata file,
94 // but is a valid TZ string according to POSIX standard.
95 setenv("TZ", "UTC+08:00:00", 1);
96 tzset();
97 ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm));
98}
99
Elliott Hughes329103d2014-04-25 16:55:04 -0700100static void* gmtime_no_stack_overflow_14313703_fn(void*) {
101 const char* original_tz = getenv("TZ");
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700102 // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist.
Elliott Hughes329103d2014-04-25 16:55:04 -0700103 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
104 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700105 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -0700106 setenv("TZ", original_tz, 1);
107 }
108 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -0700110}
111
112TEST(time, gmtime_no_stack_overflow_14313703) {
113 // Is it safe to call tzload on a thread with a small stack?
114 // http://b/14313703
115 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800116 pthread_attr_t a;
117 ASSERT_EQ(0, pthread_attr_init(&a));
118 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700119
120 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800122 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700123}
124
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900125TEST(time, mktime_empty_TZ) {
126 // tzcode used to have a bug where it didn't reinitialize some internal state.
127
128 // Choose a time where DST is set.
Christopher Ferris2a391882024-12-19 13:44:35 -0800129 struct tm t = {};
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900130 t.tm_year = 1980 - 1900;
131 t.tm_mon = 6;
132 t.tm_mday = 2;
133
134 setenv("TZ", "America/Los_Angeles", 1);
135 tzset();
136 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
137
Christopher Ferris2a391882024-12-19 13:44:35 -0800138 t = {};
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900139 t.tm_year = 1980 - 1900;
140 t.tm_mon = 6;
141 t.tm_mday = 2;
142
143 setenv("TZ", "", 1); // Implies UTC.
144 tzset();
145 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
146}
147
Elliott Hughes7843d442013-08-22 11:37:32 -0700148TEST(time, mktime_10310929) {
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100149 struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10};
Elliott Hughes7843d442013-08-22 11:37:32 -0700150
Elliott Hughes0c401522013-10-18 16:21:54 -0700151#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700152 // 32-bit bionic has a signed 32-bit time_t.
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100153 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700154 ASSERT_ERRNO(EOVERFLOW);
Elliott Hughes0c401522013-10-18 16:21:54 -0700155#else
156 // Everyone else should be using a signed 64-bit time_t.
157 ASSERT_GE(sizeof(time_t) * 8, 64U);
158
159 setenv("TZ", "America/Los_Angeles", 1);
160 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700161 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700162
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100163 // On the date/time specified by tm America/Los_Angeles
164 // follows DST. But tm_isdst is set to 0, which forces
165 // mktime to interpret that time as local standard, hence offset
166 // is 8 hours, not 7.
167 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700168 ASSERT_ERRNO(0);
Elliott Hughes7843d442013-08-22 11:37:32 -0700169#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800170}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800171
Elliott Hughes7c9c1222024-01-12 23:46:29 +0000172TEST(time, mktime_EOVERFLOW) {
Elliott Hughesf52b2162023-05-19 16:09:47 -0700173 setenv("TZ", "UTC", 1);
174
Christopher Ferris2a391882024-12-19 13:44:35 -0800175 struct tm t = {};
Elliott Hughes47126ed2016-09-06 13:25:53 -0700176
177 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
178 t.tm_year = 2016 - 1900;
179
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700180 t.tm_mon = 2;
181 t.tm_mday = 10;
182
183 errno = 0;
184 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700185 ASSERT_ERRNO(0);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700186
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100187 // This will overflow for LP32.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700188 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700189
190 errno = 0;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100191#if !defined(__LP64__)
192 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700193 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100194#else
195 ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700196 ASSERT_ERRNO(0);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100197#endif
198
199 // This will overflow for LP32 or LP64.
200 // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is.
201 t.tm_year = INT_MAX;
202 t.tm_mon = 11;
203 t.tm_mday = 45;
204
205 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700206 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
Elliott Hughes95646e62023-09-21 14:11:19 -0700207 ASSERT_ERRNO(EOVERFLOW);
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700208}
209
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000210TEST(time, mktime_invalid_tm_TZ_combination) {
211 setenv("TZ", "UTC", 1);
212
Christopher Ferris2a391882024-12-19 13:44:35 -0800213 struct tm t = {};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000214 t.tm_year = 2022 - 1900;
215 t.tm_mon = 11;
216 t.tm_mday = 31;
217 // UTC does not observe DST
218 t.tm_isdst = 1;
219
220 errno = 0;
221
222 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
223 // mktime sets errno to EOVERFLOW if result is unrepresentable.
Elliott Hughes95646e62023-09-21 14:11:19 -0700224 EXPECT_ERRNO(EOVERFLOW);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000225}
226
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100227// Transitions in the tzdata file are generated up to the year 2100. Testing
228// that dates beyond that are handled properly too.
229TEST(time, mktime_after_2100) {
230 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
231
232#if !defined(__LP64__)
233 // 32-bit bionic has a signed 32-bit time_t.
234 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700235 ASSERT_ERRNO(EOVERFLOW);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100236#else
237 setenv("TZ", "Europe/London", 1);
238 tzset();
239 errno = 0;
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100240 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
Elliott Hughes95646e62023-09-21 14:11:19 -0700241 ASSERT_ERRNO(0);
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100242#endif
243}
244
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700245TEST(time, strftime) {
246 setenv("TZ", "UTC", 1);
247
Christopher Ferris2a391882024-12-19 13:44:35 -0800248 struct tm t = {};
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700249 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
Christopher Ferris2a391882024-12-19 13:44:35 -0800269 struct tm t = {};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000270 t.tm_year = 1969 - 1900;
271 t.tm_mon = 11;
272 t.tm_mday = 31;
273 t.tm_hour = 23;
274 t.tm_min = 59;
275 t.tm_sec = 59;
276
277 char buf[64];
278
279 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
280 EXPECT_STREQ("-1", buf);
281}
282
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100283TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800284 // Netflix on Nexus Player wouldn't start (http://b/25170306).
Christopher Ferris2a391882024-12-19 13:44:35 -0800285 struct tm t = {};
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800286 char buf[64];
287
288 setenv("TZ", "America/Los_Angeles", 1);
289 tzset();
290
291 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
292 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
293 EXPECT_STREQ("<PST>", buf);
294
295#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
296 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
297 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
298 EXPECT_STREQ("<PDT>", buf);
299
300 t.tm_isdst = -123; // "and negative if the information is not available".
301 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
302 EXPECT_STREQ("<>", buf);
303#endif
304
305 setenv("TZ", "UTC", 1);
306 tzset();
307
308 t.tm_isdst = 0;
309 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
310 EXPECT_STREQ("<UTC>", buf);
311
312#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
313 t.tm_isdst = 1; // UTC has no DST.
314 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
315 EXPECT_STREQ("<>", buf);
316#endif
317}
318
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100319// According to C language specification the only tm struct field needed to
320// find out replacement for %z and %Z in strftime is tm_isdst. Which is
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700321// wrong, as timezones change their standard offset and even DST savings.
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100322// tzcode deviates from C language specification and requires tm struct either
323// to be output of localtime-like functions or to be modified by mktime call
324// before passing to strftime. See tz mailing discussion for more details
325// https://mm.icann.org/pipermail/tz/2022-July/031674.html
326// But we are testing case when tm.tm_zone is null, which means that tm struct
327// is not coming from localtime and is neither modified by mktime. That's why
328// we are comparing against +0000, even though America/Los_Angeles never
329// observes it.
330TEST(time, strftime_z_null_tm_zone) {
331 char str[64];
332 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
333
334 setenv("TZ", "America/Los_Angeles", 1);
335 tzset();
336
337 tm.tm_zone = NULL;
338
339 size_t result = strftime(str, sizeof(str), "%z", &tm);
340
341 EXPECT_EQ(5U, result);
342 EXPECT_STREQ("+0000", str);
343
344 tm.tm_isdst = 1;
345
346 result = strftime(str, sizeof(str), "%z", &tm);
347
348 EXPECT_EQ(5U, result);
349 EXPECT_STREQ("+0000", str);
350
351 setenv("TZ", "UTC", 1);
352 tzset();
353
354 tm.tm_isdst = 0;
355
356 result = strftime(str, sizeof(str), "%z", &tm);
357
358 EXPECT_EQ(5U, result);
359 EXPECT_STREQ("+0000", str);
360
361 tm.tm_isdst = 1;
362
363 result = strftime(str, sizeof(str), "%z", &tm);
364
365 EXPECT_EQ(5U, result);
366 EXPECT_STREQ("+0000", str);
367}
368
369TEST(time, strftime_z_Europe_Lisbon) {
370 char str[64];
371 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
372 // tm_isdst is not set as it will be overridden by mktime call anyway.
373 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
374
375 setenv("TZ", "Europe/Lisbon", 1);
376 tzset();
377
378 // tzcode's strftime implementation for %z relies on prior mktime call.
379 // At the moment of writing %z value is taken from tm_gmtoff. So without
380 // mktime call %z is replaced with +0000.
381 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
382 mktime(&tm);
383
384 size_t result = strftime(str, sizeof(str), "%z", &tm);
385
386 EXPECT_EQ(5U, result);
387 EXPECT_STREQ("+0100", str);
388
389 // Now standard offset is 0.
390 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
391
392 mktime(&tm);
393 result = strftime(str, sizeof(str), "%z", &tm);
394
395 EXPECT_EQ(5U, result);
396 EXPECT_STREQ("+0000", str);
397}
398
Elliott Hughes0a610d02016-07-29 14:04:17 -0700399TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700400 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700401 locale_t old_locale = uselocale(cloc);
402
403 setenv("TZ", "UTC", 1);
404
Christopher Ferris2a391882024-12-19 13:44:35 -0800405 struct tm t = {};
Elliott Hughes0a610d02016-07-29 14:04:17 -0700406 t.tm_year = 200;
407 t.tm_mon = 2;
408 t.tm_mday = 10;
409
410 // Date and time as text.
411 char buf[64];
412 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
413 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
414
415 uselocale(old_locale);
416 freelocale(cloc);
417}
418
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700419TEST(time, strptime) {
420 setenv("TZ", "UTC", 1);
421
Christopher Ferris2a391882024-12-19 13:44:35 -0800422 struct tm t = {};
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700423 char buf[64];
424
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700425 strptime("11:14", "%R", &t);
426 strftime(buf, sizeof(buf), "%H:%M", &t);
427 EXPECT_STREQ("11:14", buf);
428
Christopher Ferris2a391882024-12-19 13:44:35 -0800429 t = {};
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700430 strptime("09:41:53", "%T", &t);
431 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
432 EXPECT_STREQ("09:41:53", buf);
433}
434
Elliott Hughes3376c232018-02-13 23:14:12 -0800435TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700436#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800437 setenv("TZ", "UTC", 1);
438
Christopher Ferris2a391882024-12-19 13:44:35 -0800439 struct tm t = {};
Elliott Hughes3376c232018-02-13 23:14:12 -0800440 char buf[64];
441
Elliott Hughes3376c232018-02-13 23:14:12 -0800442 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
443 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
444 EXPECT_STREQ("11:14", buf);
445
Christopher Ferris2a391882024-12-19 13:44:35 -0800446 t = {};
Elliott Hughes3376c232018-02-13 23:14:12 -0800447 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
448 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
449 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700450#else
451 GTEST_SKIP() << "musl doesn't support strptime_l";
452#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800453}
454
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700455TEST(time, strptime_F) {
456 setenv("TZ", "UTC", 1);
457
458 struct tm tm = {};
459 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
460 EXPECT_EQ(119, tm.tm_year);
461 EXPECT_EQ(2, tm.tm_mon);
462 EXPECT_EQ(26, tm.tm_mday);
463}
464
465TEST(time, strptime_P_p) {
466 setenv("TZ", "UTC", 1);
467
Elliott Hughes11678822019-03-27 08:56:49 -0700468 // For parsing, %P and %p are the same: case doesn't matter.
469
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700470 struct tm tm = {.tm_hour = 12};
471 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
472 EXPECT_EQ(0, tm.tm_hour);
473
474 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700475 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
476 EXPECT_EQ(0, tm.tm_hour);
477
478 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700479 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
480 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700481
482 tm = {.tm_hour = 12};
483 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
484 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700485}
486
Elliott Hughesdd56beb2025-08-26 14:53:48 +0000487TEST(time, strptime_s) {
488 setenv("TZ", "UTC", 1);
489
490 struct tm tm;
491
492 // 0 + 1 --- trivial.
493 tm = {};
494 ASSERT_EQ('\0', *strptime("1", "%s", &tm));
495 EXPECT_EQ(70, tm.tm_year);
496 EXPECT_EQ(0, tm.tm_mon);
497 EXPECT_EQ(1, tm.tm_mday);
498 EXPECT_EQ(0, tm.tm_hour);
499 EXPECT_EQ(0, tm.tm_min);
500 EXPECT_EQ(1, tm.tm_sec);
501
502 // INT32_MAX (aka "time_t max" for ILP32).
503 tm = {};
504 ASSERT_EQ('\0', *strptime("2147483647", "%s", &tm));
505 EXPECT_EQ(138, tm.tm_year);
506 EXPECT_EQ(0, tm.tm_mon);
507 EXPECT_EQ(19, tm.tm_mday);
508 EXPECT_EQ(3, tm.tm_hour);
509 EXPECT_EQ(14, tm.tm_min);
510 EXPECT_EQ(7, tm.tm_sec);
511
512 // INT32_MAX + 1 (aka overflow for ILP32).
513 // This should be easy to detect because it'll be negative.
514 tm = {};
515#if defined(__LP64__)
516 ASSERT_EQ('\0', *strptime("2147483648", "%s", &tm));
517#else
518 ASSERT_EQ(nullptr, strptime("2147483648", "%s", &tm));
519#endif
520
521 // This wraps to 1 as an int32_t.
522 tm = {};
523#if defined(__LP64__)
524 ASSERT_EQ('\0', *strptime("4294967297", "%s", &tm));
525 EXPECT_EQ(206, tm.tm_year);
526 EXPECT_EQ(1, tm.tm_mon);
527 EXPECT_EQ(7, tm.tm_mday);
528 EXPECT_EQ(6, tm.tm_hour);
529 EXPECT_EQ(28, tm.tm_min);
530 EXPECT_EQ(17, tm.tm_sec);
531#else
532 ASSERT_EQ(nullptr, strptime("4294967297", "%s", &tm));
533#endif
534
535 // INT64_MAX (aka "time_t max" for LP64).
536 // This actually fails for LP64 too...
537 // ...but in localtime_r() because the year is too large.
538 // (Wolfram Alpha says this is 21 times the age of the universe!)
539 tm = {};
540 ASSERT_EQ(nullptr, strptime("9223372036854775807", "%s", &tm));
541}
542
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700543TEST(time, strptime_u) {
544 setenv("TZ", "UTC", 1);
545
546 struct tm tm = {};
547 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
548 EXPECT_EQ(2, tm.tm_wday);
549}
550
551TEST(time, strptime_v) {
552 setenv("TZ", "UTC", 1);
553
554 struct tm tm = {};
555 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
556 EXPECT_EQ(80, tm.tm_year);
557 EXPECT_EQ(2, tm.tm_mon);
558 EXPECT_EQ(26, tm.tm_mday);
559}
560
561TEST(time, strptime_V_G_g) {
562 setenv("TZ", "UTC", 1);
563
564 // %V (ISO-8601 week number), %G (year of week number, without century), and
565 // %g (year of week number) have no effect when parsed, and are supported
566 // solely so that it's possible for strptime(3) to parse everything that
567 // strftime(3) can output.
568 struct tm tm = {};
569 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
570 struct tm zero = {};
571 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
572}
573
Elliott Hughesd065c042020-09-01 19:02:44 -0700574TEST(time, strptime_Z) {
575#if defined(__BIONIC__)
576 // glibc doesn't handle %Z at all.
577 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
578 // are in the global `tzname` (which correspond to the current $TZ).
579 struct tm tm;
580 setenv("TZ", "Europe/Berlin", 1);
581
582 // "GMT" always works.
583 tm = {};
584 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
585 EXPECT_STREQ("GMT", tm.tm_zone);
586 EXPECT_EQ(0, tm.tm_isdst);
587 EXPECT_EQ(0, tm.tm_gmtoff);
588
589 // As does "UTC".
590 tm = {};
591 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
592 EXPECT_STREQ("UTC", tm.tm_zone);
593 EXPECT_EQ(0, tm.tm_isdst);
594 EXPECT_EQ(0, tm.tm_gmtoff);
595
596 // Europe/Berlin is known as "CET" when there's no DST.
597 tm = {};
598 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
599 EXPECT_STREQ("CET", tm.tm_zone);
600 EXPECT_EQ(0, tm.tm_isdst);
601 EXPECT_EQ(3600, tm.tm_gmtoff);
602
603 // Europe/Berlin is known as "CEST" when there's no DST.
604 tm = {};
605 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
606 EXPECT_STREQ("CEST", tm.tm_zone);
607 EXPECT_EQ(1, tm.tm_isdst);
608 EXPECT_EQ(3600, tm.tm_gmtoff);
609
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700610 // And as long as we're in Europe/Berlin, those are the only timezone
Elliott Hughesd065c042020-09-01 19:02:44 -0700611 // abbreviations that are recognized.
612 tm = {};
613 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
614#endif
615}
616
617TEST(time, strptime_z) {
618 struct tm tm;
619 setenv("TZ", "Europe/Berlin", 1);
620
621 // "UT" is what RFC822 called UTC.
622 tm = {};
623 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
624 EXPECT_STREQ("UTC", tm.tm_zone);
625 EXPECT_EQ(0, tm.tm_isdst);
626 EXPECT_EQ(0, tm.tm_gmtoff);
627 // "GMT" is RFC822's other name for UTC.
628 tm = {};
629 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
630 EXPECT_STREQ("UTC", tm.tm_zone);
631 EXPECT_EQ(0, tm.tm_isdst);
632 EXPECT_EQ(0, tm.tm_gmtoff);
633
634 // "Z" ("Zulu") is a synonym for UTC.
635 tm = {};
636 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
637 EXPECT_STREQ("UTC", tm.tm_zone);
638 EXPECT_EQ(0, tm.tm_isdst);
639 EXPECT_EQ(0, tm.tm_gmtoff);
640
641 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
642 tm = {};
643 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
644 EXPECT_STREQ("PST", tm.tm_zone);
645 EXPECT_EQ(0, tm.tm_isdst);
646 EXPECT_EQ(-28800, tm.tm_gmtoff);
647 tm = {};
648 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
649 EXPECT_STREQ("PDT", tm.tm_zone);
650 EXPECT_EQ(1, tm.tm_isdst);
651 EXPECT_EQ(-25200, tm.tm_gmtoff);
652
653 // +-hh
654 tm = {};
655 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
656 EXPECT_EQ(3600, tm.tm_gmtoff);
657 EXPECT_TRUE(tm.tm_zone == nullptr);
658 EXPECT_EQ(0, tm.tm_isdst);
659 // +-hhmm
660 tm = {};
661 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
662 EXPECT_EQ(5400, tm.tm_gmtoff);
663 EXPECT_TRUE(tm.tm_zone == nullptr);
664 EXPECT_EQ(0, tm.tm_isdst);
665 // +-hh:mm
666 tm = {};
667 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
668 EXPECT_EQ(5400, tm.tm_gmtoff);
669 EXPECT_TRUE(tm.tm_zone == nullptr);
670 EXPECT_EQ(0, tm.tm_isdst);
671}
672
Elliott Hughes4b558f52014-03-04 15:58:02 -0800673void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
674 itimerspec ts;
675 ts.it_value.tv_sec = value_s;
676 ts.it_value.tv_nsec = value_ns;
677 ts.it_interval.tv_sec = interval_s;
678 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700679 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800680}
681
Colin Cross7da20342021-07-28 11:18:11 -0700682static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800683}
684
685TEST(time, timer_create) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800686 sigevent se = {};
Elliott Hughes4b558f52014-03-04 15:58:02 -0800687 se.sigev_notify = SIGEV_THREAD;
688 se.sigev_notify_function = NoOpNotifyFunction;
689 timer_t timer_id;
690 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
691
Elliott Hughes33697a02016-01-26 13:04:57 -0800692 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800693 ASSERT_NE(-1, pid) << strerror(errno);
694
695 if (pid == 0) {
696 // Timers are not inherited by the child.
697 ASSERT_EQ(-1, timer_delete(timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700698 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699 _exit(0);
700 }
701
Elliott Hughes33697a02016-01-26 13:04:57 -0800702 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800703
704 ASSERT_EQ(0, timer_delete(timer_id));
705}
706
Yabin Cui95f1ee22015-01-13 19:53:15 -0800707static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800708static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
709 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
710 ASSERT_EQ(SIGUSR1, signal_number);
711}
712
713TEST(time, timer_create_SIGEV_SIGNAL) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800714 sigevent se = {};
Elliott Hughes4b558f52014-03-04 15:58:02 -0800715 se.sigev_notify = SIGEV_SIGNAL;
716 se.sigev_signo = SIGUSR1;
717
718 timer_t timer_id;
719 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
720
Yabin Cui95f1ee22015-01-13 19:53:15 -0800721 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800722 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
723
724 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
725
726 itimerspec ts;
727 ts.it_value.tv_sec = 0;
728 ts.it_value.tv_nsec = 1;
729 ts.it_interval.tv_sec = 0;
730 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700731 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800732
733 usleep(500000);
734 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
735}
736
737struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800738 private:
739 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800740 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700741 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700742 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800743
Elliott Hughes4b558f52014-03-04 15:58:02 -0800744 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700745 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800746 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700747 timer_valid = true;
748 }
749
Yabin Cui95f1ee22015-01-13 19:53:15 -0800750 public:
Colin Cross7da20342021-07-28 11:18:11 -0700751 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800752 se = {.sigev_notify = SIGEV_THREAD, .sigev_notify_function = fn, .sigev_value.sival_ptr = this};
Yabin Cui95f1ee22015-01-13 19:53:15 -0800753 Create();
754 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700755 void DeleteTimer() {
756 ASSERT_TRUE(timer_valid);
757 ASSERT_EQ(0, timer_delete(timer_id));
758 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800759 }
760
761 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700762 if (timer_valid) {
763 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800764 }
765 }
766
Yabin Cui95f1ee22015-01-13 19:53:15 -0800767 int Value() const {
768 return value;
769 }
770
Christopher Ferris62d84b12014-10-20 19:09:19 -0700771 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
772 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
773 }
774
775 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800776 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700777 time_t start = time(nullptr);
778 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700779 }
780 return current_value != value;
781 }
782
Colin Cross7da20342021-07-28 11:18:11 -0700783 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800784 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
785 ++cd->value;
786 }
787
Colin Cross7da20342021-07-28 11:18:11 -0700788 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800789 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
790 ++cd->value;
791
792 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700793 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800794 }
795};
796
797TEST(time, timer_settime_0) {
798 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800799 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800800
Yabin Cuibf572d92015-08-11 11:23:16 -0700801 counter.SetTime(0, 500000000, 1, 0);
802 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800803
804 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800805 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800806}
807
808TEST(time, timer_settime_repeats) {
809 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800810 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800811
Christopher Ferris62d84b12014-10-20 19:09:19 -0700812 counter.SetTime(0, 1, 0, 10);
813 ASSERT_TRUE(counter.ValueUpdated());
814 ASSERT_TRUE(counter.ValueUpdated());
815 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800816 counter.DeleteTimer();
817 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
818 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800819}
820
Yabin Cui95f1ee22015-01-13 19:53:15 -0800821static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800822static void timer_create_NULL_signal_handler(int signal_number) {
823 ++timer_create_NULL_signal_handler_invocation_count;
824 ASSERT_EQ(SIGALRM, signal_number);
825}
826
827TEST(time, timer_create_NULL) {
828 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
829 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700830 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800831
Yabin Cui95f1ee22015-01-13 19:53:15 -0800832 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800833 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
834
835 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
836
837 SetTime(timer_id, 0, 1, 0, 0);
838 usleep(500000);
839
840 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
841}
842
Elliott Hughesc793bc02024-05-21 21:35:49 +0000843static int GetThreadCount() {
844 std::string status;
845 if (android::base::ReadFileToString("/proc/self/status", &status)) {
846 for (const auto& line : android::base::Split(status, "\n")) {
847 int thread_count;
848 if (sscanf(line.c_str(), "Threads: %d", &thread_count) == 1) {
849 return thread_count;
850 }
851 }
852 }
853 return -1;
854}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800855
Elliott Hughesc793bc02024-05-21 21:35:49 +0000856TEST(time, timer_create_EINVAL) {
857 const clockid_t kInvalidClock = 16;
858
859 // A SIGEV_SIGNAL timer failure is easy; that's the kernel's problem.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800860 timer_t timer_id;
Elliott Hughesc793bc02024-05-21 21:35:49 +0000861 ASSERT_EQ(-1, timer_create(kInvalidClock, nullptr, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700862 ASSERT_ERRNO(EINVAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800863
Elliott Hughesc793bc02024-05-21 21:35:49 +0000864 // A SIGEV_THREAD timer failure is more interesting because we have a thread
865 // to clean up (https://issuetracker.google.com/340125671).
866 sigevent se = {};
Elliott Hughes4b558f52014-03-04 15:58:02 -0800867 se.sigev_notify = SIGEV_THREAD;
868 se.sigev_notify_function = NoOpNotifyFunction;
Elliott Hughesc793bc02024-05-21 21:35:49 +0000869 ASSERT_EQ(-1, timer_create(kInvalidClock, &se, &timer_id));
Elliott Hughes95646e62023-09-21 14:11:19 -0700870 ASSERT_ERRNO(EINVAL);
Elliott Hughesc793bc02024-05-21 21:35:49 +0000871
872 // timer_create() doesn't guarantee that the thread will be dead _before_
873 // it returns because that would require extra synchronization that's
874 // unnecessary in the normal (successful) case. A timeout here means we
875 // leaked a thread.
876 while (GetThreadCount() > 1) {
877 }
Elliott Hughes4b558f52014-03-04 15:58:02 -0800878}
879
Elliott Hughes4b558f52014-03-04 15:58:02 -0800880TEST(time, timer_create_multiple) {
881 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800882 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800883 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800884
Yabin Cui95f1ee22015-01-13 19:53:15 -0800885 ASSERT_EQ(0, counter1.Value());
886 ASSERT_EQ(0, counter2.Value());
887 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800888
Yabin Cui410c1ad2015-06-18 16:19:02 -0700889 counter2.SetTime(0, 500000000, 0, 0);
890 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800891
Yabin Cui95f1ee22015-01-13 19:53:15 -0800892 EXPECT_EQ(0, counter1.Value());
893 EXPECT_EQ(1, counter2.Value());
894 EXPECT_EQ(0, counter3.Value());
895}
896
897// Test to verify that disarming a repeatable timer disables the callbacks.
898TEST(time, timer_disarm_terminates) {
899 Counter counter(Counter::CountNotifyFunction);
900 ASSERT_EQ(0, counter.Value());
901
902 counter.SetTime(0, 1, 0, 1);
903 ASSERT_TRUE(counter.ValueUpdated());
904 ASSERT_TRUE(counter.ValueUpdated());
905 ASSERT_TRUE(counter.ValueUpdated());
906
907 counter.SetTime(0, 0, 0, 0);
908 // Add a sleep as the kernel may have pending events when the timer is disarmed.
909 usleep(500000);
910 int value = counter.Value();
911 usleep(500000);
912
913 // Verify the counter has not been incremented.
914 ASSERT_EQ(value, counter.Value());
915}
916
917// Test to verify that deleting a repeatable timer disables the callbacks.
918TEST(time, timer_delete_terminates) {
919 Counter counter(Counter::CountNotifyFunction);
920 ASSERT_EQ(0, counter.Value());
921
922 counter.SetTime(0, 1, 0, 1);
923 ASSERT_TRUE(counter.ValueUpdated());
924 ASSERT_TRUE(counter.ValueUpdated());
925 ASSERT_TRUE(counter.ValueUpdated());
926
927 counter.DeleteTimer();
928 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
929 usleep(500000);
930 int value = counter.Value();
931 usleep(500000);
932
933 // Verify the counter has not been incremented.
934 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800935}
Christopher Ferris753ad772014-03-20 20:47:45 -0700936
937struct TimerDeleteData {
938 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800939 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700940 volatile bool complete;
941};
942
Colin Cross7da20342021-07-28 11:18:11 -0700943static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700944 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
945
Elliott Hughes11859d42017-02-13 17:59:29 -0800946 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700947 timer_delete(tdd->timer_id);
948 tdd->complete = true;
949}
950
951TEST(time, timer_delete_from_timer_thread) {
952 TimerDeleteData tdd;
Christopher Ferris2a391882024-12-19 13:44:35 -0800953 sigevent se = {};
Christopher Ferris753ad772014-03-20 20:47:45 -0700954 se.sigev_notify = SIGEV_THREAD;
955 se.sigev_notify_function = TimerDeleteCallback;
956 se.sigev_value.sival_ptr = &tdd;
957
958 tdd.complete = false;
959 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
960
961 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800962 ts.it_value.tv_sec = 1;
963 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700964 ts.it_interval.tv_sec = 0;
965 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700966 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700967
Yi Kong32bc0fc2018-08-02 17:31:13 -0700968 time_t cur_time = time(nullptr);
969 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700970 ASSERT_TRUE(tdd.complete);
971
972#if defined(__BIONIC__)
973 // Since bionic timers are implemented by creating a thread to handle the
974 // callback, verify that the thread actually completes.
975 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800976 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
977 ASSERT_EQ(-1, kill(tdd.tid, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700978 ASSERT_ERRNO(ESRCH);
Christopher Ferris753ad772014-03-20 20:47:45 -0700979#endif
980}
Elliott Hughes625993d2014-07-15 16:53:13 -0700981
Colin Cross35d469b2021-08-16 15:26:28 -0700982// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
983#if !defined(__NR_clock_gettime)
984#define __NR_clock_gettime __NR_clock_gettime32
985#endif
986
Elliott Hughes625993d2014-07-15 16:53:13 -0700987TEST(time, clock_gettime) {
988 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100989 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700990 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700991 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100992 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
993 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
994 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700995
Giuliano Procida096f5952021-04-08 10:51:58 +0100996 // Check we have a nice monotonic timestamp sandwich.
997 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
998 if (ts0.tv_sec == ts1.tv_sec) {
999 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -07001000 }
Giuliano Procida096f5952021-04-08 10:51:58 +01001001 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
1002 if (ts1.tv_sec == ts2.tv_sec) {
1003 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
1004 }
Elliott Hughes625993d2014-07-15 16:53:13 -07001005}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001006
Elliott Hughes3a8f75d2017-10-05 10:33:18 -07001007TEST(time, clock_gettime_CLOCK_REALTIME) {
1008 timespec ts;
1009 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1010}
1011
1012TEST(time, clock_gettime_CLOCK_MONOTONIC) {
1013 timespec ts;
1014 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1015}
1016
1017TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
1018 timespec ts;
1019 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
1020}
1021
1022TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
1023 timespec ts;
1024 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
1025}
1026
1027TEST(time, clock_gettime_CLOCK_BOOTTIME) {
1028 timespec ts;
1029 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
1030}
1031
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -08001032TEST(time, clock_gettime_unknown) {
1033 errno = 0;
1034 timespec ts;
1035 ASSERT_EQ(-1, clock_gettime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001036 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -08001037}
1038
1039TEST(time, clock_getres_CLOCK_REALTIME) {
1040 timespec ts;
1041 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
1042 ASSERT_EQ(1, ts.tv_nsec);
1043 ASSERT_EQ(0, ts.tv_sec);
1044}
1045
1046TEST(time, clock_getres_CLOCK_MONOTONIC) {
1047 timespec ts;
1048 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
1049 ASSERT_EQ(1, ts.tv_nsec);
1050 ASSERT_EQ(0, ts.tv_sec);
1051}
1052
1053TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
1054 timespec ts;
1055 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
1056}
1057
1058TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
1059 timespec ts;
1060 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
1061}
1062
1063TEST(time, clock_getres_CLOCK_BOOTTIME) {
1064 timespec ts;
1065 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
1066 ASSERT_EQ(1, ts.tv_nsec);
1067 ASSERT_EQ(0, ts.tv_sec);
1068}
1069
1070TEST(time, clock_getres_unknown) {
1071 errno = 0;
1072 timespec ts = { -1, -1 };
1073 ASSERT_EQ(-1, clock_getres(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001074 ASSERT_ERRNO(EINVAL);
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -08001075 ASSERT_EQ(-1, ts.tv_nsec);
1076 ASSERT_EQ(-1, ts.tv_sec);
1077}
1078
zijunzhaoe6202662023-01-03 23:32:18 +00001079TEST(time, clock_getres_null_resolution) {
1080 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr));
1081}
1082
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001083TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001084 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1085 static const clock_t N = 5;
1086 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001087 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001088 for (size_t i = 0; i < N; ++i) {
1089 sleep(1);
1090 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001091 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001092 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001093}
1094
Elliott Hughesb4413592017-11-29 18:17:06 -08001095static pid_t GetInvalidPid() {
1096 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001097 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001098 fscanf(fp.get(), "%ld", &pid_max);
1099 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001100}
1101
Elliott Hughesb4413592017-11-29 18:17:06 -08001102TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001103 clockid_t clockid;
1104 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001105 timespec ts;
1106 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001107}
Yabin Cuid5c65272014-11-26 14:04:26 -08001108
Elliott Hughesb4413592017-11-29 18:17:06 -08001109TEST(time, clock_getcpuclockid_parent) {
1110 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001111 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001112 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001113 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001114}
Yabin Cuid5c65272014-11-26 14:04:26 -08001115
Elliott Hughesb4413592017-11-29 18:17:06 -08001116TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001117 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1118 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001119 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001120 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001121 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1122 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1123 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1124 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1125 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1126 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Elliott Hughes95646e62023-09-21 14:11:19 -07001127 ASSERT_ERRNO(0);
Yabin Cuid5c65272014-11-26 14:04:26 -08001128}
1129
Haruki Hasegawa18160252014-10-13 00:50:47 +09001130TEST(time, clock_settime) {
1131 errno = 0;
1132 timespec ts;
1133 ASSERT_EQ(-1, clock_settime(-1, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -07001134 ASSERT_ERRNO(EINVAL);
Haruki Hasegawa18160252014-10-13 00:50:47 +09001135}
1136
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001137TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001138 timespec in;
1139 timespec out;
1140 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001141}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001142
1143TEST(time, clock_nanosleep_thread_cputime_id) {
1144 timespec in;
1145 in.tv_sec = 1;
1146 in.tv_nsec = 0;
1147 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1148}
Elliott Hughes12443702016-10-19 16:02:31 -07001149
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001150TEST(time, clock_nanosleep) {
1151 auto t0 = std::chrono::steady_clock::now();
1152 const timespec ts = {.tv_nsec = 5000000};
1153 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1154 auto t1 = std::chrono::steady_clock::now();
1155 ASSERT_GE(t1-t0, 5000000ns);
1156}
1157
1158TEST(time, nanosleep) {
1159 auto t0 = std::chrono::steady_clock::now();
1160 const timespec ts = {.tv_nsec = 5000000};
1161 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1162 auto t1 = std::chrono::steady_clock::now();
1163 ASSERT_GE(t1-t0, 5000000ns);
1164}
1165
1166TEST(time, nanosleep_EINVAL) {
1167 timespec ts = {.tv_sec = -1};
1168 errno = 0;
1169 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -07001170 ASSERT_ERRNO(EINVAL);
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001171}
1172
Elliott Hughes12443702016-10-19 16:02:31 -07001173TEST(time, bug_31938693) {
1174 // User-visible symptoms in N:
1175 // http://b/31938693
1176 // https://code.google.com/p/android/issues/detail?id=225132
1177
1178 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1179 // http://b/31848040
1180
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001181 // This isn't a great test, because very few timezones were actually affected, and there's
Elliott Hughes12443702016-10-19 16:02:31 -07001182 // no real logic to which ones were affected: it was just a coincidence of the data that came
1183 // after them in the tzdata file.
1184
1185 time_t t = 1475619727;
1186 struct tm tm;
1187
1188 setenv("TZ", "America/Los_Angeles", 1);
1189 tzset();
1190 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1191 EXPECT_EQ(15, tm.tm_hour);
1192
1193 setenv("TZ", "Europe/London", 1);
1194 tzset();
1195 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1196 EXPECT_EQ(23, tm.tm_hour);
1197
1198 setenv("TZ", "America/Atka", 1);
1199 tzset();
1200 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1201 EXPECT_EQ(13, tm.tm_hour);
1202
1203 setenv("TZ", "Pacific/Apia", 1);
1204 tzset();
1205 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1206 EXPECT_EQ(12, tm.tm_hour);
1207
1208 setenv("TZ", "Pacific/Honolulu", 1);
1209 tzset();
1210 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1211 EXPECT_EQ(12, tm.tm_hour);
1212
1213 setenv("TZ", "Asia/Magadan", 1);
1214 tzset();
1215 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1216 EXPECT_EQ(9, tm.tm_hour);
1217}
Elliott Hughesea877162017-01-11 14:34:16 -08001218
1219TEST(time, bug_31339449) {
1220 // POSIX says localtime acts as if it calls tzset.
1221 // tzset does two things:
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001222 // 1. it sets the timezone ctime/localtime/mktime/strftime will use.
Elliott Hughesea877162017-01-11 14:34:16 -08001223 // 2. it sets the global `tzname`.
1224 // POSIX says localtime_r need not set `tzname` (2).
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001225 // Q: should localtime_r set the timezone (1)?
Elliott Hughesea877162017-01-11 14:34:16 -08001226 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1227
1228 // Pick a time, any time...
1229 time_t t = 1475619727;
1230
1231 // Call tzset with a specific timezone.
1232 setenv("TZ", "America/Atka", 1);
1233 tzset();
1234
1235 // If we change the timezone and call localtime, localtime should use the new timezone.
1236 setenv("TZ", "America/Los_Angeles", 1);
1237 struct tm* tm_p = localtime(&t);
1238 EXPECT_EQ(15, tm_p->tm_hour);
1239
1240 // Reset the timezone back.
1241 setenv("TZ", "America/Atka", 1);
1242 tzset();
1243
1244#if defined(__BIONIC__)
1245 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1246 setenv("TZ", "America/Los_Angeles", 1);
1247 struct tm tm = {};
1248 localtime_r(&t, &tm);
1249 EXPECT_EQ(15, tm.tm_hour);
1250#else
1251 // The BSDs agree with us, but glibc gets this wrong.
1252#endif
1253}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001254
1255TEST(time, asctime) {
Elliott Hughesb891dda2025-08-27 07:19:07 -07001256 const tm tm = {};
Elliott Hughes5a29d542017-12-07 16:05:57 -08001257 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1258}
1259
1260TEST(time, asctime_r) {
Elliott Hughesb891dda2025-08-27 07:19:07 -07001261 const tm tm = {};
Elliott Hughes5a29d542017-12-07 16:05:57 -08001262 char buf[256];
1263 ASSERT_EQ(buf, asctime_r(&tm, buf));
1264 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1265}
1266
Elliott Hughesb891dda2025-08-27 07:19:07 -07001267TEST(time, asctime_nullptr) {
1268 tm* smuggled_null = nullptr;
1269 char buf[256];
1270 // I'd argue that the glibc behavior is more reasonable,
1271 // but traditionally we've had the BSD behavior.
1272 errno = 0;
1273#if defined(__GLIBC__)
1274 ASSERT_EQ(nullptr, asctime_r(smuggled_null, buf));
1275#else
1276 ASSERT_EQ(buf, asctime_r(smuggled_null, buf));
1277 ASSERT_STREQ("??? ??? ?? ??:??:?? ????\n", buf);
1278#endif
1279 ASSERT_ERRNO(EINVAL);
1280}
1281
1282TEST(time, asctime_bad_wday) {
1283 // This is undefined behavior, but our traditional behavior is to substitute "???".
1284 tm tm = { .tm_wday = -1 };
1285 char buf[256];
1286 ASSERT_EQ(buf, asctime_r(&tm, buf));
1287 ASSERT_STREQ("??? Jan 0 00:00:00 1900\n", buf);
1288 tm.tm_wday = 7;
1289 ASSERT_EQ(buf, asctime_r(&tm, buf));
1290 ASSERT_STREQ("??? Jan 0 00:00:00 1900\n", buf);
1291}
1292
1293TEST(time, asctime_bad_mon) {
1294 // This is undefined behavior, but our traditional behavior is to substitute "???".
1295 tm tm = { .tm_mon = -1 };
1296 char buf[256];
1297 ASSERT_EQ(buf, asctime_r(&tm, buf));
1298 ASSERT_STREQ("Sun ??? 0 00:00:00 1900\n", buf);
1299 tm.tm_mon = 12;
1300 ASSERT_EQ(buf, asctime_r(&tm, buf));
1301 ASSERT_STREQ("Sun ??? 0 00:00:00 1900\n", buf);
1302}
1303
1304TEST(time, asctime_bad_year) {
1305 // This is undefined behavior, but our traditional behavior is to return NULL/EOVERFLOW.
1306 tm tm = { .tm_year = 99999 };
1307 char buf[256];
1308 errno = 0;
1309 ASSERT_EQ(nullptr, asctime_r(&tm, buf));
1310 ASSERT_ERRNO(EOVERFLOW);
1311}
1312
Elliott Hughes5a29d542017-12-07 16:05:57 -08001313TEST(time, ctime) {
1314 setenv("TZ", "UTC", 1);
1315 const time_t t = 0;
1316 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1317}
1318
1319TEST(time, ctime_r) {
1320 setenv("TZ", "UTC", 1);
1321 const time_t t = 0;
1322 char buf[256];
1323 ASSERT_EQ(buf, ctime_r(&t, buf));
1324 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1325}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001326
1327// https://issuetracker.google.com/37128336
1328TEST(time, strftime_strptime_s) {
1329 char buf[32];
1330 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1331
1332 setenv("TZ", "America/Los_Angeles", 1);
1333 strftime(buf, sizeof(buf), "<%s>", &tm0);
1334 EXPECT_STREQ("<378720000>", buf);
1335
1336 setenv("TZ", "UTC", 1);
1337 strftime(buf, sizeof(buf), "<%s>", &tm0);
1338 EXPECT_STREQ("<378691200>", buf);
1339
Elliott Hughes81baaf22018-02-28 15:22:48 -08001340 setenv("TZ", "America/Los_Angeles", 1);
1341 tzset();
Christopher Ferris2a391882024-12-19 13:44:35 -08001342 struct tm tm = {};
Elliott Hughes81baaf22018-02-28 15:22:48 -08001343 char* p = strptime("378720000x", "%s", &tm);
1344 ASSERT_EQ('x', *p);
1345 EXPECT_EQ(0, tm.tm_sec);
1346 EXPECT_EQ(0, tm.tm_min);
1347 EXPECT_EQ(0, tm.tm_hour);
1348 EXPECT_EQ(1, tm.tm_mday);
1349 EXPECT_EQ(0, tm.tm_mon);
1350 EXPECT_EQ(82, tm.tm_year);
1351 EXPECT_EQ(5, tm.tm_wday);
1352 EXPECT_EQ(0, tm.tm_yday);
1353 EXPECT_EQ(0, tm.tm_isdst);
1354
1355 setenv("TZ", "UTC", 1);
1356 tzset();
Christopher Ferris2a391882024-12-19 13:44:35 -08001357 tm = {};
Elliott Hughes81baaf22018-02-28 15:22:48 -08001358 p = strptime("378691200x", "%s", &tm);
1359 ASSERT_EQ('x', *p);
1360 EXPECT_EQ(0, tm.tm_sec);
1361 EXPECT_EQ(0, tm.tm_min);
1362 EXPECT_EQ(0, tm.tm_hour);
1363 EXPECT_EQ(1, tm.tm_mday);
1364 EXPECT_EQ(0, tm.tm_mon);
1365 EXPECT_EQ(82, tm.tm_year);
1366 EXPECT_EQ(5, tm.tm_wday);
1367 EXPECT_EQ(0, tm.tm_yday);
1368 EXPECT_EQ(0, tm.tm_isdst);
1369}
1370
1371TEST(time, strptime_s_nothing) {
1372 struct tm tm;
1373 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1374}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001375
1376TEST(time, timespec_get) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001377#if defined(__BIONIC__)
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001378 timespec ts = {};
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001379 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001380 ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC));
1381 ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE));
1382 ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE));
1383#else
1384 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1385#endif
1386}
1387
1388TEST(time, timespec_get_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001389#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001390 timespec ts = {};
Elliott Hughes7db0a6c2023-05-03 15:37:46 -07001391 ASSERT_EQ(0, timespec_get(&ts, 123));
Elliott Hughes52541ee2023-04-24 17:04:49 -07001392#else
1393 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1394#endif
1395}
1396
1397TEST(time, timespec_getres) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001398#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001399 timespec ts = {};
1400 ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC));
1401 ASSERT_EQ(1, ts.tv_nsec);
1402 ASSERT_EQ(0, ts.tv_sec);
1403#else
1404 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1405#endif
1406}
1407
1408TEST(time, timespec_getres_invalid) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001409#if defined(__BIONIC__)
Elliott Hughes52541ee2023-04-24 17:04:49 -07001410 timespec ts = {};
1411 ASSERT_EQ(0, timespec_getres(&ts, 123));
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001412#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001413 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001414#endif
1415}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001416
1417TEST(time, difftime) {
1418 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001419 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001420}
Elliott Hughes2bd43162023-06-15 13:17:08 -07001421
1422TEST(time, tzfree_null) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001423#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001424 tzfree(nullptr);
1425#else
1426 GTEST_SKIP() << "glibc doesn't have timezone_t";
1427#endif
1428}
1429
1430TEST(time, localtime_rz) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001431#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001432 setenv("TZ", "America/Los_Angeles", 1);
1433 tzset();
1434
1435 auto AssertTmEq = [](const struct tm& rhs, int hour) {
1436 ASSERT_EQ(93, rhs.tm_year);
1437 ASSERT_EQ(0, rhs.tm_mon);
1438 ASSERT_EQ(1, rhs.tm_mday);
1439 ASSERT_EQ(hour, rhs.tm_hour);
1440 ASSERT_EQ(0, rhs.tm_min);
1441 ASSERT_EQ(0, rhs.tm_sec);
1442 };
1443
1444 const time_t t = 725875200;
1445
1446 // Spam localtime_r() while we use localtime_rz().
1447 std::atomic<bool> done = false;
1448 std::thread thread{[&] {
1449 while (!done) {
1450 struct tm tm {};
1451 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1452 AssertTmEq(tm, 0);
1453 }
1454 }};
1455
1456 struct tm tm;
1457
1458 timezone_t london{tzalloc("Europe/London")};
1459 tm = {};
1460 ASSERT_EQ(&tm, localtime_rz(london, &t, &tm));
1461 AssertTmEq(tm, 8);
1462
1463 timezone_t seoul{tzalloc("Asia/Seoul")};
1464 tm = {};
1465 ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm));
1466 AssertTmEq(tm, 17);
1467
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001468 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001469 tm = {};
1470 ASSERT_EQ(&tm, localtime_r(&t, &tm));
1471 ASSERT_EQ(0, tm.tm_hour);
1472 AssertTmEq(tm, 0);
1473
1474 done = true;
1475 thread.join();
1476
1477 tzfree(london);
1478 tzfree(seoul);
1479#else
1480 GTEST_SKIP() << "glibc doesn't have timezone_t";
1481#endif
1482}
1483
1484TEST(time, mktime_z) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001485#if defined(__BIONIC__)
Elliott Hughes2bd43162023-06-15 13:17:08 -07001486 setenv("TZ", "America/Los_Angeles", 1);
1487 tzset();
1488
1489 // Spam mktime() while we use mktime_z().
1490 std::atomic<bool> done = false;
1491 std::thread thread{[&done] {
1492 while (!done) {
1493 struct tm tm {
1494 .tm_year = 93, .tm_mday = 1
1495 };
1496 ASSERT_EQ(725875200, mktime(&tm));
1497 }
1498 }};
1499
1500 struct tm tm;
1501
1502 timezone_t london{tzalloc("Europe/London")};
1503 tm = {.tm_year = 93, .tm_mday = 1};
1504 ASSERT_EQ(725846400, mktime_z(london, &tm));
1505
1506 timezone_t seoul{tzalloc("Asia/Seoul")};
1507 tm = {.tm_year = 93, .tm_mday = 1};
1508 ASSERT_EQ(725814000, mktime_z(seoul, &tm));
1509
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001510 // Just check that mktime()'s timezone didn't change.
Elliott Hughes2bd43162023-06-15 13:17:08 -07001511 tm = {.tm_year = 93, .tm_mday = 1};
1512 ASSERT_EQ(725875200, mktime(&tm));
1513
1514 done = true;
1515 thread.join();
1516
1517 tzfree(london);
1518 tzfree(seoul);
1519#else
1520 GTEST_SKIP() << "glibc doesn't have timezone_t";
1521#endif
1522}
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001523
1524TEST(time, tzalloc_nullptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001525#if defined(__BIONIC__)
Elliott Hughes31fc69f2023-06-20 15:36:11 -07001526 // tzalloc(nullptr) returns the system timezone.
1527 timezone_t default_tz = tzalloc(nullptr);
1528 ASSERT_NE(nullptr, default_tz);
1529
1530 // Check that mktime_z() with the default timezone matches mktime().
1531 // This assumes that the system timezone doesn't change during the test,
1532 // but that should be unlikely, and we don't have much choice if we
1533 // want to write a test at all.
1534 // We unset $TZ before calling mktime() because mktime() honors $TZ.
1535 unsetenv("TZ");
1536 struct tm tm = {.tm_year = 93, .tm_mday = 1};
1537 time_t t = mktime(&tm);
1538 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1539
1540 // Check that changing $TZ doesn't affect the tzalloc() default in
1541 // the same way it would the mktime() default.
1542 setenv("TZ", "America/Los_Angeles", 1);
1543 tzset();
1544 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1545
1546 setenv("TZ", "Europe/London", 1);
1547 tzset();
1548 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1549
1550 setenv("TZ", "Asia/Seoul", 1);
1551 tzset();
1552 ASSERT_EQ(t, mktime_z(default_tz, &tm));
1553
1554 tzfree(default_tz);
1555#else
1556 GTEST_SKIP() << "glibc doesn't have timezone_t";
1557#endif
1558}
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001559
1560TEST(time, tzalloc_unique_ptr) {
Elliott Hughese7943f82023-09-28 08:20:20 -07001561#if defined(__BIONIC__)
Elliott Hughes5ea305b2023-06-22 20:53:00 +00001562 std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"),
1563 tzfree};
1564#else
1565 GTEST_SKIP() << "glibc doesn't have timezone_t";
1566#endif
1567}