time.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * ISO C99 Standard: 7.23 Date and time <time.h>
  16. */
  17. #ifndef _TIME_H
  18. #define _TIME_H 1
  19. #include <features.h>
  20. #define __need_size_t
  21. #define __need_NULL
  22. #include <stddef.h>
  23. #if __GLIBC_USE (ISOC23)
  24. # define __STDC_VERSION_TIME_H__ 202311L
  25. #endif
  26. /* This defines CLOCKS_PER_SEC, which is the number of processor clock
  27. ticks per second, and possibly a number of other constants. */
  28. #include <bits/time.h>
  29. /* Many of the typedefs and structs whose official home is this header
  30. may also need to be defined by other headers. */
  31. #include <bits/types/clock_t.h>
  32. #include <bits/types/time_t.h>
  33. #include <bits/types/struct_tm.h>
  34. #if defined __USE_POSIX199309 || defined __USE_ISOC11
  35. # include <bits/types/struct_timespec.h>
  36. #endif
  37. #ifdef __USE_POSIX199309
  38. # include <bits/types/clockid_t.h>
  39. # include <bits/types/timer_t.h>
  40. # include <bits/types/struct_itimerspec.h>
  41. struct sigevent;
  42. #endif
  43. #ifdef __USE_XOPEN2K
  44. # ifndef __pid_t_defined
  45. typedef __pid_t pid_t;
  46. # define __pid_t_defined
  47. # endif
  48. #endif
  49. #ifdef __USE_XOPEN2K8
  50. # include <bits/types/locale_t.h>
  51. #endif
  52. #ifdef __USE_ISOC11
  53. /* Time base values for timespec_get. */
  54. # define TIME_UTC 1
  55. #endif
  56. #if __GLIBC_USE (ISOC23)
  57. # define TIME_MONOTONIC 2
  58. # define TIME_ACTIVE 3
  59. # define TIME_THREAD_ACTIVE 4
  60. #endif
  61. __BEGIN_DECLS
  62. /* Time used by the program so far (user time + system time).
  63. The result / CLOCKS_PER_SEC is program time in seconds. */
  64. extern clock_t clock (void) __THROW;
  65. #ifndef __USE_TIME64_REDIRECTS
  66. /* Return the current time and put it in *TIMER if TIMER is not NULL. */
  67. extern time_t time (time_t *__timer) __THROW;
  68. /* Return the difference between TIME1 and TIME0. */
  69. extern double difftime (time_t __time1, time_t __time0);
  70. /* Return the `time_t' representation of TP and normalize TP. */
  71. extern time_t mktime (struct tm *__tp) __THROW;
  72. #else
  73. # ifdef __REDIRECT_NTH
  74. extern time_t __REDIRECT_NTH (time, (time_t *__timer), __time64);
  75. extern double __REDIRECT_NTH (difftime, (time_t __time1, time_t __time0),
  76. __difftime64);
  77. extern time_t __REDIRECT_NTH (mktime, (struct tm *__tp), __mktime64);
  78. # else
  79. # define time __time64
  80. # define difftime __difftime64
  81. # define mktime __mktime64
  82. # endif
  83. #endif
  84. /* Format TP into S according to FORMAT.
  85. Write no more than MAXSIZE characters and return the number
  86. of characters written, or 0 if it would exceed MAXSIZE. */
  87. extern size_t strftime (char *__restrict __s, size_t __maxsize,
  88. const char *__restrict __format,
  89. const struct tm *__restrict __tp)
  90. __THROW __nonnull((1, 3, 4));
  91. #ifdef __USE_XOPEN
  92. /* Parse S according to FORMAT and store binary time information in TP.
  93. The return value is a pointer to the first unparsed character in S. */
  94. extern char *strptime (const char *__restrict __s,
  95. const char *__restrict __fmt, struct tm *__tp)
  96. __THROW;
  97. #endif
  98. #ifdef __USE_XOPEN2K8
  99. /* Similar to the two functions above but take the information from
  100. the provided locale and not the global locale. */
  101. extern size_t strftime_l (char *__restrict __s, size_t __maxsize,
  102. const char *__restrict __format,
  103. const struct tm *__restrict __tp,
  104. locale_t __loc) __THROW;
  105. #endif
  106. #ifdef __USE_GNU
  107. extern char *strptime_l (const char *__restrict __s,
  108. const char *__restrict __fmt, struct tm *__tp,
  109. locale_t __loc) __THROW;
  110. #endif
  111. #ifndef __USE_TIME64_REDIRECTS
  112. /* Return the `struct tm' representation of *TIMER
  113. in Universal Coordinated Time (aka Greenwich Mean Time). */
  114. extern struct tm *gmtime (const time_t *__timer) __THROW;
  115. /* Return the `struct tm' representation
  116. of *TIMER in the local timezone. */
  117. extern struct tm *localtime (const time_t *__timer) __THROW;
  118. #else
  119. # ifdef __REDIRECT_NTH
  120. extern struct tm*__REDIRECT_NTH (gmtime, (const time_t *__timer), __gmtime64);
  121. extern struct tm *__REDIRECT_NTH (localtime, (const time_t *__timer),
  122. __localtime64);
  123. # else
  124. # define gmtime __gmtime64
  125. # define localtime __localtime64
  126. # endif
  127. #endif
  128. #if defined __USE_POSIX || __GLIBC_USE (ISOC23)
  129. # ifndef __USE_TIME64_REDIRECTS
  130. /* Return the `struct tm' representation of *TIMER in UTC,
  131. using *TP to store the result. */
  132. extern struct tm *gmtime_r (const time_t *__restrict __timer,
  133. struct tm *__restrict __tp) __THROW;
  134. /* Return the `struct tm' representation of *TIMER in local time,
  135. using *TP to store the result. */
  136. extern struct tm *localtime_r (const time_t *__restrict __timer,
  137. struct tm *__restrict __tp) __THROW;
  138. # else
  139. # ifdef __REDIRECT_NTH
  140. extern struct tm*__REDIRECT_NTH (gmtime_r, (const time_t *__restrict __timer,
  141. struct tm *__restrict __tp),
  142. __gmtime64_r);
  143. extern struct tm*__REDIRECT_NTH (localtime_r, (const time_t *__restrict __t,
  144. struct tm *__restrict __tp),
  145. __localtime64_r);
  146. # else
  147. # define gmtime_r __gmtime64_r
  148. # define localtime_r __localtime_r
  149. # endif
  150. # endif
  151. #endif /* POSIX || C23 */
  152. /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  153. that is the representation of TP in this format. */
  154. extern char *asctime (const struct tm *__tp) __THROW;
  155. /* Equivalent to `asctime (localtime (timer))'. */
  156. #ifndef __USE_TIME64_REDIRECTS
  157. extern char *ctime (const time_t *__timer) __THROW;
  158. #else
  159. # ifdef __REDIRECT_NTH
  160. extern char *__REDIRECT_NTH (ctime, (const time_t *__timer), __ctime64);
  161. # else
  162. # define ctime __ctime64
  163. # endif
  164. #endif
  165. #ifdef __USE_POSIX
  166. /* Reentrant versions of the above functions. */
  167. /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  168. that is the representation of TP in this format. */
  169. extern char *asctime_r (const struct tm *__restrict __tp,
  170. char *__restrict __buf) __THROW;
  171. /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */
  172. #ifndef __USE_TIME64_REDIRECTS
  173. extern char *ctime_r (const time_t *__restrict __timer,
  174. char *__restrict __buf) __THROW;
  175. #else
  176. # ifdef __REDIRECT_NTH
  177. extern char *__REDIRECT_NTH (ctime_r, (const time_t *__restrict __timer,
  178. char *__restrict __buf), __ctime64_r);
  179. # else
  180. # define ctime_r __ctime64_r
  181. # endif
  182. #endif
  183. #endif /* POSIX */
  184. /* Defined in localtime.c. */
  185. extern char *__tzname[2]; /* Current time zone abbreviations. */
  186. extern int __daylight; /* If daylight-saving time is ever in use. */
  187. extern long int __timezone; /* Seconds west of UTC. */
  188. #ifdef __USE_POSIX
  189. /* Same as above. */
  190. extern char *tzname[2];
  191. /* Set time conversion information from the TZ environment variable.
  192. If TZ is not defined, a locale-dependent default is used. */
  193. extern void tzset (void) __THROW;
  194. #endif
  195. #if defined __USE_MISC || defined __USE_XOPEN
  196. extern int daylight;
  197. extern long int timezone;
  198. #endif
  199. /* Nonzero if YEAR is a leap year (every 4 years,
  200. except every 100th isn't, and every 400th is). */
  201. #define __isleap(year) \
  202. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  203. #if defined __USE_MISC || __GLIBC_USE (ISOC23)
  204. # ifndef __USE_TIME64_REDIRECTS
  205. /* Like `mktime', but for TP represents Universal Time, not local time. */
  206. extern time_t timegm (struct tm *__tp) __THROW;
  207. # else
  208. # ifdef __REDIRECT_NTH
  209. extern time_t __REDIRECT_NTH (timegm, (struct tm *__tp), __timegm64);
  210. # else
  211. # define timegm __timegm64
  212. # endif
  213. # endif
  214. #endif
  215. #ifdef __USE_MISC
  216. /* Miscellaneous functions many Unices inherited from the public domain
  217. localtime package. These are included only for compatibility. */
  218. #ifndef __USE_TIME64_REDIRECTS
  219. /* Another name for `mktime'. */
  220. extern time_t timelocal (struct tm *__tp) __THROW;
  221. #else
  222. # ifdef __REDIRECT_NTH
  223. extern time_t __REDIRECT_NTH (timelocal, (struct tm *__tp), __mktime64);
  224. # endif
  225. #endif
  226. /* Return the number of days in YEAR. */
  227. extern int dysize (int __year) __THROW __attribute__ ((__const__));
  228. #endif
  229. #ifdef __USE_POSIX199309
  230. # ifndef __USE_TIME64_REDIRECTS
  231. /* Pause execution for a number of nanoseconds.
  232. This function is a cancellation point and therefore not marked with
  233. __THROW. */
  234. extern int nanosleep (const struct timespec *__requested_time,
  235. struct timespec *__remaining);
  236. /* Get resolution of clock CLOCK_ID. */
  237. extern int clock_getres (clockid_t __clock_id, struct timespec *__res) __THROW;
  238. /* Get current value of clock CLOCK_ID and store it in TP. */
  239. extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp)
  240. __THROW __nonnull((2));
  241. /* Set clock CLOCK_ID to value TP. */
  242. extern int clock_settime (clockid_t __clock_id, const struct timespec *__tp)
  243. __THROW __nonnull((2));
  244. # else
  245. # ifdef __REDIRECT
  246. extern int __REDIRECT (nanosleep, (const struct timespec *__requested_time,
  247. struct timespec *__remaining),
  248. __nanosleep64);
  249. extern int __REDIRECT_NTH (clock_getres, (clockid_t __clock_id,
  250. struct timespec *__res),
  251. __clock_getres64);
  252. extern int __REDIRECT_NTH (clock_gettime, (clockid_t __clock_id, struct
  253. timespec *__tp), __clock_gettime64)
  254. __nonnull((2));
  255. extern int __REDIRECT_NTH (clock_settime, (clockid_t __clock_id, const struct
  256. timespec *__tp), __clock_settime64)
  257. __nonnull((2));
  258. # else
  259. # define nanosleep __nanosleep64
  260. # define clock_getres __clock_getres64
  261. # define clock_gettime __clock_gettime64
  262. # define clock_settime __clock_settime64
  263. # endif
  264. # endif
  265. # ifdef __USE_XOPEN2K
  266. /* High-resolution sleep with the specified clock.
  267. This function is a cancellation point and therefore not marked with
  268. __THROW. */
  269. # ifndef __USE_TIME64_REDIRECTS
  270. extern int clock_nanosleep (clockid_t __clock_id, int __flags,
  271. const struct timespec *__req,
  272. struct timespec *__rem);
  273. # else
  274. # ifdef __REDIRECT
  275. extern int __REDIRECT (clock_nanosleep, (clockid_t __clock_id, int __flags,
  276. const struct timespec *__req,
  277. struct timespec *__rem),
  278. __clock_nanosleep_time64);
  279. # else
  280. # define clock_nanosleep __clock_nanosleep_time64
  281. # endif
  282. # endif
  283. /* Return clock ID for CPU-time clock. */
  284. extern int clock_getcpuclockid (pid_t __pid, clockid_t *__clock_id) __THROW;
  285. # endif
  286. /* Create new per-process timer using CLOCK_ID. */
  287. extern int timer_create (clockid_t __clock_id,
  288. struct sigevent *__restrict __evp,
  289. timer_t *__restrict __timerid) __THROW;
  290. /* Delete timer TIMERID. */
  291. extern int timer_delete (timer_t __timerid) __THROW;
  292. /* Set timer TIMERID to VALUE, returning old value in OVALUE. */
  293. # ifndef __USE_TIME64_REDIRECTS
  294. extern int timer_settime (timer_t __timerid, int __flags,
  295. const struct itimerspec *__restrict __value,
  296. struct itimerspec *__restrict __ovalue) __THROW;
  297. /* Get current value of timer TIMERID and store it in VALUE. */
  298. extern int timer_gettime (timer_t __timerid, struct itimerspec *__value)
  299. __THROW;
  300. # else
  301. # ifdef __REDIRECT_NTH
  302. extern int __REDIRECT_NTH (timer_settime, (timer_t __timerid, int __flags,
  303. const struct itimerspec *__restrict __value,
  304. struct itimerspec *__restrict __ovalue),
  305. __timer_settime64);
  306. extern int __REDIRECT_NTH (timer_gettime, (timer_t __timerid,
  307. struct itimerspec *__value),
  308. __timer_gettime64);
  309. # else
  310. # define timer_settime __timer_settime64
  311. # define timer_gettime __timer_gettime64
  312. # endif
  313. # endif
  314. /* Get expiration overrun for timer TIMERID. */
  315. extern int timer_getoverrun (timer_t __timerid) __THROW;
  316. #endif
  317. #ifdef __USE_ISOC11
  318. # ifndef __USE_TIME64_REDIRECTS
  319. /* Set TS to calendar time based in time base BASE. */
  320. extern int timespec_get (struct timespec *__ts, int __base)
  321. __THROW __nonnull ((1));
  322. # else
  323. # ifdef __REDIRECT_NTH
  324. extern int __REDIRECT_NTH (timespec_get, (struct timespec *__ts, int __base),
  325. __timespec_get64) __nonnull ((1));
  326. # else
  327. # define timespec_get __timespec_get64
  328. # endif
  329. # endif
  330. #endif
  331. #if __GLIBC_USE (ISOC23)
  332. # ifndef __USE_TIME64_REDIRECTS
  333. /* Set TS to resolution of time base BASE. */
  334. extern int timespec_getres (struct timespec *__ts, int __base)
  335. __THROW;
  336. # else
  337. # ifdef __REDIRECT_NTH
  338. extern int __REDIRECT_NTH (timespec_getres, (struct timespec *__ts,
  339. int __base),
  340. __timespec_getres64);
  341. # else
  342. # define timespec_getres __timespec_getres64
  343. # endif
  344. # endif
  345. #endif
  346. #ifdef __USE_XOPEN_EXTENDED
  347. /* Set to one of the following values to indicate an error.
  348. 1 the DATEMSK environment variable is null or undefined,
  349. 2 the template file cannot be opened for reading,
  350. 3 failed to get file status information,
  351. 4 the template file is not a regular file,
  352. 5 an error is encountered while reading the template file,
  353. 6 memory allication failed (not enough memory available),
  354. 7 there is no line in the template that matches the input,
  355. 8 invalid input specification Example: February 31 or a time is
  356. specified that can not be represented in a time_t (representing
  357. the time in seconds since 00:00:00 UTC, January 1, 1970) */
  358. extern int getdate_err;
  359. /* Parse the given string as a date specification and return a value
  360. representing the value. The templates from the file identified by
  361. the environment variable DATEMSK are used. In case of an error
  362. `getdate_err' is set.
  363. This function is a possible cancellation point and therefore not
  364. marked with __THROW. */
  365. extern struct tm *getdate (const char *__string);
  366. #endif
  367. #ifdef __USE_GNU
  368. /* Since `getdate' is not reentrant because of the use of `getdate_err'
  369. and the static buffer to return the result in, we provide a thread-safe
  370. variant. The functionality is the same. The result is returned in
  371. the buffer pointed to by RESBUFP and in case of an error the return
  372. value is != 0 with the same values as given above for `getdate_err'.
  373. This function is not part of POSIX and therefore no official
  374. cancellation point. But due to similarity with an POSIX interface
  375. or due to the implementation it is a cancellation point and
  376. therefore not marked with __THROW. */
  377. extern int getdate_r (const char *__restrict __string,
  378. struct tm *__restrict __resbufp);
  379. #endif
  380. __END_DECLS
  381. #endif /* time.h. */