The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 29 | #pragma once |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 31 | /** |
| 32 | * @file time.h |
| 33 | * @brief Clock and timer functionality. |
| 34 | */ |
| 35 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | #include <sys/cdefs.h> |
| 37 | #include <sys/time.h> |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 38 | #include <xlocale.h> |
David 'Digit' Turner | c1b44ec | 2012-10-17 19:10:11 +0200 | [diff] [blame] | 39 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | __BEGIN_DECLS |
| 41 | |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 42 | /* If we just use void* in the typedef, the compiler exposes that in error messages. */ |
| 43 | struct __timezone_t; |
| 44 | |
| 45 | /** The `timezone_t` type that represents a time zone. */ |
| 46 | typedef struct __timezone_t* timezone_t; |
| 47 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 48 | /** Divisor to compute seconds from the result of a call to clock(). */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 49 | #define CLOCKS_PER_SEC 1000000 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 51 | /** |
| 52 | * The name of the current time zone's non-daylight savings (`tzname[0]`) and |
| 53 | * daylight savings (`tzname[1]`) variants. See tzset(). |
| 54 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 55 | extern char* _Nonnull tzname[]; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 56 | |
| 57 | /** Whether the current time zone ever uses daylight savings time. See tzset(). */ |
Elliott Hughes | f47514d | 2016-07-19 13:56:46 -0700 | [diff] [blame] | 58 | extern int daylight; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 59 | |
| 60 | /** The difference in seconds between UTC and the current time zone. See tzset(). */ |
Elliott Hughes | f47514d | 2016-07-19 13:56:46 -0700 | [diff] [blame] | 61 | extern long int timezone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 | |
Elliott Hughes | 61fb3fc | 2013-11-07 12:28:46 -0800 | [diff] [blame] | 63 | struct sigevent; |
| 64 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 65 | /** |
| 66 | * A "broken-down" time, useful for parsing/formatting times for human consumption. |
| 67 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 | struct tm { |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 69 | /** Seconds, 0-60. (60 is a leap second.) */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 70 | int tm_sec; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 71 | /** Minutes, 0-59. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 72 | int tm_min; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 73 | /** Hours, 0-23. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 74 | int tm_hour; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 75 | /** Day of month, 1-31. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 76 | int tm_mday; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 77 | /** Month of year, 0-11. (Not 1-12!) */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 78 | int tm_mon; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 79 | /** Years since 1900. (So 2023 is 123, not 2023!) */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 80 | int tm_year; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 81 | /** Day of week, 0-6. (Sunday is 0, Saturday is 6.) */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 82 | int tm_wday; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 83 | /** Day of year, 0-365. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 84 | int tm_yday; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 85 | /** Daylight savings flag, positive for DST in effect, 0 for DST not in effect, and -1 for unknown. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 86 | int tm_isdst; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 87 | /** Offset from UTC (GMT) in seconds for this time. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 88 | long int tm_gmtoff; |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 89 | /** Name of the time zone for this time. */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 90 | const char* _Nullable tm_zone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 93 | /** Alternative name for `tm_zone` in `struct tm`. */ |
Elliott Hughes | 3503ce2 | 2013-11-05 13:28:36 -0800 | [diff] [blame] | 94 | #define TM_ZONE tm_zone |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 96 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 102 | time_t time(time_t* _Nullable __t); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 103 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 112 | int nanosleep(const struct timespec* _Nonnull __request, struct timespec* _Nullable __remainder); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 114 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 124 | char* _Nullable asctime(const struct tm* _Nonnull __tm); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 125 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 134 | char* _Nullable asctime_r(const struct tm* _Nonnull __tm, char* _Nonnull __buf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 136 | /** |
| 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 Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 142 | double difftime(time_t __lhs, time_t __rhs); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 143 | |
| 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 Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 148 | * See tzset() for details of how the time zone is set, and mktime_rz() |
| 149 | * for an alternative. |
| 150 | * |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 151 | * Returns the time in seconds on success, and returns -1 and sets `errno` on failure. |
| 152 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 153 | time_t mktime(struct tm* _Nonnull __tm); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 155 | /** |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 156 | * 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 | */ |
| 163 | time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35); |
| 164 | |
| 165 | /** |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 166 | * [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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 174 | struct tm* _Nullable localtime(const time_t* _Nonnull __t); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 175 | |
| 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 Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 181 | * See tzset() for details of how the time zone is set, and localtime_rz() |
| 182 | * for an alternative. |
| 183 | * |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 184 | * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure. |
| 185 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 186 | struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 187 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 188 | /** |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 189 | * 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 | */ |
| 197 | struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35); |
| 198 | |
| 199 | /** |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 200 | * Inverse of localtime(). |
| 201 | */ |
| 202 | time_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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 213 | struct tm* _Nullable gmtime(const time_t* _Nonnull __t); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 214 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 224 | struct tm* _Nullable gmtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 225 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Inverse of gmtime(). |
| 228 | */ |
| 229 | time_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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 237 | char* _Nullable strptime(const char* _Nonnull __s, const char* _Nonnull __fmt, struct tm* _Nonnull __tm) __strftimelike(2); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * Equivalent to strptime() on Android where only C/POSIX locales are available. |
| 241 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 242 | char* _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 Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 243 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 244 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 250 | size_t strftime(char* _Nonnull __buf, size_t __n, const char* _Nonnull __fmt, const struct tm* _Nullable __tm) __strftimelike(3); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * Equivalent to strftime() on Android where only C/POSIX locales are available. |
| 254 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 255 | size_t strftime_l(char* _Nonnull __buf, size_t __n, const char* _Nonnull __fmt, const struct tm* _Nullable __tm, locale_t _Nonnull __l) __strftimelike(3) __INTRODUCED_IN(21); |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 256 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 257 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 267 | char* _Nullable ctime(const time_t* _Nonnull __t); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 268 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 277 | char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 278 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 279 | /** |
| 280 | * [tzset(3)](http://man7.org/linux/man-pages/man3/tzset.3.html) tells |
| 281 | * libc that the time zone has changed. |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 282 | * |
| 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 Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 290 | */ |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 291 | void tzset(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 293 | /** |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame^] | 294 | * 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 | */ |
| 300 | timezone_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 | */ |
| 307 | void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35); |
| 308 | |
| 309 | /** |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 310 | * [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 Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 319 | clock_t clock(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 320 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 321 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 327 | int clock_getcpuclockid(pid_t __pid, clockid_t* _Nonnull __clock) __INTRODUCED_IN(23); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 328 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 329 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 335 | int clock_getres(clockid_t __clock, struct timespec* _Nullable __resolution); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 336 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 343 | int clock_gettime(clockid_t __clock, struct timespec* _Nonnull __ts); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 344 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 353 | int clock_nanosleep(clockid_t __clock, int __flags, const struct timespec* _Nonnull __request, struct timespec* _Nullable __remainder); |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 354 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 361 | int clock_settime(clockid_t __clock, const struct timespec* _Nonnull __ts); |
| 362 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 363 | /** |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 369 | int timer_create(clockid_t __clock, struct sigevent* _Nullable __event, timer_t _Nonnull * _Nonnull __timer_ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 370 | |
Elliott Hughes | d192dbe | 2023-05-25 11:17:56 -0700 | [diff] [blame] | 371 | /** |
| 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 | */ |
| 377 | int 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 | */ |
| 385 | int 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 | */ |
| 393 | int 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 | */ |
| 402 | int timer_getoverrun(timer_t _Nonnull __timer); |
David 'Digit' Turner | 6481b91 | 2010-12-06 12:23:16 +0100 | [diff] [blame] | 403 | |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 404 | /** |
| 405 | * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_REALTIME. |
| 406 | * |
| 407 | * Available since API level 29. |
| 408 | */ |
Elliott Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 409 | #define TIME_UTC (CLOCK_REALTIME+1) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * The timebase for timespec_get() and timespec_getres() corresponding to CLOCK_MONOTONIC. |
| 413 | * |
| 414 | * Available since API level 35. |
| 415 | */ |
Elliott Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 416 | #define TIME_MONOTONIC (CLOCK_MONOTONIC+1) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 417 | |
| 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 Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 423 | #define TIME_ACTIVE (CLOCK_PROCESS_CPUTIME_ID+1) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 424 | |
| 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 Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 430 | #define TIME_THREAD_ACTIVE (CLOCK_THREAD_CPUTIME_ID+1) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 431 | |
| 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 | */ |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 440 | int timespec_get(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(29); |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 441 | |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 442 | /** |
| 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 | */ |
| 450 | int timespec_getres(struct timespec* _Nonnull __ts, int __base) __INTRODUCED_IN(35); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 451 | |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 452 | __END_DECLS |