blob: 3d745ea3140e23967abbf16a2668c0bf58e6fc81 [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>
Elliott Hughes625993d2014-07-15 16:53:13 -070023#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080024#include <sys/types.h>
25#include <sys/wait.h>
Yabin Cuid5c65272014-11-26 14:04:26 -080026#include <unistd.h>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070027
Yabin Cui95f1ee22015-01-13 19:53:15 -080028#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070029#include <chrono>
Elliott Hughese0175ca2013-03-14 14:38:08 -070030
Elliott Hughes71ba5892018-02-07 12:44:45 -080031#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080032#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070033
Elliott Hughes04303f52014-09-18 16:11:59 -070034#include "private/bionic_constants.h"
35
Elliott Hughesca3f8e42019-10-28 15:59:38 -070036using namespace std::chrono_literals;
37
Mark Salyzyn0b846e82017-12-20 08:56:18 -080038TEST(time, time) {
39 // Acquire time
40 time_t p1, t1 = time(&p1);
41 // valid?
42 ASSERT_NE(static_cast<time_t>(0), t1);
43 ASSERT_NE(static_cast<time_t>(-1), t1);
44 ASSERT_EQ(p1, t1);
45
46 // Acquire time one+ second later
47 usleep(1010000);
48 time_t p2, t2 = time(&p2);
49 // valid?
50 ASSERT_NE(static_cast<time_t>(0), t2);
51 ASSERT_NE(static_cast<time_t>(-1), t2);
52 ASSERT_EQ(p2, t2);
53
54 // Expect time progression
55 ASSERT_LT(p1, p2);
56 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
57
58 // Expect nullptr call to produce same results
59 ASSERT_LE(t2, time(nullptr));
60 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
61}
62
Elliott Hughesee178bf2013-07-12 11:25:20 -070063TEST(time, gmtime) {
64 time_t t = 0;
65 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070066 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070067 ASSERT_EQ(0, broken_down->tm_sec);
68 ASSERT_EQ(0, broken_down->tm_min);
69 ASSERT_EQ(0, broken_down->tm_hour);
70 ASSERT_EQ(1, broken_down->tm_mday);
71 ASSERT_EQ(0, broken_down->tm_mon);
72 ASSERT_EQ(1970, broken_down->tm_year + 1900);
73}
Elliott Hughes7843d442013-08-22 11:37:32 -070074
Elliott Hughes5a29d542017-12-07 16:05:57 -080075TEST(time, gmtime_r) {
76 struct tm tm = {};
77 time_t t = 0;
78 struct tm* broken_down = gmtime_r(&t, &tm);
79 ASSERT_EQ(broken_down, &tm);
80 ASSERT_EQ(0, broken_down->tm_sec);
81 ASSERT_EQ(0, broken_down->tm_min);
82 ASSERT_EQ(0, broken_down->tm_hour);
83 ASSERT_EQ(1, broken_down->tm_mday);
84 ASSERT_EQ(0, broken_down->tm_mon);
85 ASSERT_EQ(1970, broken_down->tm_year + 1900);
86}
87
Elliott Hughes329103d2014-04-25 16:55:04 -070088static void* gmtime_no_stack_overflow_14313703_fn(void*) {
89 const char* original_tz = getenv("TZ");
90 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
91 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
92 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070093 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -070094 setenv("TZ", original_tz, 1);
95 }
96 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070097 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -070098}
99
100TEST(time, gmtime_no_stack_overflow_14313703) {
101 // Is it safe to call tzload on a thread with a small stack?
102 // http://b/14313703
103 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800104 pthread_attr_t a;
105 ASSERT_EQ(0, pthread_attr_init(&a));
106 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700107
108 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800110 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700111}
112
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900113TEST(time, mktime_empty_TZ) {
114 // tzcode used to have a bug where it didn't reinitialize some internal state.
115
116 // Choose a time where DST is set.
117 struct tm t;
118 memset(&t, 0, sizeof(tm));
119 t.tm_year = 1980 - 1900;
120 t.tm_mon = 6;
121 t.tm_mday = 2;
122
123 setenv("TZ", "America/Los_Angeles", 1);
124 tzset();
125 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
126
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", "", 1); // Implies UTC.
133 tzset();
134 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
135}
136
Elliott Hughes7843d442013-08-22 11:37:32 -0700137TEST(time, mktime_10310929) {
138 struct tm t;
139 memset(&t, 0, sizeof(tm));
140 t.tm_year = 200;
141 t.tm_mon = 2;
142 t.tm_mday = 10;
143
Elliott Hughes0c401522013-10-18 16:21:54 -0700144#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700145 // 32-bit bionic has a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700146 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700147 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700148#else
149 // Everyone else should be using a signed 64-bit time_t.
150 ASSERT_GE(sizeof(time_t) * 8, 64U);
151
152 setenv("TZ", "America/Los_Angeles", 1);
153 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700154 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700155 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700156 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700157
158 setenv("TZ", "UTC", 1);
159 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700160 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700161 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700162 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700163#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800164}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800165
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700166TEST(time, mktime_EOVERFLOW) {
167 struct tm t;
168 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700169
170 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
171 t.tm_year = 2016 - 1900;
172
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700173 t.tm_mon = 2;
174 t.tm_mday = 10;
175
176 errno = 0;
177 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
178 ASSERT_EQ(0, errno);
179
Elliott Hughes47126ed2016-09-06 13:25:53 -0700180 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700181 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700182
183 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700184 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
185 ASSERT_EQ(EOVERFLOW, errno);
186}
187
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700188TEST(time, strftime) {
189 setenv("TZ", "UTC", 1);
190
191 struct tm t;
192 memset(&t, 0, sizeof(tm));
193 t.tm_year = 200;
194 t.tm_mon = 2;
195 t.tm_mday = 10;
196
197 char buf[64];
198
199 // Seconds since the epoch.
200#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
201 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
202 EXPECT_STREQ("4108320000", buf);
203#endif
204
205 // Date and time as text.
206 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
207 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
208}
209
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800210TEST(time, strftime_null_tm_zone) {
211 // Netflix on Nexus Player wouldn't start (http://b/25170306).
212 struct tm t;
213 memset(&t, 0, sizeof(tm));
214
215 char buf[64];
216
217 setenv("TZ", "America/Los_Angeles", 1);
218 tzset();
219
220 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
221 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
222 EXPECT_STREQ("<PST>", buf);
223
224#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
225 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
226 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
227 EXPECT_STREQ("<PDT>", buf);
228
229 t.tm_isdst = -123; // "and negative if the information is not available".
230 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
231 EXPECT_STREQ("<>", buf);
232#endif
233
234 setenv("TZ", "UTC", 1);
235 tzset();
236
237 t.tm_isdst = 0;
238 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
239 EXPECT_STREQ("<UTC>", buf);
240
241#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
242 t.tm_isdst = 1; // UTC has no DST.
243 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
244 EXPECT_STREQ("<>", buf);
245#endif
246}
247
Elliott Hughes0a610d02016-07-29 14:04:17 -0700248TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700250 locale_t old_locale = uselocale(cloc);
251
252 setenv("TZ", "UTC", 1);
253
254 struct tm t;
255 memset(&t, 0, sizeof(tm));
256 t.tm_year = 200;
257 t.tm_mon = 2;
258 t.tm_mday = 10;
259
260 // Date and time as text.
261 char buf[64];
262 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
263 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
264
265 uselocale(old_locale);
266 freelocale(cloc);
267}
268
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700269TEST(time, strptime) {
270 setenv("TZ", "UTC", 1);
271
272 struct tm t;
273 char buf[64];
274
275 memset(&t, 0, sizeof(t));
276 strptime("11:14", "%R", &t);
277 strftime(buf, sizeof(buf), "%H:%M", &t);
278 EXPECT_STREQ("11:14", buf);
279
280 memset(&t, 0, sizeof(t));
281 strptime("09:41:53", "%T", &t);
282 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
283 EXPECT_STREQ("09:41:53", buf);
284}
285
Elliott Hughes3376c232018-02-13 23:14:12 -0800286TEST(time, strptime_l) {
287 setenv("TZ", "UTC", 1);
288
289 struct tm t;
290 char buf[64];
291
292 memset(&t, 0, sizeof(t));
293 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
294 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
295 EXPECT_STREQ("11:14", buf);
296
297 memset(&t, 0, sizeof(t));
298 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
299 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
300 EXPECT_STREQ("09:41:53", buf);
301}
302
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700303TEST(time, strptime_F) {
304 setenv("TZ", "UTC", 1);
305
306 struct tm tm = {};
307 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
308 EXPECT_EQ(119, tm.tm_year);
309 EXPECT_EQ(2, tm.tm_mon);
310 EXPECT_EQ(26, tm.tm_mday);
311}
312
313TEST(time, strptime_P_p) {
314 setenv("TZ", "UTC", 1);
315
Elliott Hughes11678822019-03-27 08:56:49 -0700316 // For parsing, %P and %p are the same: case doesn't matter.
317
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700318 struct tm tm = {.tm_hour = 12};
319 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
320 EXPECT_EQ(0, tm.tm_hour);
321
322 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700323 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
324 EXPECT_EQ(0, tm.tm_hour);
325
326 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700327 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
328 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700329
330 tm = {.tm_hour = 12};
331 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
332 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700333}
334
335TEST(time, strptime_u) {
336 setenv("TZ", "UTC", 1);
337
338 struct tm tm = {};
339 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
340 EXPECT_EQ(2, tm.tm_wday);
341}
342
343TEST(time, strptime_v) {
344 setenv("TZ", "UTC", 1);
345
346 struct tm tm = {};
347 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
348 EXPECT_EQ(80, tm.tm_year);
349 EXPECT_EQ(2, tm.tm_mon);
350 EXPECT_EQ(26, tm.tm_mday);
351}
352
353TEST(time, strptime_V_G_g) {
354 setenv("TZ", "UTC", 1);
355
356 // %V (ISO-8601 week number), %G (year of week number, without century), and
357 // %g (year of week number) have no effect when parsed, and are supported
358 // solely so that it's possible for strptime(3) to parse everything that
359 // strftime(3) can output.
360 struct tm tm = {};
361 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
362 struct tm zero = {};
363 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
364}
365
Elliott Hughes4b558f52014-03-04 15:58:02 -0800366void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
367 itimerspec ts;
368 ts.it_value.tv_sec = value_s;
369 ts.it_value.tv_nsec = value_ns;
370 ts.it_interval.tv_sec = interval_s;
371 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700372 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800373}
374
375static void NoOpNotifyFunction(sigval_t) {
376}
377
378TEST(time, timer_create) {
379 sigevent_t se;
380 memset(&se, 0, sizeof(se));
381 se.sigev_notify = SIGEV_THREAD;
382 se.sigev_notify_function = NoOpNotifyFunction;
383 timer_t timer_id;
384 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
385
Elliott Hughes33697a02016-01-26 13:04:57 -0800386 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800387 ASSERT_NE(-1, pid) << strerror(errno);
388
389 if (pid == 0) {
390 // Timers are not inherited by the child.
391 ASSERT_EQ(-1, timer_delete(timer_id));
392 ASSERT_EQ(EINVAL, errno);
393 _exit(0);
394 }
395
Elliott Hughes33697a02016-01-26 13:04:57 -0800396 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800397
398 ASSERT_EQ(0, timer_delete(timer_id));
399}
400
Yabin Cui95f1ee22015-01-13 19:53:15 -0800401static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800402static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
403 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
404 ASSERT_EQ(SIGUSR1, signal_number);
405}
406
407TEST(time, timer_create_SIGEV_SIGNAL) {
408 sigevent_t se;
409 memset(&se, 0, sizeof(se));
410 se.sigev_notify = SIGEV_SIGNAL;
411 se.sigev_signo = SIGUSR1;
412
413 timer_t timer_id;
414 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
415
Yabin Cui95f1ee22015-01-13 19:53:15 -0800416 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800417 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
418
419 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
420
421 itimerspec ts;
422 ts.it_value.tv_sec = 0;
423 ts.it_value.tv_nsec = 1;
424 ts.it_interval.tv_sec = 0;
425 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700426 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800427
428 usleep(500000);
429 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
430}
431
432struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800433 private:
434 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800435 timer_t timer_id;
436 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700437 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800438
Elliott Hughes4b558f52014-03-04 15:58:02 -0800439 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700440 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800441 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700442 timer_valid = true;
443 }
444
Yabin Cui95f1ee22015-01-13 19:53:15 -0800445 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700446 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800447 memset(&se, 0, sizeof(se));
448 se.sigev_notify = SIGEV_THREAD;
449 se.sigev_notify_function = fn;
450 se.sigev_value.sival_ptr = this;
451 Create();
452 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700453 void DeleteTimer() {
454 ASSERT_TRUE(timer_valid);
455 ASSERT_EQ(0, timer_delete(timer_id));
456 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800457 }
458
459 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700460 if (timer_valid) {
461 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800462 }
463 }
464
Yabin Cui95f1ee22015-01-13 19:53:15 -0800465 int Value() const {
466 return value;
467 }
468
Christopher Ferris62d84b12014-10-20 19:09:19 -0700469 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
470 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
471 }
472
473 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800474 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700475 time_t start = time(nullptr);
476 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700477 }
478 return current_value != value;
479 }
480
Elliott Hughes4b558f52014-03-04 15:58:02 -0800481 static void CountNotifyFunction(sigval_t value) {
482 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
483 ++cd->value;
484 }
485
486 static void CountAndDisarmNotifyFunction(sigval_t value) {
487 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
488 ++cd->value;
489
490 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700491 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800492 }
493};
494
495TEST(time, timer_settime_0) {
496 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800497 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800498
Yabin Cuibf572d92015-08-11 11:23:16 -0700499 counter.SetTime(0, 500000000, 1, 0);
500 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800501
502 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800503 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800504}
505
506TEST(time, timer_settime_repeats) {
507 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800508 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800509
Christopher Ferris62d84b12014-10-20 19:09:19 -0700510 counter.SetTime(0, 1, 0, 10);
511 ASSERT_TRUE(counter.ValueUpdated());
512 ASSERT_TRUE(counter.ValueUpdated());
513 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800514 counter.DeleteTimer();
515 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
516 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800517}
518
Yabin Cui95f1ee22015-01-13 19:53:15 -0800519static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800520static void timer_create_NULL_signal_handler(int signal_number) {
521 ++timer_create_NULL_signal_handler_invocation_count;
522 ASSERT_EQ(SIGALRM, signal_number);
523}
524
525TEST(time, timer_create_NULL) {
526 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
527 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700528 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800529
Yabin Cui95f1ee22015-01-13 19:53:15 -0800530 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800531 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
532
533 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
534
535 SetTime(timer_id, 0, 1, 0, 0);
536 usleep(500000);
537
538 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
539}
540
541TEST(time, timer_create_EINVAL) {
542 clockid_t invalid_clock = 16;
543
544 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
545 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700546 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800547 ASSERT_EQ(EINVAL, errno);
548
549 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
550 sigevent_t se;
551 memset(&se, 0, sizeof(se));
552 se.sigev_notify = SIGEV_THREAD;
553 se.sigev_notify_function = NoOpNotifyFunction;
554 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
555 ASSERT_EQ(EINVAL, errno);
556}
557
Elliott Hughes4b558f52014-03-04 15:58:02 -0800558TEST(time, timer_create_multiple) {
559 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800560 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800561 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800562
Yabin Cui95f1ee22015-01-13 19:53:15 -0800563 ASSERT_EQ(0, counter1.Value());
564 ASSERT_EQ(0, counter2.Value());
565 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800566
Yabin Cui410c1ad2015-06-18 16:19:02 -0700567 counter2.SetTime(0, 500000000, 0, 0);
568 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800569
Yabin Cui95f1ee22015-01-13 19:53:15 -0800570 EXPECT_EQ(0, counter1.Value());
571 EXPECT_EQ(1, counter2.Value());
572 EXPECT_EQ(0, counter3.Value());
573}
574
575// Test to verify that disarming a repeatable timer disables the callbacks.
576TEST(time, timer_disarm_terminates) {
577 Counter counter(Counter::CountNotifyFunction);
578 ASSERT_EQ(0, counter.Value());
579
580 counter.SetTime(0, 1, 0, 1);
581 ASSERT_TRUE(counter.ValueUpdated());
582 ASSERT_TRUE(counter.ValueUpdated());
583 ASSERT_TRUE(counter.ValueUpdated());
584
585 counter.SetTime(0, 0, 0, 0);
586 // Add a sleep as the kernel may have pending events when the timer is disarmed.
587 usleep(500000);
588 int value = counter.Value();
589 usleep(500000);
590
591 // Verify the counter has not been incremented.
592 ASSERT_EQ(value, counter.Value());
593}
594
595// Test to verify that deleting a repeatable timer disables the callbacks.
596TEST(time, timer_delete_terminates) {
597 Counter counter(Counter::CountNotifyFunction);
598 ASSERT_EQ(0, counter.Value());
599
600 counter.SetTime(0, 1, 0, 1);
601 ASSERT_TRUE(counter.ValueUpdated());
602 ASSERT_TRUE(counter.ValueUpdated());
603 ASSERT_TRUE(counter.ValueUpdated());
604
605 counter.DeleteTimer();
606 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
607 usleep(500000);
608 int value = counter.Value();
609 usleep(500000);
610
611 // Verify the counter has not been incremented.
612 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800613}
Christopher Ferris753ad772014-03-20 20:47:45 -0700614
615struct TimerDeleteData {
616 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800617 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700618 volatile bool complete;
619};
620
621static void TimerDeleteCallback(sigval_t value) {
622 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
623
Elliott Hughes11859d42017-02-13 17:59:29 -0800624 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700625 timer_delete(tdd->timer_id);
626 tdd->complete = true;
627}
628
629TEST(time, timer_delete_from_timer_thread) {
630 TimerDeleteData tdd;
631 sigevent_t se;
632
633 memset(&se, 0, sizeof(se));
634 se.sigev_notify = SIGEV_THREAD;
635 se.sigev_notify_function = TimerDeleteCallback;
636 se.sigev_value.sival_ptr = &tdd;
637
638 tdd.complete = false;
639 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
640
641 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800642 ts.it_value.tv_sec = 1;
643 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700644 ts.it_interval.tv_sec = 0;
645 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700646 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700647
Yi Kong32bc0fc2018-08-02 17:31:13 -0700648 time_t cur_time = time(nullptr);
649 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700650 ASSERT_TRUE(tdd.complete);
651
652#if defined(__BIONIC__)
653 // Since bionic timers are implemented by creating a thread to handle the
654 // callback, verify that the thread actually completes.
655 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800656 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
657 ASSERT_EQ(-1, kill(tdd.tid, 0));
658 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700659#endif
660}
Elliott Hughes625993d2014-07-15 16:53:13 -0700661
662TEST(time, clock_gettime) {
663 // Try to ensure that our vdso clock_gettime is working.
664 timespec ts1;
665 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
666 timespec ts2;
667 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
668
669 // What's the difference between the two?
670 ts2.tv_sec -= ts1.tv_sec;
671 ts2.tv_nsec -= ts1.tv_nsec;
672 if (ts2.tv_nsec < 0) {
673 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700674 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700675 }
676
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800677 // To try to avoid flakiness we'll accept answers within 10,000,000ns (0.01s).
Elliott Hughes625993d2014-07-15 16:53:13 -0700678 ASSERT_EQ(0, ts2.tv_sec);
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800679 ASSERT_LT(ts2.tv_nsec, 10'000'000);
Elliott Hughes625993d2014-07-15 16:53:13 -0700680}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700681
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700682TEST(time, clock_gettime_CLOCK_REALTIME) {
683 timespec ts;
684 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
685}
686
687TEST(time, clock_gettime_CLOCK_MONOTONIC) {
688 timespec ts;
689 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
690}
691
692TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
693 timespec ts;
694 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
695}
696
697TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
698 timespec ts;
699 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
700}
701
702TEST(time, clock_gettime_CLOCK_BOOTTIME) {
703 timespec ts;
704 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
705}
706
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800707TEST(time, clock_gettime_unknown) {
708 errno = 0;
709 timespec ts;
710 ASSERT_EQ(-1, clock_gettime(-1, &ts));
711 ASSERT_EQ(EINVAL, errno);
712}
713
714TEST(time, clock_getres_CLOCK_REALTIME) {
715 timespec ts;
716 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
717 ASSERT_EQ(1, ts.tv_nsec);
718 ASSERT_EQ(0, ts.tv_sec);
719}
720
721TEST(time, clock_getres_CLOCK_MONOTONIC) {
722 timespec ts;
723 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
724 ASSERT_EQ(1, ts.tv_nsec);
725 ASSERT_EQ(0, ts.tv_sec);
726}
727
728TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
729 timespec ts;
730 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
731}
732
733TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
734 timespec ts;
735 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
736}
737
738TEST(time, clock_getres_CLOCK_BOOTTIME) {
739 timespec ts;
740 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
741 ASSERT_EQ(1, ts.tv_nsec);
742 ASSERT_EQ(0, ts.tv_sec);
743}
744
745TEST(time, clock_getres_unknown) {
746 errno = 0;
747 timespec ts = { -1, -1 };
748 ASSERT_EQ(-1, clock_getres(-1, &ts));
749 ASSERT_EQ(EINVAL, errno);
750 ASSERT_EQ(-1, ts.tv_nsec);
751 ASSERT_EQ(-1, ts.tv_sec);
752}
753
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700754TEST(time, clock) {
Elliott Hughes5ae16282019-06-20 14:35:49 -0700755 // clock(3) is hard to test, but a 1s sleep should cost less than 5ms.
Haruki Hasegawa18160252014-10-13 00:50:47 +0900756 clock_t t0 = clock();
757 sleep(1);
758 clock_t t1 = clock();
Elliott Hughes5ae16282019-06-20 14:35:49 -0700759 ASSERT_LT(t1 - t0, 5 * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900760}
761
Elliott Hughesb4413592017-11-29 18:17:06 -0800762static pid_t GetInvalidPid() {
763 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800764 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800765 fscanf(fp.get(), "%ld", &pid_max);
766 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800767}
768
Elliott Hughesb4413592017-11-29 18:17:06 -0800769TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800770 clockid_t clockid;
771 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800772 timespec ts;
773 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800774}
Yabin Cuid5c65272014-11-26 14:04:26 -0800775
Elliott Hughesb4413592017-11-29 18:17:06 -0800776TEST(time, clock_getcpuclockid_parent) {
777 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800778 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800779 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800780 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800781}
Yabin Cuid5c65272014-11-26 14:04:26 -0800782
Elliott Hughesb4413592017-11-29 18:17:06 -0800783TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800784 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
785 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800786 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800787 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800788 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
789 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
790 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
791 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
792 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
793 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800794 ASSERT_EQ(0, errno);
795}
796
Haruki Hasegawa18160252014-10-13 00:50:47 +0900797TEST(time, clock_settime) {
798 errno = 0;
799 timespec ts;
800 ASSERT_EQ(-1, clock_settime(-1, &ts));
801 ASSERT_EQ(EINVAL, errno);
802}
803
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700804TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900805 timespec in;
806 timespec out;
807 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700808}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700809
810TEST(time, clock_nanosleep_thread_cputime_id) {
811 timespec in;
812 in.tv_sec = 1;
813 in.tv_nsec = 0;
814 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
815}
Elliott Hughes12443702016-10-19 16:02:31 -0700816
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700817TEST(time, clock_nanosleep) {
818 auto t0 = std::chrono::steady_clock::now();
819 const timespec ts = {.tv_nsec = 5000000};
820 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
821 auto t1 = std::chrono::steady_clock::now();
822 ASSERT_GE(t1-t0, 5000000ns);
823}
824
825TEST(time, nanosleep) {
826 auto t0 = std::chrono::steady_clock::now();
827 const timespec ts = {.tv_nsec = 5000000};
828 ASSERT_EQ(0, nanosleep(&ts, nullptr));
829 auto t1 = std::chrono::steady_clock::now();
830 ASSERT_GE(t1-t0, 5000000ns);
831}
832
833TEST(time, nanosleep_EINVAL) {
834 timespec ts = {.tv_sec = -1};
835 errno = 0;
836 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
837 ASSERT_EQ(EINVAL, errno);
838}
839
Elliott Hughes12443702016-10-19 16:02:31 -0700840TEST(time, bug_31938693) {
841 // User-visible symptoms in N:
842 // http://b/31938693
843 // https://code.google.com/p/android/issues/detail?id=225132
844
845 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
846 // http://b/31848040
847
848 // This isn't a great test, because very few time zones were actually affected, and there's
849 // no real logic to which ones were affected: it was just a coincidence of the data that came
850 // after them in the tzdata file.
851
852 time_t t = 1475619727;
853 struct tm tm;
854
855 setenv("TZ", "America/Los_Angeles", 1);
856 tzset();
857 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
858 EXPECT_EQ(15, tm.tm_hour);
859
860 setenv("TZ", "Europe/London", 1);
861 tzset();
862 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
863 EXPECT_EQ(23, tm.tm_hour);
864
865 setenv("TZ", "America/Atka", 1);
866 tzset();
867 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
868 EXPECT_EQ(13, tm.tm_hour);
869
870 setenv("TZ", "Pacific/Apia", 1);
871 tzset();
872 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
873 EXPECT_EQ(12, tm.tm_hour);
874
875 setenv("TZ", "Pacific/Honolulu", 1);
876 tzset();
877 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
878 EXPECT_EQ(12, tm.tm_hour);
879
880 setenv("TZ", "Asia/Magadan", 1);
881 tzset();
882 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
883 EXPECT_EQ(9, tm.tm_hour);
884}
Elliott Hughesea877162017-01-11 14:34:16 -0800885
886TEST(time, bug_31339449) {
887 // POSIX says localtime acts as if it calls tzset.
888 // tzset does two things:
889 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
890 // 2. it sets the global `tzname`.
891 // POSIX says localtime_r need not set `tzname` (2).
892 // Q: should localtime_r set the time zone (1)?
893 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
894
895 // Pick a time, any time...
896 time_t t = 1475619727;
897
898 // Call tzset with a specific timezone.
899 setenv("TZ", "America/Atka", 1);
900 tzset();
901
902 // If we change the timezone and call localtime, localtime should use the new timezone.
903 setenv("TZ", "America/Los_Angeles", 1);
904 struct tm* tm_p = localtime(&t);
905 EXPECT_EQ(15, tm_p->tm_hour);
906
907 // Reset the timezone back.
908 setenv("TZ", "America/Atka", 1);
909 tzset();
910
911#if defined(__BIONIC__)
912 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
913 setenv("TZ", "America/Los_Angeles", 1);
914 struct tm tm = {};
915 localtime_r(&t, &tm);
916 EXPECT_EQ(15, tm.tm_hour);
917#else
918 // The BSDs agree with us, but glibc gets this wrong.
919#endif
920}
Elliott Hughes5a29d542017-12-07 16:05:57 -0800921
922TEST(time, asctime) {
923 const struct tm tm = {};
924 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
925}
926
927TEST(time, asctime_r) {
928 const struct tm tm = {};
929 char buf[256];
930 ASSERT_EQ(buf, asctime_r(&tm, buf));
931 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
932}
933
934TEST(time, ctime) {
935 setenv("TZ", "UTC", 1);
936 const time_t t = 0;
937 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
938}
939
940TEST(time, ctime_r) {
941 setenv("TZ", "UTC", 1);
942 const time_t t = 0;
943 char buf[256];
944 ASSERT_EQ(buf, ctime_r(&t, buf));
945 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
946}
Elliott Hughes81baaf22018-02-28 15:22:48 -0800947
948// https://issuetracker.google.com/37128336
949TEST(time, strftime_strptime_s) {
950 char buf[32];
951 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
952
953 setenv("TZ", "America/Los_Angeles", 1);
954 strftime(buf, sizeof(buf), "<%s>", &tm0);
955 EXPECT_STREQ("<378720000>", buf);
956
957 setenv("TZ", "UTC", 1);
958 strftime(buf, sizeof(buf), "<%s>", &tm0);
959 EXPECT_STREQ("<378691200>", buf);
960
961 struct tm tm;
962
963 setenv("TZ", "America/Los_Angeles", 1);
964 tzset();
965 memset(&tm, 0xff, sizeof(tm));
966 char* p = strptime("378720000x", "%s", &tm);
967 ASSERT_EQ('x', *p);
968 EXPECT_EQ(0, tm.tm_sec);
969 EXPECT_EQ(0, tm.tm_min);
970 EXPECT_EQ(0, tm.tm_hour);
971 EXPECT_EQ(1, tm.tm_mday);
972 EXPECT_EQ(0, tm.tm_mon);
973 EXPECT_EQ(82, tm.tm_year);
974 EXPECT_EQ(5, tm.tm_wday);
975 EXPECT_EQ(0, tm.tm_yday);
976 EXPECT_EQ(0, tm.tm_isdst);
977
978 setenv("TZ", "UTC", 1);
979 tzset();
980 memset(&tm, 0xff, sizeof(tm));
981 p = strptime("378691200x", "%s", &tm);
982 ASSERT_EQ('x', *p);
983 EXPECT_EQ(0, tm.tm_sec);
984 EXPECT_EQ(0, tm.tm_min);
985 EXPECT_EQ(0, tm.tm_hour);
986 EXPECT_EQ(1, tm.tm_mday);
987 EXPECT_EQ(0, tm.tm_mon);
988 EXPECT_EQ(82, tm.tm_year);
989 EXPECT_EQ(5, tm.tm_wday);
990 EXPECT_EQ(0, tm.tm_yday);
991 EXPECT_EQ(0, tm.tm_isdst);
992}
993
994TEST(time, strptime_s_nothing) {
995 struct tm tm;
996 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
997}
Elliott Hughesf98d87b2018-07-17 13:21:05 -0700998
999TEST(time, timespec_get) {
1000#if __BIONIC__
1001 timespec ts = {};
1002 ASSERT_EQ(0, timespec_get(&ts, 123));
1003 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1004#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001005 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001006#endif
1007}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001008
1009TEST(time, difftime) {
1010 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001011 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001012}