time.in.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* A more-standard <time.h>.
  2. Copyright (C) 2007-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #if __GNUC__ >= 3
  14. @PRAGMA_SYSTEM_HEADER@
  15. #endif
  16. @PRAGMA_COLUMNS@
  17. /* Don't get in the way of glibc when it includes time.h merely to
  18. declare a few standard symbols, rather than to declare all the
  19. symbols. (However, skip this for MinGW as it treats __need_time_t
  20. incompatibly.) Also, Solaris 8 <time.h> eventually includes itself
  21. recursively; if that is happening, just include the system <time.h>
  22. without adding our own declarations. */
  23. #if (((defined __need_time_t || defined __need_clock_t \
  24. || defined __need_timespec) \
  25. && !defined __MINGW32__) \
  26. || defined _@GUARD_PREFIX@_TIME_H)
  27. # @INCLUDE_NEXT@ @NEXT_TIME_H@
  28. #else
  29. # define _@GUARD_PREFIX@_TIME_H
  30. /* mingw's <time.h> provides the functions asctime_r, ctime_r, gmtime_r,
  31. localtime_r only if <unistd.h> or <pthread.h> has been included before. */
  32. # if defined __MINGW32__
  33. # include <unistd.h>
  34. # endif
  35. # @INCLUDE_NEXT@ @NEXT_TIME_H@
  36. /* NetBSD 5.0 mis-defines NULL. */
  37. # include <stddef.h>
  38. /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
  39. /* The definition of _GL_ARG_NONNULL is copied here. */
  40. /* The definition of _GL_WARN_ON_USE is copied here. */
  41. /* Some systems don't define struct timespec (e.g., AIX 4.1).
  42. Or they define it with the wrong member names or define it in <sys/time.h>
  43. (e.g., FreeBSD circa 1997). Stock Mingw prior to 3.0 does not define it,
  44. but the pthreads-win32 library defines it in <pthread.h>. */
  45. # if ! @TIME_H_DEFINES_STRUCT_TIMESPEC@
  46. # if @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@
  47. # include <sys/time.h>
  48. # elif @PTHREAD_H_DEFINES_STRUCT_TIMESPEC@
  49. # include <pthread.h>
  50. # elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
  51. # include <unistd.h>
  52. # else
  53. # ifdef __cplusplus
  54. extern "C" {
  55. # endif
  56. # if !GNULIB_defined_struct_timespec
  57. # undef timespec
  58. # define timespec rpl_timespec
  59. struct timespec
  60. {
  61. time_t tv_sec;
  62. long int tv_nsec;
  63. };
  64. # define GNULIB_defined_struct_timespec 1
  65. # endif
  66. # ifdef __cplusplus
  67. }
  68. # endif
  69. # endif
  70. # endif
  71. # if !GNULIB_defined_struct_time_t_must_be_integral
  72. /* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
  73. requires time_t to be an integer type, even though C99 permits floating
  74. point. We don't know of any implementation that uses floating
  75. point, and it is much easier to write code that doesn't have to
  76. worry about that corner case, so we force the issue. */
  77. struct __time_t_must_be_integral {
  78. unsigned int __floating_time_t_unsupported : (time_t) 1;
  79. };
  80. # define GNULIB_defined_struct_time_t_must_be_integral 1
  81. # endif
  82. /* Define TIME_UTC, a positive integer constant used for timespec_get(). */
  83. # if ! @TIME_H_DEFINES_TIME_UTC@
  84. # if !GNULIB_defined_TIME_UTC
  85. # define TIME_UTC 1
  86. # define GNULIB_defined_TIME_UTC 1
  87. # endif
  88. # endif
  89. /* Set *TS to the current time, and return BASE.
  90. Upon failure, return 0. */
  91. # if @GNULIB_TIMESPEC_GET@
  92. # if ! @HAVE_TIMESPEC_GET@
  93. _GL_FUNCDECL_SYS (timespec_get, int, (struct timespec *ts, int base)
  94. _GL_ARG_NONNULL ((1)));
  95. # endif
  96. _GL_CXXALIAS_SYS (timespec_get, int, (struct timespec *ts, int base));
  97. _GL_CXXALIASWARN (timespec_get);
  98. # endif
  99. /* Set *TS to the current time resolution, and return BASE.
  100. Upon failure, return 0. */
  101. # if @GNULIB_TIMESPEC_GETRES@
  102. # if ! @HAVE_TIMESPEC_GETRES@
  103. _GL_FUNCDECL_SYS (timespec_getres, int, (struct timespec *ts, int base)
  104. _GL_ARG_NONNULL ((1)));
  105. # endif
  106. _GL_CXXALIAS_SYS (timespec_getres, int, (struct timespec *ts, int base));
  107. _GL_CXXALIASWARN (timespec_getres);
  108. # endif
  109. /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
  110. return -1 and store the remaining time into RMTP. See
  111. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
  112. # if @GNULIB_NANOSLEEP@
  113. # if @REPLACE_NANOSLEEP@
  114. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  115. # define nanosleep rpl_nanosleep
  116. # endif
  117. _GL_FUNCDECL_RPL (nanosleep, int,
  118. (struct timespec const *__rqtp, struct timespec *__rmtp)
  119. _GL_ARG_NONNULL ((1)));
  120. _GL_CXXALIAS_RPL (nanosleep, int,
  121. (struct timespec const *__rqtp, struct timespec *__rmtp));
  122. # else
  123. # if ! @HAVE_NANOSLEEP@
  124. _GL_FUNCDECL_SYS (nanosleep, int,
  125. (struct timespec const *__rqtp, struct timespec *__rmtp)
  126. _GL_ARG_NONNULL ((1)));
  127. # endif
  128. _GL_CXXALIAS_SYS (nanosleep, int,
  129. (struct timespec const *__rqtp, struct timespec *__rmtp));
  130. # endif
  131. _GL_CXXALIASWARN (nanosleep);
  132. # endif
  133. /* Initialize time conversion information. */
  134. # if @GNULIB_TZSET@
  135. # if @REPLACE_TZSET@
  136. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  137. # undef tzset
  138. # define tzset rpl_tzset
  139. # endif
  140. _GL_FUNCDECL_RPL (tzset, void, (void));
  141. _GL_CXXALIAS_RPL (tzset, void, (void));
  142. # elif defined _WIN32 && !defined __CYGWIN__
  143. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  144. # undef tzset
  145. # define tzset _tzset
  146. # endif
  147. _GL_CXXALIAS_MDA (tzset, void, (void));
  148. # else
  149. _GL_CXXALIAS_SYS (tzset, void, (void));
  150. # endif
  151. _GL_CXXALIASWARN (tzset);
  152. # elif @GNULIB_MDA_TZSET@
  153. /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
  154. required. In C++ with GNULIB_NAMESPACE, avoid differences between
  155. platforms by defining GNULIB_NAMESPACE::tzset always. */
  156. # if defined _WIN32 && !defined __CYGWIN__
  157. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  158. # undef tzset
  159. # define tzset _tzset
  160. # endif
  161. _GL_CXXALIAS_MDA (tzset, void, (void));
  162. # else
  163. _GL_CXXALIAS_SYS (tzset, void, (void));
  164. # endif
  165. _GL_CXXALIASWARN (tzset);
  166. # endif
  167. /* Return the 'time_t' representation of TP and normalize TP. */
  168. # if @GNULIB_MKTIME@
  169. # if @REPLACE_MKTIME@
  170. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  171. # define mktime rpl_mktime
  172. # endif
  173. _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
  174. _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
  175. # else
  176. _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
  177. # endif
  178. # if __GLIBC__ >= 2
  179. _GL_CXXALIASWARN (mktime);
  180. # endif
  181. # endif
  182. /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
  183. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
  184. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
  185. # if @GNULIB_TIME_R@
  186. # if @REPLACE_LOCALTIME_R@
  187. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  188. # undef localtime_r
  189. # define localtime_r rpl_localtime_r
  190. # endif
  191. _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
  192. struct tm *restrict __result)
  193. _GL_ARG_NONNULL ((1, 2)));
  194. _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
  195. struct tm *restrict __result));
  196. # else
  197. # if ! @HAVE_DECL_LOCALTIME_R@
  198. _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
  199. struct tm *restrict __result)
  200. _GL_ARG_NONNULL ((1, 2)));
  201. # endif
  202. _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
  203. struct tm *restrict __result));
  204. # endif
  205. # if @HAVE_DECL_LOCALTIME_R@
  206. _GL_CXXALIASWARN (localtime_r);
  207. # endif
  208. # if @REPLACE_LOCALTIME_R@
  209. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  210. # undef gmtime_r
  211. # define gmtime_r rpl_gmtime_r
  212. # endif
  213. _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
  214. struct tm *restrict __result)
  215. _GL_ARG_NONNULL ((1, 2)));
  216. _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
  217. struct tm *restrict __result));
  218. # else
  219. # if ! @HAVE_DECL_LOCALTIME_R@
  220. _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
  221. struct tm *restrict __result)
  222. _GL_ARG_NONNULL ((1, 2)));
  223. # endif
  224. _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
  225. struct tm *restrict __result));
  226. # endif
  227. # if @HAVE_DECL_LOCALTIME_R@
  228. _GL_CXXALIASWARN (gmtime_r);
  229. # endif
  230. # endif
  231. /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
  232. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
  233. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
  234. # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
  235. # if @REPLACE_LOCALTIME@
  236. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  237. # undef localtime
  238. # define localtime rpl_localtime
  239. # endif
  240. _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
  241. _GL_ARG_NONNULL ((1)));
  242. _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
  243. # else
  244. _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
  245. # endif
  246. # if __GLIBC__ >= 2
  247. _GL_CXXALIASWARN (localtime);
  248. # endif
  249. # endif
  250. # if 0 || @REPLACE_GMTIME@
  251. # if @REPLACE_GMTIME@
  252. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  253. # undef gmtime
  254. # define gmtime rpl_gmtime
  255. # endif
  256. _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
  257. _GL_ARG_NONNULL ((1)));
  258. _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
  259. # else
  260. _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
  261. # endif
  262. _GL_CXXALIASWARN (gmtime);
  263. # endif
  264. /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
  265. the resulting broken-down time into TM. See
  266. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
  267. # if @GNULIB_STRPTIME@
  268. # if ! @HAVE_STRPTIME@
  269. _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
  270. char const *restrict __format,
  271. struct tm *restrict __tm)
  272. _GL_ARG_NONNULL ((1, 2, 3)));
  273. # endif
  274. _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
  275. char const *restrict __format,
  276. struct tm *restrict __tm));
  277. _GL_CXXALIASWARN (strptime);
  278. # endif
  279. /* Convert *TP to a date and time string. See
  280. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
  281. # if @GNULIB_CTIME@
  282. # if @REPLACE_CTIME@
  283. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  284. # define ctime rpl_ctime
  285. # endif
  286. _GL_ATTRIBUTE_DEPRECATED
  287. _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
  288. _GL_ARG_NONNULL ((1)));
  289. _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
  290. # else
  291. _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
  292. # endif
  293. # if __GLIBC__ >= 2
  294. _GL_CXXALIASWARN (ctime);
  295. # endif
  296. # endif
  297. /* Convert *TP to a date and time string. See
  298. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
  299. # if @GNULIB_STRFTIME@
  300. # if @REPLACE_STRFTIME@
  301. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  302. # define strftime rpl_strftime
  303. # endif
  304. _GL_FUNCDECL_RPL (strftime, size_t,
  305. (char *restrict __buf, size_t __bufsize,
  306. const char *restrict __fmt, const struct tm *restrict __tp)
  307. _GL_ARG_NONNULL ((1, 3, 4)));
  308. _GL_CXXALIAS_RPL (strftime, size_t,
  309. (char *restrict __buf, size_t __bufsize,
  310. const char *restrict __fmt, const struct tm *restrict __tp));
  311. # else
  312. _GL_CXXALIAS_SYS (strftime, size_t,
  313. (char *restrict __buf, size_t __bufsize,
  314. const char *restrict __fmt, const struct tm *restrict __tp));
  315. # endif
  316. # if __GLIBC__ >= 2
  317. _GL_CXXALIASWARN (strftime);
  318. # endif
  319. # endif
  320. # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
  321. /* Functions that use a first-class time zone data type, instead of
  322. relying on an implicit global time zone.
  323. Inspired by NetBSD. */
  324. /* Represents a time zone.
  325. (timezone_t) NULL stands for UTC. */
  326. typedef struct tm_zone *timezone_t;
  327. /* tzalloc (name)
  328. Returns a time zone object for the given time zone NAME. This object
  329. represents the time zone that other functions would use it the TZ
  330. environment variable was set to NAME.
  331. If NAME is NULL, the result represents the time zone that other functions
  332. would use it the TZ environment variable was unset.
  333. May return NULL if NAME is invalid (this is platform dependent) or
  334. upon memory allocation failure. */
  335. _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
  336. _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
  337. /* tzfree (tz)
  338. Frees a time zone object.
  339. The argument must have been returned by tzalloc(). */
  340. _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
  341. _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
  342. /* localtime_rz (tz, &t, &result)
  343. Converts an absolute time T to a broken-down time RESULT, assuming the
  344. time zone TZ.
  345. This function is like 'localtime_r', but relies on the argument TZ instead
  346. of an implicit global time zone. */
  347. _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
  348. (timezone_t __tz, time_t const *restrict __timer,
  349. struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
  350. _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
  351. (timezone_t __tz, time_t const *restrict __timer,
  352. struct tm *restrict __result));
  353. /* mktime_z (tz, &tm)
  354. Normalizes the broken-down time TM and converts it to an absolute time,
  355. assuming the time zone TZ. Returns the absolute time.
  356. This function is like 'mktime', but relies on the argument TZ instead
  357. of an implicit global time zone. */
  358. _GL_FUNCDECL_SYS (mktime_z, time_t,
  359. (timezone_t __tz, struct tm *restrict __tm)
  360. _GL_ARG_NONNULL ((2)));
  361. _GL_CXXALIAS_SYS (mktime_z, time_t,
  362. (timezone_t __tz, struct tm *restrict __tm));
  363. /* Time zone abbreviation strings (returned by 'localtime_rz' or 'mktime_z'
  364. in the 'tm_zone' member of 'struct tm') are valid as long as
  365. - the 'struct tm' argument is not destroyed or overwritten,
  366. and
  367. - the 'timezone_t' argument is not freed through tzfree(). */
  368. # endif
  369. /* Convert TM to a time_t value, assuming UTC. */
  370. # if @GNULIB_TIMEGM@
  371. # if @REPLACE_TIMEGM@
  372. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  373. # undef timegm
  374. # define timegm rpl_timegm
  375. # endif
  376. _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
  377. _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
  378. # else
  379. # if ! @HAVE_TIMEGM@
  380. _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
  381. # endif
  382. _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
  383. # endif
  384. _GL_CXXALIASWARN (timegm);
  385. # endif
  386. /* Encourage applications to avoid unsafe functions that can overrun
  387. buffers when given outlandish struct tm values. Portable
  388. applications should use strftime (or even sprintf) instead. */
  389. # if defined GNULIB_POSIXCHECK
  390. # undef asctime
  391. _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
  392. "better use strftime (or even sprintf) instead");
  393. # endif
  394. # if defined GNULIB_POSIXCHECK
  395. # undef asctime_r
  396. # if HAVE_RAW_DECL_ASCTIME_R
  397. _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
  398. "better use strftime (or even sprintf) instead");
  399. # endif
  400. # endif
  401. # if defined GNULIB_POSIXCHECK
  402. # undef ctime
  403. _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
  404. "better use strftime (or even sprintf) instead");
  405. # endif
  406. # if defined GNULIB_POSIXCHECK
  407. # undef ctime_r
  408. # if HAVE_RAW_DECL_CTIME_R
  409. _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
  410. "better use strftime (or even sprintf) instead");
  411. # endif
  412. # endif
  413. #endif