blob: 5b762880f6fe1623fd5ba6272d20f54693708b67 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes3503ce22013-11-05 13:28:36 -080028
Elliott Hughes52541ee2023-04-24 17:04:49 -070029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030
Elliott Hughesd192dbe2023-05-25 11:17:56 -070031/**
32 * @file time.h
33 * @brief Clock and timer functionality.
34 */
35
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <sys/cdefs.h>
37#include <sys/time.h>
Dan Albertdfb5ce42014-07-09 22:51:34 +000038#include <xlocale.h>
David 'Digit' Turnerc1b44ec2012-10-17 19:10:11 +020039
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040__BEGIN_DECLS
41
Elliott Hughes2bd43162023-06-15 13:17:08 -070042/* If we just use void* in the typedef, the compiler exposes that in error messages. */
43struct __timezone_t;
44
45/** The `timezone_t` type that represents a time zone. */
46typedef struct __timezone_t* timezone_t;
47
Elliott Hughesd192dbe2023-05-25 11:17:56 -070048/** Divisor to compute seconds from the result of a call to clock(). */
Elliott Hughes3503ce22013-11-05 13:28:36 -080049#define CLOCKS_PER_SEC 1000000
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
Elliott Hughesd192dbe2023-05-25 11:17:56 -070051/**
52 * The name of the current time zone's non-daylight savings (`tzname[0]`) and
53 * daylight savings (`tzname[1]`) variants. See tzset().
54 */
zijunzhaoe6202662023-01-03 23:32:18 +000055extern char* _Nonnull tzname[];
Elliott Hughesd192dbe2023-05-25 11:17:56 -070056
57/** Whether the current time zone ever uses daylight savings time. See tzset(). */
Elliott Hughesf47514d2016-07-19 13:56:46 -070058extern int daylight;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070059
60/** The difference in seconds between UTC and the current time zone. See tzset(). */
Elliott Hughesf47514d2016-07-19 13:56:46 -070061extern long int timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
Elliott Hughes61fb3fc2013-11-07 12:28:46 -080063struct sigevent;
64
Elliott Hughesd192dbe2023-05-25 11:17:56 -070065/**
66 * A "broken-down" time, useful for parsing/formatting times for human consumption.
67 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068struct tm {
Elliott Hughesd192dbe2023-05-25 11:17:56 -070069 /** Seconds, 0-60. (60 is a leap second.) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080070 int tm_sec;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070071 /** Minutes, 0-59. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080072 int tm_min;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070073 /** Hours, 0-23. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080074 int tm_hour;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070075 /** Day of month, 1-31. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080076 int tm_mday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070077 /** Month of year, 0-11. (Not 1-12!) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080078 int tm_mon;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070079 /** Years since 1900. (So 2023 is 123, not 2023!) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080080 int tm_year;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070081 /** Day of week, 0-6. (Sunday is 0, Saturday is 6.) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080082 int tm_wday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070083 /** Day of year, 0-365. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080084 int tm_yday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070085 /** Daylight savings flag, positive for DST in effect, 0 for DST not in effect, and -1 for unknown. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080086 int tm_isdst;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070087 /** Offset from UTC (GMT) in seconds for this time. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080088 long int tm_gmtoff;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070089 /** Name of the time zone for this time. */
zijunzhaoe6202662023-01-03 23:32:18 +000090 const char* _Nullable tm_zone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091};
92
Elliott Hughesd192dbe2023-05-25 11:17:56 -070093/** Alternative name for `tm_zone` in `struct tm`. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080094#define TM_ZONE tm_zone
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughesd192dbe2023-05-25 11:17:56 -070096/**
97 * [time(2)](http://man7.org/linux/man-pages/man2/time.2.html) returns
98 * the number of seconds since the Unix epoch (1970-01-01 00:00:00 +0000).
99 *
100 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
101 */
zijunzhaoe6202662023-01-03 23:32:18 +0000102time_t time(time_t* _Nullable __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700103
104/**
105 * [nanosleep(2)](http://man7.org/linux/man-pages/man2/nanosleep.2.html) sleeps
106 * for at least the given time (or until a signal arrives).
107 *
108 * Returns 0 on success, and returns -1 and sets `errno` on failure. If the sleep
109 * was interrupted by a signal, `errno` will be `EINTR` and `remainder` will be
110 * the amount of time remaining.
111 */
zijunzhaoe6202662023-01-03 23:32:18 +0000112int nanosleep(const struct timespec* _Nonnull __request, struct timespec* _Nullable __remainder);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700114/**
115 * [asctime(3)](http://man7.org/linux/man-pages/man3/asctime.3p.html) formats
116 * the time `tm` as a string.
117 *
118 * Returns a pointer to a string on success, and returns NULL on failure.
119 *
120 * That string will be overwritten by later calls to this function.
121 *
122 * New code should prefer strftime().
123 */
zijunzhaoe6202662023-01-03 23:32:18 +0000124char* _Nullable asctime(const struct tm* _Nonnull __tm);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700125
126/**
127 * [asctime_r(3)](http://man7.org/linux/man-pages/man3/asctime_r.3p.html) formats
128 * the time `tm` as a string in the given buffer `buf`.
129 *
130 * Returns a pointer to a string on success, and returns NULL on failure.
131 *
132 * New code should prefer strftime().
133 */
zijunzhaoe6202662023-01-03 23:32:18 +0000134char* _Nullable asctime_r(const struct tm* _Nonnull __tm, char* _Nonnull __buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700136/**
137 * [difftime(3)](http://man7.org/linux/man-pages/man3/difftime.3.html) returns
138 * the difference between two times.
139 *
140 * Returns the difference in seconds.
141 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000142double difftime(time_t __lhs, time_t __rhs);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700143
144/**
145 * [mktime(3)](http://man7.org/linux/man-pages/man3/mktime.3p.html) converts
146 * broken-down time `tm` into the number of seconds since the Unix epoch.
147 *
Elliott Hughes2bd43162023-06-15 13:17:08 -0700148 * See tzset() for details of how the time zone is set, and mktime_rz()
149 * for an alternative.
150 *
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700151 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
152 */
zijunzhaoe6202662023-01-03 23:32:18 +0000153time_t mktime(struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700155/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700156 * mktime_z(3) converts broken-down time `tm` into the number of seconds
157 * since the Unix epoch, assuming the given time zone.
158 *
159 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
160 *
161 * Available since API level 35.
162 */
163time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
164
165/**
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700166 * [localtime(3)](http://man7.org/linux/man-pages/man3/localtime.3p.html) converts
167 * the number of seconds since the Unix epoch in `t` to a broken-down time, taking
168 * the device's timezone into account.
169 *
170 * That broken-down time will be overwritten by later calls to this function.
171 *
172 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
173 */
zijunzhaoe6202662023-01-03 23:32:18 +0000174struct tm* _Nullable localtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700175
176/**
177 * [localtime_r(3)](http://man7.org/linux/man-pages/man3/localtime_r.3p.html) converts
178 * the number of seconds since the Unix epoch in `t` to a broken-down time.
179 * That broken-down time will be written to the given struct `tm`.
180 *
Elliott Hughes2bd43162023-06-15 13:17:08 -0700181 * See tzset() for details of how the time zone is set, and localtime_rz()
182 * for an alternative.
183 *
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700184 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
185 */
zijunzhaoe6202662023-01-03 23:32:18 +0000186struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700188/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700189 * localtime_rz(3) converts the number of seconds since the Unix epoch in
190 * `t` to a broken-down time, assuming the given time zone. That broken-down
191 * time will be written to the given struct `tm`.
192 *
193 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
194 *
195 * Available since API level 35.
196 */
197struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
198
199/**
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700200 * Inverse of localtime().
201 */
202time_t timelocal(struct tm* _Nonnull __tm);
203
204/**
205 * [gmtime(3)](http://man7.org/linux/man-pages/man3/gmtime.3p.html) converts
206 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
207 * UTC (historically also known as GMT).
208 *
209 * That broken-down time will be overwritten by later calls to this function.
210 *
211 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
212 */
zijunzhaoe6202662023-01-03 23:32:18 +0000213struct tm* _Nullable gmtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700214
215/**
216 * [gmtime_r(3)](http://man7.org/linux/man-pages/man3/gmtime_r.3p.html) converts
217 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
218 * UTC (historically also known as GMT).
219 *
220 * That broken-down time will be written to the provided struct `tm`.
221 *
222 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
223 */
zijunzhaoe6202662023-01-03 23:32:18 +0000224struct tm* _Nullable gmtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700226/**
227 * Inverse of gmtime().
228 */
229time_t timegm(struct tm* _Nonnull __tm);
230
231/**
232 * [strptime(3)](http://man7.org/linux/man-pages/man3/strptime.3.html) parses
233 * a string `s` assuming format `fmt` into broken-down time `tm`.
234 *
235 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
236 */
zijunzhaoe6202662023-01-03 23:32:18 +0000237char* _Nullable strptime(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm) __strftimelike(2);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700238
239/**
240 * Equivalent to strptime() on Android where only C/POSIX locales are available.
241 */
zijunzhaoe6202662023-01-03 23:32:18 +0000242char* _Nullable strptime_l(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm, locale_t _Nonnull __l) __strftimelike(2) __INTRODUCED_IN(28);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700243
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700244/**
245 * [strftime(3)](http://man7.org/linux/man-pages/man3/strftime.3.html) formats
246 * a broken-down time `tm` into the buffer `buf` using format `fmt`.
247 *
248 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
249 */
zijunzhaoe6202662023-01-03 23:32:18 +0000250size_t strftime(char* _Nonnull __buf, size_t __n, const char* _Nonnull __fmt, const struct tm* _Nullable __tm) __strftimelike(3);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700251
252/**
253 * Equivalent to strftime() on Android where only C/POSIX locales are available.
254 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700255size_t strftime_l(char* _Nonnull __buf, size_t __n, const char* _Nonnull __fmt, const struct tm* _Nullable __tm, locale_t _Nonnull __l) __strftimelike(3);
zijunzhaoe6202662023-01-03 23:32:18 +0000256
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700257/**
258 * [ctime(3)](http://man7.org/linux/man-pages/man3/ctime.3p.html) formats
259 * the time `tm` as a string.
260 *
261 * Returns a pointer to a string on success, and returns NULL on failure.
262 *
263 * That string will be overwritten by later calls to this function.
264 *
265 * New code should prefer strftime().
266 */
zijunzhaoe6202662023-01-03 23:32:18 +0000267char* _Nullable ctime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700268
269/**
270 * [ctime_r(3)](http://man7.org/linux/man-pages/man3/ctime.3p.html) formats
271 * the time `tm` as a string in the given buffer `buf`.
272 *
273 * Returns a pointer to a string on success, and returns NULL on failure.
274 *
275 * New code should prefer strftime().
276 */
zijunzhaoe6202662023-01-03 23:32:18 +0000277char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700279/**
280 * [tzset(3)](http://man7.org/linux/man-pages/man3/tzset.3.html) tells
281 * libc that the time zone has changed.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700282 *
283 * Android looks at both the system property `persist.sys.timezone` and the
284 * environment variable `TZ`. The former is the device's current time zone
285 * as shown in Settings, while the latter is usually unset but can be used
286 * to override the global setting. This is a bad idea outside of unit tests
287 * or single-threaded programs because it's inherently thread-unsafe.
288 * See tzalloc(), localtime_rz(), mktime_z(), and tzfree() for an
289 * alternative.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700290 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700291void tzset(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700293/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700294 * tzalloc(3) allocates a time zone corresponding to the given Olson id.
295 *
296 * Returns a time zone object on success, and returns NULL and sets `errno` on failure.
297 *
298 * Available since API level 35.
299 */
300timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35);
301
302/**
303 * tzfree(3) frees a time zone object returned by tzalloc().
304 *
305 * Available since API level 35.
306 */
307void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35);
308
309/**
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700310 * [clock(3)](http://man7.org/linux/man-pages/man3/clock.3.html)
311 * returns an approximation of CPU time used, equivalent to
312 * `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)` but with more confusing
313 * units. Use `CLOCKS_PER_SEC` to convert the result to seconds.
314 *
315 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
316 *
317 * New code should prefer `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)`.
318 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700319clock_t clock(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700321/**
322 * [clock_getcpuclockid(3)](http://man7.org/linux/man-pages/man3/clock_getcpuclockid.3.html)
323 * gets the clock id of the cpu-time clock for the given `pid`.
324 *
325 * Returns 0 on success, and returns -1 and returns an error number on failure.
326 */
zijunzhaoe6202662023-01-03 23:32:18 +0000327int clock_getcpuclockid(pid_t __pid, clockid_t* _Nonnull __clock) __INTRODUCED_IN(23);
Yabin Cuid5c65272014-11-26 14:04:26 -0800328
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700329/**
330 * [clock_getres(2)](http://man7.org/linux/man-pages/man2/clock_getres.2.html)
331 * gets the resolution of the given clock.
332 *
333 * Returns 0 on success, and returns -1 and returns an error number on failure.
334 */
zijunzhaoe6202662023-01-03 23:32:18 +0000335int clock_getres(clockid_t __clock, struct timespec* _Nullable __resolution);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700336
337/**
338 * [clock_gettime(2)](http://man7.org/linux/man-pages/man2/clock_gettime.2.html)
339 * gets the time according to the given clock.
340 *
341 * Returns 0 on success, and returns -1 and returns an error number on failure.
342 */
zijunzhaoe6202662023-01-03 23:32:18 +0000343int clock_gettime(clockid_t __clock, struct timespec* _Nonnull __ts);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700344
345/**
346 * [clock_nanosleep(2)](http://man7.org/linux/man-pages/man2/clock_nanosleep.2.html)
347 * sleeps for the given time as measured by the given clock.
348 *
349 * Returns 0 on success, and returns -1 and returns an error number on failure.
350 * If the sleep was interrupted by a signal, the return value will be `EINTR`
351 * and `remainder` will be the amount of time remaining.
352 */
zijunzhaoe6202662023-01-03 23:32:18 +0000353int clock_nanosleep(clockid_t __clock, int __flags, const struct timespec* _Nonnull __request, struct timespec* _Nullable __remainder);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700354
355/**
356 * [clock_settime(2)](http://man7.org/linux/man-pages/man2/clock_settime.2.html)
357 * sets the time for the given clock.
358 *
359 * Returns 0 on success, and returns -1 and returns an error number on failure.
360 */
zijunzhaoe6202662023-01-03 23:32:18 +0000361int clock_settime(clockid_t __clock, const struct timespec* _Nonnull __ts);
362
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700363/**
364 * [timer_create(2)](http://man7.org/linux/man-pages/man2/timer_create.2.html)
365 * creates a POSIX timer.
366 *
367 * Returns 0 on success, and returns -1 and sets `errno` on failure.
368 */
zijunzhaoe6202662023-01-03 23:32:18 +0000369int timer_create(clockid_t __clock, struct sigevent* _Nullable __event, timer_t _Nonnull * _Nonnull __timer_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800370
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700371/**
372 * [timer_delete(2)](http://man7.org/linux/man-pages/man2/timer_delete.2.html)
373 * destroys a POSIX timer.
374 *
375 * Returns 0 on success, and returns -1 and sets `errno` on failure.
376 */
377int timer_delete(timer_t _Nonnull __timer);
378
379/**
380 * [timer_settime(2)](http://man7.org/linux/man-pages/man2/timer_settime.2.html)
381 * starts or stops a POSIX timer.
382 *
383 * Returns 0 on success, and returns -1 and sets `errno` on failure.
384 */
385int timer_settime(timer_t _Nonnull __timer, int __flags, const struct itimerspec* _Nonnull __new_value, struct itimerspec* _Nullable __old_value);
386
387/**
388 * [timer_gettime(2)](http://man7.org/linux/man-pages/man2/timer_gettime.2.html)
389 * gets the time until the given timer next fires.
390 *
391 * Returns 0 on success, and returns -1 and sets `errno` on failure.
392 */
393int timer_gettime(timer_t _Nonnull _timer, struct itimerspec* _Nonnull __ts);
394
395/**
396 * [timer_getoverrun(2)](http://man7.org/linux/man-pages/man2/timer_getoverrun.2.html)
397 * gets the overrun count (the number of times the timer should have fired, but
398 * didn't) for the last time the timer fired.
399 *
400 * Returns the overrun count on success, and returns -1 and sets `errno` on failure.
401 */
402int timer_getoverrun(timer_t _Nonnull __timer);
David 'Digit' Turner6481b912010-12-06 12:23:16 +0100403
Elliott Hughes52541ee2023-04-24 17:04:49 -0700404/**
405 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_REALTIME.
406 *
407 * Available since API level 29.
408 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700409#define TIME_UTC (CLOCK_REALTIME+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700410
411/**
412 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_MONOTONIC.
413 *
414 * Available since API level 35.
415 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700416#define TIME_MONOTONIC (CLOCK_MONOTONIC+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700417
418/**
419 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_PROCESS_CPUTIME_ID.
420 *
421 * Available since API level 35.
422 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700423#define TIME_ACTIVE (CLOCK_PROCESS_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700424
425/**
426 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_THREAD_CPUTIME_ID.
427 *
428 * Available since API level 35.
429 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700430#define TIME_THREAD_ACTIVE (CLOCK_THREAD_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700431
432/**
433 * timespec_get(3) is equivalent to clock_gettime() for the clock corresponding to the given base.
434 *
435 * Returns the base on success and returns 0 on failure.
436 *
437 * Available since API level 29 for TIME_UTC; other bases arrived later.
438 * Code for Android should prefer clock_gettime().
439 */
zijunzhaoe6202662023-01-03 23:32:18 +0000440int timespec_get(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(29);
Elliott Hughesf98d87b2018-07-17 13:21:05 -0700441
Elliott Hughes52541ee2023-04-24 17:04:49 -0700442/**
443 * timespec_getres(3) is equivalent to clock_getres() for the clock corresponding to the given base.
444 *
445 * Returns the base on success and returns 0 on failure.
446 *
447 * Available since API level 35.
448 * Code for Android should prefer clock_gettime().
449 */
450int timespec_getres(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(35);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451
Elliott Hughes52541ee2023-04-24 17:04:49 -0700452__END_DECLS