blob: e9d656994ff8e197122d0c3771422caeac2ea018 [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
Elliott Hughes5ea305b2023-06-22 20:53:00 +000045/**
46 * The `timezone_t` type that represents a timezone.
47 *
48 * To use this with std::unique_ptr you'll want something like
49 * `std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};`
50 * to remove the pointer.
51 */
Elliott Hughes2bd43162023-06-15 13:17:08 -070052typedef struct __timezone_t* timezone_t;
53
Elliott Hughesd192dbe2023-05-25 11:17:56 -070054/** Divisor to compute seconds from the result of a call to clock(). */
Elliott Hughes3503ce22013-11-05 13:28:36 -080055#define CLOCKS_PER_SEC 1000000
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Elliott Hughesd192dbe2023-05-25 11:17:56 -070057/**
Elliott Hughes31fc69f2023-06-20 15:36:11 -070058 * The name of the current timezone's non-daylight savings (`tzname[0]`) and
Elliott Hughesd192dbe2023-05-25 11:17:56 -070059 * daylight savings (`tzname[1]`) variants. See tzset().
60 */
zijunzhaoe6202662023-01-03 23:32:18 +000061extern char* _Nonnull tzname[];
Elliott Hughesd192dbe2023-05-25 11:17:56 -070062
Elliott Hughes31fc69f2023-06-20 15:36:11 -070063/** Whether the current timezone ever uses daylight savings time. See tzset(). */
Elliott Hughesf47514d2016-07-19 13:56:46 -070064extern int daylight;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070065
Elliott Hughes31fc69f2023-06-20 15:36:11 -070066/** The difference in seconds between UTC and the current timezone. See tzset(). */
Elliott Hughesf47514d2016-07-19 13:56:46 -070067extern long int timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Elliott Hughes61fb3fc2013-11-07 12:28:46 -080069struct sigevent;
70
Elliott Hughesd192dbe2023-05-25 11:17:56 -070071/**
72 * A "broken-down" time, useful for parsing/formatting times for human consumption.
73 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074struct tm {
Elliott Hughesd192dbe2023-05-25 11:17:56 -070075 /** Seconds, 0-60. (60 is a leap second.) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080076 int tm_sec;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070077 /** Minutes, 0-59. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080078 int tm_min;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070079 /** Hours, 0-23. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080080 int tm_hour;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070081 /** Day of month, 1-31. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080082 int tm_mday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070083 /** Month of year, 0-11. (Not 1-12!) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080084 int tm_mon;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070085 /** Years since 1900. (So 2023 is 123, not 2023!) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080086 int tm_year;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070087 /** Day of week, 0-6. (Sunday is 0, Saturday is 6.) */
Elliott Hughes3503ce22013-11-05 13:28:36 -080088 int tm_wday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070089 /** Day of year, 0-365. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080090 int tm_yday;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070091 /** 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 -080092 int tm_isdst;
Elliott Hughesd192dbe2023-05-25 11:17:56 -070093 /** Offset from UTC (GMT) in seconds for this time. */
Elliott Hughes3503ce22013-11-05 13:28:36 -080094 long int tm_gmtoff;
Elliott Hughes31fc69f2023-06-20 15:36:11 -070095 /** Name of the timezone for this time. */
zijunzhaoe6202662023-01-03 23:32:18 +000096 const char* _Nullable tm_zone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097};
98
Elliott Hughesd192dbe2023-05-25 11:17:56 -070099/** Alternative name for `tm_zone` in `struct tm`. */
Elliott Hughes3503ce22013-11-05 13:28:36 -0800100#define TM_ZONE tm_zone
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700102/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000103 * [time(2)](https://man7.org/linux/man-pages/man2/time.2.html) returns
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700104 * the number of seconds since the Unix epoch (1970-01-01 00:00:00 +0000).
105 *
106 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
107 */
zijunzhaoe6202662023-01-03 23:32:18 +0000108time_t time(time_t* _Nullable __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700109
110/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000111 * [nanosleep(2)](https://man7.org/linux/man-pages/man2/nanosleep.2.html) sleeps
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700112 * for at least the given time (or until a signal arrives).
113 *
114 * Returns 0 on success, and returns -1 and sets `errno` on failure. If the sleep
115 * was interrupted by a signal, `errno` will be `EINTR` and `remainder` will be
116 * the amount of time remaining.
117 */
Elliott Hughes77add1b2023-11-01 00:34:47 +0000118int nanosleep(const struct timespec* _Nonnull __duration, struct timespec* _Nullable __remainder);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700120/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000121 * [asctime(3)](https://man7.org/linux/man-pages/man3/asctime.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700122 * the time `tm` as a string.
123 *
124 * Returns a pointer to a string on success, and returns NULL on failure.
125 *
126 * That string will be overwritten by later calls to this function.
127 *
128 * New code should prefer strftime().
129 */
zijunzhaoe6202662023-01-03 23:32:18 +0000130char* _Nullable asctime(const struct tm* _Nonnull __tm);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700131
132/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000133 * [asctime_r(3)](https://man7.org/linux/man-pages/man3/asctime_r.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700134 * the time `tm` as a string in the given buffer `buf`.
135 *
136 * Returns a pointer to a string on success, and returns NULL on failure.
137 *
138 * New code should prefer strftime().
139 */
zijunzhaoe6202662023-01-03 23:32:18 +0000140char* _Nullable asctime_r(const struct tm* _Nonnull __tm, char* _Nonnull __buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700142/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000143 * [difftime(3)](https://man7.org/linux/man-pages/man3/difftime.3.html) returns
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700144 * the difference between two times.
145 *
146 * Returns the difference in seconds.
147 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000148double difftime(time_t __lhs, time_t __rhs);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700149
150/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000151 * [mktime(3)](https://man7.org/linux/man-pages/man3/mktime.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700152 * broken-down time `tm` into the number of seconds since the Unix epoch.
153 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700154 * See tzset() for details of how the timezone is set, and mktime_rz()
Elliott Hughes2bd43162023-06-15 13:17:08 -0700155 * for an alternative.
156 *
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700157 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
158 */
zijunzhaoe6202662023-01-03 23:32:18 +0000159time_t mktime(struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700161/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700162 * mktime_z(3) converts broken-down time `tm` into the number of seconds
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700163 * since the Unix epoch, assuming the given timezone.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700164 *
165 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
166 *
167 * Available since API level 35.
168 */
169time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
170
171/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000172 * [localtime(3)](https://man7.org/linux/man-pages/man3/localtime.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700173 * the number of seconds since the Unix epoch in `t` to a broken-down time, taking
174 * the device's timezone into account.
175 *
176 * That broken-down time will be overwritten by later calls to this function.
177 *
178 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
179 */
zijunzhaoe6202662023-01-03 23:32:18 +0000180struct tm* _Nullable localtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700181
182/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000183 * [localtime_r(3)](https://man7.org/linux/man-pages/man3/localtime_r.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700184 * the number of seconds since the Unix epoch in `t` to a broken-down time.
185 * That broken-down time will be written to the given struct `tm`.
186 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700187 * See tzset() for details of how the timezone is set, and localtime_rz()
Elliott Hughes2bd43162023-06-15 13:17:08 -0700188 * for an alternative.
189 *
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700190 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
191 */
zijunzhaoe6202662023-01-03 23:32:18 +0000192struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700194/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700195 * localtime_rz(3) converts the number of seconds since the Unix epoch in
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700196 * `t` to a broken-down time, assuming the given timezone. That broken-down
Elliott Hughes2bd43162023-06-15 13:17:08 -0700197 * time will be written to the given struct `tm`.
198 *
199 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
200 *
201 * Available since API level 35.
202 */
203struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
204
205/**
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700206 * Inverse of localtime().
207 */
208time_t timelocal(struct tm* _Nonnull __tm);
209
210/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000211 * [gmtime(3)](https://man7.org/linux/man-pages/man3/gmtime.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700212 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
213 * UTC (historically also known as GMT).
214 *
215 * That broken-down time will be overwritten by later calls to this function.
216 *
217 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
218 */
zijunzhaoe6202662023-01-03 23:32:18 +0000219struct tm* _Nullable gmtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700220
221/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000222 * [gmtime_r(3)](https://man7.org/linux/man-pages/man3/gmtime_r.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700223 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
224 * UTC (historically also known as GMT).
225 *
226 * That broken-down time will be written to the provided struct `tm`.
227 *
228 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
229 */
zijunzhaoe6202662023-01-03 23:32:18 +0000230struct tm* _Nullable gmtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700232/**
233 * Inverse of gmtime().
234 */
235time_t timegm(struct tm* _Nonnull __tm);
236
237/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000238 * [strptime(3)](https://man7.org/linux/man-pages/man3/strptime.3.html) parses
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700239 * a string `s` assuming format `fmt` into broken-down time `tm`.
240 *
241 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
242 */
zijunzhaoe6202662023-01-03 23:32:18 +0000243char* _Nullable strptime(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm) __strftimelike(2);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700244
245/**
246 * Equivalent to strptime() on Android where only C/POSIX locales are available.
247 */
Elliott Hughes55acfc12024-08-23 13:48:02 +0000248char* _Nullable strptime_l(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm, locale_t _Nonnull __l) __strftimelike(2) __RENAME(strptime);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700249
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700250/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000251 * [strftime(3)](https://man7.org/linux/man-pages/man3/strftime.3.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700252 * a broken-down time `tm` into the buffer `buf` using format `fmt`.
253 *
254 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
255 */
zijunzhaoe6202662023-01-03 23:32:18 +0000256size_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 -0700257
258/**
259 * Equivalent to strftime() on Android where only C/POSIX locales are available.
260 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700261size_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 +0000262
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700263/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000264 * [ctime(3)](https://man7.org/linux/man-pages/man3/ctime.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700265 * the time `tm` as a string.
266 *
267 * Returns a pointer to a string on success, and returns NULL on failure.
268 *
269 * That string will be overwritten by later calls to this function.
270 *
271 * New code should prefer strftime().
272 */
zijunzhaoe6202662023-01-03 23:32:18 +0000273char* _Nullable ctime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700274
275/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000276 * [ctime_r(3)](https://man7.org/linux/man-pages/man3/ctime.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700277 * the time `tm` as a string in the given buffer `buf`.
278 *
279 * Returns a pointer to a string on success, and returns NULL on failure.
280 *
281 * New code should prefer strftime().
282 */
zijunzhaoe6202662023-01-03 23:32:18 +0000283char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700285/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000286 * [tzset(3)](https://man7.org/linux/man-pages/man3/tzset.3.html) tells
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700287 * libc that the timezone has changed.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700288 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700289 * tzset() on Android looks at both the system property
290 * `persist.sys.timezone` and the environment variable `TZ`. The former is
291 * the device's current timezone as shown in Settings, while the latter is
292 * usually unset but can be used to override the global setting. This is a
293 * bad idea outside of unit tests or single-threaded programs because it's
294 * inherently thread-unsafe. See tzalloc(), localtime_rz(), mktime_z(),
295 * and tzfree() for an alternative.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700296 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700297void tzset(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700299/**
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700300 * tzalloc(3) allocates a timezone corresponding to the given Olson ID.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700301 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700302 * A null `id` returns the system timezone (as seen in Settings) from
303 * the system property `persist.sys.timezone`, ignoring `$TZ`. Although
304 * tzset() honors `$TZ`, callers of tzalloc() can use `$TZ` themselves if
305 * that's the (thread-unsafe) behavior they want, but by ignoring `$TZ`
306 * tzalloc() is thread safe (though obviously the system timezone can
307 * change, especially if your mobile device is actually mobile!).
308 *
Elliott Hughes5ea305b2023-06-22 20:53:00 +0000309 * To use this with std::unique_ptr you'll want something like
310 * `std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};`
311 * to remove the pointer.
312 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700313 * Returns a timezone object on success, and returns NULL and sets `errno` on failure.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700314 *
315 * Available since API level 35.
316 */
317timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35);
318
319/**
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700320 * tzfree(3) frees a timezone object returned by tzalloc().
Elliott Hughes2bd43162023-06-15 13:17:08 -0700321 *
Elliott Hughes5ea305b2023-06-22 20:53:00 +0000322 * To use this with std::unique_ptr you'll want something like
323 * `std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};`
324 * to remove the pointer.
325 *
Elliott Hughes2bd43162023-06-15 13:17:08 -0700326 * Available since API level 35.
327 */
328void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35);
329
330/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000331 * [clock(3)](https://man7.org/linux/man-pages/man3/clock.3.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700332 * returns an approximation of CPU time used, equivalent to
333 * `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)` but with more confusing
334 * units. Use `CLOCKS_PER_SEC` to convert the result to seconds.
335 *
336 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
337 *
338 * New code should prefer `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)`.
339 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700340clock_t clock(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700342/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000343 * [clock_getcpuclockid(3)](https://man7.org/linux/man-pages/man3/clock_getcpuclockid.3.html)
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700344 * gets the clock ID of the cpu-time clock for the given `pid`.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700345 *
346 * Returns 0 on success, and returns -1 and returns an error number on failure.
347 */
zijunzhaoe6202662023-01-03 23:32:18 +0000348int clock_getcpuclockid(pid_t __pid, clockid_t* _Nonnull __clock) __INTRODUCED_IN(23);
Yabin Cuid5c65272014-11-26 14:04:26 -0800349
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700350/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000351 * [clock_getres(2)](https://man7.org/linux/man-pages/man2/clock_getres.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700352 * gets the resolution of the given clock.
353 *
354 * Returns 0 on success, and returns -1 and returns an error number on failure.
355 */
zijunzhaoe6202662023-01-03 23:32:18 +0000356int clock_getres(clockid_t __clock, struct timespec* _Nullable __resolution);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700357
358/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000359 * [clock_gettime(2)](https://man7.org/linux/man-pages/man2/clock_gettime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700360 * gets the time according to the given clock.
361 *
362 * Returns 0 on success, and returns -1 and returns an error number on failure.
363 */
zijunzhaoe6202662023-01-03 23:32:18 +0000364int clock_gettime(clockid_t __clock, struct timespec* _Nonnull __ts);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700365
366/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000367 * [clock_nanosleep(2)](https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html)
Elliott Hughes8dc9c1c2024-03-05 00:17:22 +0000368 * sleeps for the given time (or until the given time if the TIMER_ABSTIME flag
369 * is used), as measured by the given clock.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700370 *
371 * Returns 0 on success, and returns -1 and returns an error number on failure.
372 * If the sleep was interrupted by a signal, the return value will be `EINTR`
373 * and `remainder` will be the amount of time remaining.
374 */
Elliott Hughes8dc9c1c2024-03-05 00:17:22 +0000375int clock_nanosleep(clockid_t __clock, int __flags, const struct timespec* _Nonnull __time, struct timespec* _Nullable __remainder);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700376
377/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000378 * [clock_settime(2)](https://man7.org/linux/man-pages/man2/clock_settime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700379 * sets the time for the given clock.
380 *
381 * Returns 0 on success, and returns -1 and returns an error number on failure.
382 */
zijunzhaoe6202662023-01-03 23:32:18 +0000383int clock_settime(clockid_t __clock, const struct timespec* _Nonnull __ts);
384
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700385/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000386 * [timer_create(2)](https://man7.org/linux/man-pages/man2/timer_create.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700387 * creates a POSIX timer.
388 *
389 * Returns 0 on success, and returns -1 and sets `errno` on failure.
390 */
zijunzhaoe6202662023-01-03 23:32:18 +0000391int 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 -0800392
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700393/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000394 * [timer_delete(2)](https://man7.org/linux/man-pages/man2/timer_delete.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700395 * destroys a POSIX timer.
396 *
397 * Returns 0 on success, and returns -1 and sets `errno` on failure.
398 */
399int timer_delete(timer_t _Nonnull __timer);
400
401/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000402 * [timer_settime(2)](https://man7.org/linux/man-pages/man2/timer_settime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700403 * starts or stops a POSIX timer.
404 *
405 * Returns 0 on success, and returns -1 and sets `errno` on failure.
406 */
407int timer_settime(timer_t _Nonnull __timer, int __flags, const struct itimerspec* _Nonnull __new_value, struct itimerspec* _Nullable __old_value);
408
409/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000410 * [timer_gettime(2)](https://man7.org/linux/man-pages/man2/timer_gettime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700411 * gets the time until the given timer next fires.
412 *
413 * Returns 0 on success, and returns -1 and sets `errno` on failure.
414 */
415int timer_gettime(timer_t _Nonnull _timer, struct itimerspec* _Nonnull __ts);
416
417/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000418 * [timer_getoverrun(2)](https://man7.org/linux/man-pages/man2/timer_getoverrun.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700419 * gets the overrun count (the number of times the timer should have fired, but
420 * didn't) for the last time the timer fired.
421 *
422 * Returns the overrun count on success, and returns -1 and sets `errno` on failure.
423 */
424int timer_getoverrun(timer_t _Nonnull __timer);
David 'Digit' Turner6481b912010-12-06 12:23:16 +0100425
Elliott Hughes52541ee2023-04-24 17:04:49 -0700426/**
427 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_REALTIME.
428 *
429 * Available since API level 29.
430 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700431#define TIME_UTC (CLOCK_REALTIME+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700432
433/**
434 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_MONOTONIC.
435 *
436 * Available since API level 35.
437 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700438#define TIME_MONOTONIC (CLOCK_MONOTONIC+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700439
440/**
441 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_PROCESS_CPUTIME_ID.
442 *
443 * Available since API level 35.
444 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700445#define TIME_ACTIVE (CLOCK_PROCESS_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700446
447/**
448 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_THREAD_CPUTIME_ID.
449 *
450 * Available since API level 35.
451 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700452#define TIME_THREAD_ACTIVE (CLOCK_THREAD_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700453
454/**
455 * timespec_get(3) is equivalent to clock_gettime() for the clock corresponding to the given base.
456 *
457 * Returns the base on success and returns 0 on failure.
458 *
459 * Available since API level 29 for TIME_UTC; other bases arrived later.
460 * Code for Android should prefer clock_gettime().
461 */
zijunzhaoe6202662023-01-03 23:32:18 +0000462int timespec_get(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(29);
Elliott Hughesf98d87b2018-07-17 13:21:05 -0700463
Elliott Hughes52541ee2023-04-24 17:04:49 -0700464/**
465 * timespec_getres(3) is equivalent to clock_getres() for the clock corresponding to the given base.
466 *
467 * Returns the base on success and returns 0 on failure.
468 *
469 * Available since API level 35.
470 * Code for Android should prefer clock_gettime().
471 */
472int timespec_getres(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(35);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473
Elliott Hughes52541ee2023-04-24 17:04:49 -0700474__END_DECLS