blob: 6c9b761bc36de792b6e745350fd0cb293451574b [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 */
Dan Albert02ce4012024-10-25 19:13:49 +0000169
170#if __BIONIC_AVAILABILITY_GUARD(35)
Elliott Hughes2bd43162023-06-15 13:17:08 -0700171time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
Dan Albert02ce4012024-10-25 19:13:49 +0000172#endif /* __BIONIC_AVAILABILITY_GUARD(35) */
173
Elliott Hughes2bd43162023-06-15 13:17:08 -0700174
175/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000176 * [localtime(3)](https://man7.org/linux/man-pages/man3/localtime.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700177 * the number of seconds since the Unix epoch in `t` to a broken-down time, taking
178 * the device's timezone into account.
179 *
180 * That broken-down time will be overwritten by later calls to this function.
181 *
182 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
183 */
zijunzhaoe6202662023-01-03 23:32:18 +0000184struct tm* _Nullable localtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700185
186/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000187 * [localtime_r(3)](https://man7.org/linux/man-pages/man3/localtime_r.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700188 * the number of seconds since the Unix epoch in `t` to a broken-down time.
189 * That broken-down time will be written to the given struct `tm`.
190 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700191 * See tzset() for details of how the timezone is set, and localtime_rz()
Elliott Hughes2bd43162023-06-15 13:17:08 -0700192 * for an alternative.
193 *
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700194 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
195 */
zijunzhaoe6202662023-01-03 23:32:18 +0000196struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700198/**
Elliott Hughes2bd43162023-06-15 13:17:08 -0700199 * localtime_rz(3) converts the number of seconds since the Unix epoch in
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700200 * `t` to a broken-down time, assuming the given timezone. That broken-down
Elliott Hughes2bd43162023-06-15 13:17:08 -0700201 * time will be written to the given struct `tm`.
202 *
203 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
204 *
205 * Available since API level 35.
206 */
Dan Albert02ce4012024-10-25 19:13:49 +0000207
208#if __BIONIC_AVAILABILITY_GUARD(35)
Elliott Hughes2bd43162023-06-15 13:17:08 -0700209struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
Dan Albert02ce4012024-10-25 19:13:49 +0000210#endif /* __BIONIC_AVAILABILITY_GUARD(35) */
211
Elliott Hughes2bd43162023-06-15 13:17:08 -0700212
213/**
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700214 * Inverse of localtime().
215 */
216time_t timelocal(struct tm* _Nonnull __tm);
217
218/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000219 * [gmtime(3)](https://man7.org/linux/man-pages/man3/gmtime.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700220 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
221 * UTC (historically also known as GMT).
222 *
223 * That broken-down time will be overwritten by later calls to this function.
224 *
225 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
226 */
zijunzhaoe6202662023-01-03 23:32:18 +0000227struct tm* _Nullable gmtime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700228
229/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000230 * [gmtime_r(3)](https://man7.org/linux/man-pages/man3/gmtime_r.3p.html) converts
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700231 * the number of seconds since the Unix epoch in `t` to a broken-down time, using
232 * UTC (historically also known as GMT).
233 *
234 * That broken-down time will be written to the provided struct `tm`.
235 *
236 * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
237 */
zijunzhaoe6202662023-01-03 23:32:18 +0000238struct tm* _Nullable gmtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700240/**
241 * Inverse of gmtime().
242 */
243time_t timegm(struct tm* _Nonnull __tm);
244
245/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000246 * [strptime(3)](https://man7.org/linux/man-pages/man3/strptime.3.html) parses
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700247 * a string `s` assuming format `fmt` into broken-down time `tm`.
248 *
249 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
250 */
zijunzhaoe6202662023-01-03 23:32:18 +0000251char* _Nullable strptime(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm) __strftimelike(2);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700252
253/**
254 * Equivalent to strptime() on Android where only C/POSIX locales are available.
255 */
Elliott Hughes55acfc12024-08-23 13:48:02 +0000256char* _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 -0700257
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700258/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000259 * [strftime(3)](https://man7.org/linux/man-pages/man3/strftime.3.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700260 * a broken-down time `tm` into the buffer `buf` using format `fmt`.
261 *
262 * Returns a pointer to the first character _not_ parsed, or null if no characters were parsed.
263 */
zijunzhaoe6202662023-01-03 23:32:18 +0000264size_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 -0700265
266/**
267 * Equivalent to strftime() on Android where only C/POSIX locales are available.
268 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700269size_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 +0000270
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700271/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000272 * [ctime(3)](https://man7.org/linux/man-pages/man3/ctime.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700273 * the time `tm` as a string.
274 *
275 * Returns a pointer to a string on success, and returns NULL on failure.
276 *
277 * That string will be overwritten by later calls to this function.
278 *
279 * New code should prefer strftime().
280 */
zijunzhaoe6202662023-01-03 23:32:18 +0000281char* _Nullable ctime(const time_t* _Nonnull __t);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700282
283/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000284 * [ctime_r(3)](https://man7.org/linux/man-pages/man3/ctime.3p.html) formats
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700285 * the time `tm` as a string in the given buffer `buf`.
286 *
287 * Returns a pointer to a string on success, and returns NULL on failure.
288 *
289 * New code should prefer strftime().
290 */
zijunzhaoe6202662023-01-03 23:32:18 +0000291char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700293/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000294 * [tzset(3)](https://man7.org/linux/man-pages/man3/tzset.3.html) tells
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700295 * libc that the timezone has changed.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700296 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700297 * tzset() on Android looks at both the system property
298 * `persist.sys.timezone` and the environment variable `TZ`. The former is
299 * the device's current timezone as shown in Settings, while the latter is
300 * usually unset but can be used to override the global setting. This is a
301 * bad idea outside of unit tests or single-threaded programs because it's
302 * inherently thread-unsafe. See tzalloc(), localtime_rz(), mktime_z(),
303 * and tzfree() for an alternative.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700304 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700305void tzset(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700307/**
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700308 * tzalloc(3) allocates a timezone corresponding to the given Olson ID.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700309 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700310 * A null `id` returns the system timezone (as seen in Settings) from
311 * the system property `persist.sys.timezone`, ignoring `$TZ`. Although
312 * tzset() honors `$TZ`, callers of tzalloc() can use `$TZ` themselves if
313 * that's the (thread-unsafe) behavior they want, but by ignoring `$TZ`
314 * tzalloc() is thread safe (though obviously the system timezone can
315 * change, especially if your mobile device is actually mobile!).
316 *
Elliott Hughes5ea305b2023-06-22 20:53:00 +0000317 * To use this with std::unique_ptr you'll want something like
318 * `std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};`
319 * to remove the pointer.
320 *
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700321 * Returns a timezone object on success, and returns NULL and sets `errno` on failure.
Elliott Hughes2bd43162023-06-15 13:17:08 -0700322 *
323 * Available since API level 35.
324 */
Dan Albert02ce4012024-10-25 19:13:49 +0000325
326#if __BIONIC_AVAILABILITY_GUARD(35)
Elliott Hughes2bd43162023-06-15 13:17:08 -0700327timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35);
328
329/**
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700330 * tzfree(3) frees a timezone object returned by tzalloc().
Elliott Hughes2bd43162023-06-15 13:17:08 -0700331 *
Elliott Hughes5ea305b2023-06-22 20:53:00 +0000332 * To use this with std::unique_ptr you'll want something like
333 * `std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};`
334 * to remove the pointer.
335 *
Elliott Hughes2bd43162023-06-15 13:17:08 -0700336 * Available since API level 35.
337 */
338void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35);
Dan Albert02ce4012024-10-25 19:13:49 +0000339#endif /* __BIONIC_AVAILABILITY_GUARD(35) */
340
Elliott Hughes2bd43162023-06-15 13:17:08 -0700341
342/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000343 * [clock(3)](https://man7.org/linux/man-pages/man3/clock.3.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700344 * returns an approximation of CPU time used, equivalent to
345 * `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)` but with more confusing
346 * units. Use `CLOCKS_PER_SEC` to convert the result to seconds.
347 *
348 * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
349 *
350 * New code should prefer `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)`.
351 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700352clock_t clock(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800353
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700354/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000355 * [clock_getcpuclockid(3)](https://man7.org/linux/man-pages/man3/clock_getcpuclockid.3.html)
Elliott Hughes31fc69f2023-06-20 15:36:11 -0700356 * gets the clock ID of the cpu-time clock for the given `pid`.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700357 *
358 * Returns 0 on success, and returns -1 and returns an error number on failure.
359 */
Dan Albert02ce4012024-10-25 19:13:49 +0000360
361#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhaoe6202662023-01-03 23:32:18 +0000362int clock_getcpuclockid(pid_t __pid, clockid_t* _Nonnull __clock) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +0000363#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
364
Yabin Cuid5c65272014-11-26 14:04:26 -0800365
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700366/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000367 * [clock_getres(2)](https://man7.org/linux/man-pages/man2/clock_getres.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700368 * gets the resolution of the given clock.
369 *
370 * Returns 0 on success, and returns -1 and returns an error number on failure.
371 */
zijunzhaoe6202662023-01-03 23:32:18 +0000372int clock_getres(clockid_t __clock, struct timespec* _Nullable __resolution);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700373
374/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000375 * [clock_gettime(2)](https://man7.org/linux/man-pages/man2/clock_gettime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700376 * gets the time according to the given clock.
377 *
378 * Returns 0 on success, and returns -1 and returns an error number on failure.
379 */
zijunzhaoe6202662023-01-03 23:32:18 +0000380int clock_gettime(clockid_t __clock, struct timespec* _Nonnull __ts);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700381
382/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000383 * [clock_nanosleep(2)](https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html)
Elliott Hughes8dc9c1c2024-03-05 00:17:22 +0000384 * sleeps for the given time (or until the given time if the TIMER_ABSTIME flag
385 * is used), as measured by the given clock.
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700386 *
387 * Returns 0 on success, and returns -1 and returns an error number on failure.
388 * If the sleep was interrupted by a signal, the return value will be `EINTR`
389 * and `remainder` will be the amount of time remaining.
390 */
Elliott Hughes8dc9c1c2024-03-05 00:17:22 +0000391int clock_nanosleep(clockid_t __clock, int __flags, const struct timespec* _Nonnull __time, struct timespec* _Nullable __remainder);
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700392
393/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000394 * [clock_settime(2)](https://man7.org/linux/man-pages/man2/clock_settime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700395 * sets the time for the given clock.
396 *
397 * Returns 0 on success, and returns -1 and returns an error number on failure.
398 */
zijunzhaoe6202662023-01-03 23:32:18 +0000399int clock_settime(clockid_t __clock, const struct timespec* _Nonnull __ts);
400
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700401/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000402 * [timer_create(2)](https://man7.org/linux/man-pages/man2/timer_create.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700403 * creates a POSIX timer.
404 *
405 * Returns 0 on success, and returns -1 and sets `errno` on failure.
406 */
zijunzhaoe6202662023-01-03 23:32:18 +0000407int 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 -0800408
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700409/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000410 * [timer_delete(2)](https://man7.org/linux/man-pages/man2/timer_delete.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700411 * destroys a POSIX timer.
412 *
413 * Returns 0 on success, and returns -1 and sets `errno` on failure.
414 */
415int timer_delete(timer_t _Nonnull __timer);
416
417/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000418 * [timer_settime(2)](https://man7.org/linux/man-pages/man2/timer_settime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700419 * starts or stops a POSIX timer.
420 *
421 * Returns 0 on success, and returns -1 and sets `errno` on failure.
422 */
423int timer_settime(timer_t _Nonnull __timer, int __flags, const struct itimerspec* _Nonnull __new_value, struct itimerspec* _Nullable __old_value);
424
425/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000426 * [timer_gettime(2)](https://man7.org/linux/man-pages/man2/timer_gettime.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700427 * gets the time until the given timer next fires.
428 *
429 * Returns 0 on success, and returns -1 and sets `errno` on failure.
430 */
431int timer_gettime(timer_t _Nonnull _timer, struct itimerspec* _Nonnull __ts);
432
433/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000434 * [timer_getoverrun(2)](https://man7.org/linux/man-pages/man2/timer_getoverrun.2.html)
Elliott Hughesd192dbe2023-05-25 11:17:56 -0700435 * gets the overrun count (the number of times the timer should have fired, but
436 * didn't) for the last time the timer fired.
437 *
438 * Returns the overrun count on success, and returns -1 and sets `errno` on failure.
439 */
440int timer_getoverrun(timer_t _Nonnull __timer);
David 'Digit' Turner6481b912010-12-06 12:23:16 +0100441
Elliott Hughes52541ee2023-04-24 17:04:49 -0700442/**
443 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_REALTIME.
444 *
445 * Available since API level 29.
446 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700447#define TIME_UTC (CLOCK_REALTIME+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700448
449/**
450 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_MONOTONIC.
451 *
452 * Available since API level 35.
453 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700454#define TIME_MONOTONIC (CLOCK_MONOTONIC+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700455
456/**
457 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_PROCESS_CPUTIME_ID.
458 *
459 * Available since API level 35.
460 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700461#define TIME_ACTIVE (CLOCK_PROCESS_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700462
463/**
464 * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_THREAD_CPUTIME_ID.
465 *
466 * Available since API level 35.
467 */
Elliott Hughes7db0a6c2023-05-03 15:37:46 -0700468#define TIME_THREAD_ACTIVE (CLOCK_THREAD_CPUTIME_ID+1)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700469
470/**
471 * timespec_get(3) is equivalent to clock_gettime() for the clock corresponding to the given base.
472 *
473 * Returns the base on success and returns 0 on failure.
474 *
475 * Available since API level 29 for TIME_UTC; other bases arrived later.
476 * Code for Android should prefer clock_gettime().
477 */
Dan Albert02ce4012024-10-25 19:13:49 +0000478
479#if __BIONIC_AVAILABILITY_GUARD(29)
zijunzhaoe6202662023-01-03 23:32:18 +0000480int timespec_get(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(29);
Dan Albert02ce4012024-10-25 19:13:49 +0000481#endif /* __BIONIC_AVAILABILITY_GUARD(29) */
482
Elliott Hughesf98d87b2018-07-17 13:21:05 -0700483
Elliott Hughes52541ee2023-04-24 17:04:49 -0700484/**
485 * timespec_getres(3) is equivalent to clock_getres() for the clock corresponding to the given base.
486 *
487 * Returns the base on success and returns 0 on failure.
488 *
489 * Available since API level 35.
490 * Code for Android should prefer clock_gettime().
491 */
Dan Albert02ce4012024-10-25 19:13:49 +0000492
493#if __BIONIC_AVAILABILITY_GUARD(35)
Elliott Hughes52541ee2023-04-24 17:04:49 -0700494int timespec_getres(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(35);
Dan Albert02ce4012024-10-25 19:13:49 +0000495#endif /* __BIONIC_AVAILABILITY_GUARD(35) */
496
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800497
Elliott Hughes52541ee2023-04-24 17:04:49 -0700498__END_DECLS