time.in.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* A more-standard <time.h>.
  2. Copyright (C) 2007-2021 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU 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. /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
  100. return -1 and store the remaining time into RMTP. See
  101. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
  102. # if @GNULIB_NANOSLEEP@
  103. # if @REPLACE_NANOSLEEP@
  104. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  105. # define nanosleep rpl_nanosleep
  106. # endif
  107. _GL_FUNCDECL_RPL (nanosleep, int,
  108. (struct timespec const *__rqtp, struct timespec *__rmtp)
  109. _GL_ARG_NONNULL ((1)));
  110. _GL_CXXALIAS_RPL (nanosleep, int,
  111. (struct timespec const *__rqtp, struct timespec *__rmtp));
  112. # else
  113. # if ! @HAVE_NANOSLEEP@
  114. _GL_FUNCDECL_SYS (nanosleep, int,
  115. (struct timespec const *__rqtp, struct timespec *__rmtp)
  116. _GL_ARG_NONNULL ((1)));
  117. # endif
  118. _GL_CXXALIAS_SYS (nanosleep, int,
  119. (struct timespec const *__rqtp, struct timespec *__rmtp));
  120. # endif
  121. _GL_CXXALIASWARN (nanosleep);
  122. # endif
  123. /* Initialize time conversion information. */
  124. # if @GNULIB_TZSET@
  125. # if @REPLACE_TZSET@
  126. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  127. # undef tzset
  128. # define tzset rpl_tzset
  129. # endif
  130. _GL_FUNCDECL_RPL (tzset, void, (void));
  131. _GL_CXXALIAS_RPL (tzset, void, (void));
  132. # elif defined _WIN32 && !defined __CYGWIN__
  133. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  134. # undef tzset
  135. # define tzset _tzset
  136. # endif
  137. _GL_CXXALIAS_MDA (tzset, void, (void));
  138. # else
  139. _GL_CXXALIAS_SYS (tzset, void, (void));
  140. # endif
  141. _GL_CXXALIASWARN (tzset);
  142. # elif @GNULIB_MDA_TZSET@
  143. /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
  144. required. In C++ with GNULIB_NAMESPACE, avoid differences between
  145. platforms by defining GNULIB_NAMESPACE::tzset always. */
  146. # if defined _WIN32 && !defined __CYGWIN__
  147. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  148. # undef tzset
  149. # define tzset _tzset
  150. # endif
  151. _GL_CXXALIAS_MDA (tzset, void, (void));
  152. # else
  153. _GL_CXXALIAS_SYS (tzset, void, (void));
  154. # endif
  155. _GL_CXXALIASWARN (tzset);
  156. # endif
  157. /* Return the 'time_t' representation of TP and normalize TP. */
  158. # if @GNULIB_MKTIME@
  159. # if @REPLACE_MKTIME@
  160. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  161. # define mktime rpl_mktime
  162. # endif
  163. _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
  164. _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
  165. # else
  166. _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
  167. # endif
  168. # if __GLIBC__ >= 2
  169. _GL_CXXALIASWARN (mktime);
  170. # endif
  171. # endif
  172. /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
  173. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
  174. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
  175. # if @GNULIB_TIME_R@
  176. # if @REPLACE_LOCALTIME_R@
  177. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  178. # undef localtime_r
  179. # define localtime_r rpl_localtime_r
  180. # endif
  181. _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
  182. struct tm *restrict __result)
  183. _GL_ARG_NONNULL ((1, 2)));
  184. _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
  185. struct tm *restrict __result));
  186. # else
  187. # if ! @HAVE_DECL_LOCALTIME_R@
  188. _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
  189. struct tm *restrict __result)
  190. _GL_ARG_NONNULL ((1, 2)));
  191. # endif
  192. _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
  193. struct tm *restrict __result));
  194. # endif
  195. # if @HAVE_DECL_LOCALTIME_R@
  196. _GL_CXXALIASWARN (localtime_r);
  197. # endif
  198. # if @REPLACE_LOCALTIME_R@
  199. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  200. # undef gmtime_r
  201. # define gmtime_r rpl_gmtime_r
  202. # endif
  203. _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
  204. struct tm *restrict __result)
  205. _GL_ARG_NONNULL ((1, 2)));
  206. _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
  207. struct tm *restrict __result));
  208. # else
  209. # if ! @HAVE_DECL_LOCALTIME_R@
  210. _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
  211. struct tm *restrict __result)
  212. _GL_ARG_NONNULL ((1, 2)));
  213. # endif
  214. _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
  215. struct tm *restrict __result));
  216. # endif
  217. # if @HAVE_DECL_LOCALTIME_R@
  218. _GL_CXXALIASWARN (gmtime_r);
  219. # endif
  220. # endif
  221. /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
  222. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
  223. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
  224. # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
  225. # if @REPLACE_LOCALTIME@
  226. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  227. # undef localtime
  228. # define localtime rpl_localtime
  229. # endif
  230. _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
  231. _GL_ARG_NONNULL ((1)));
  232. _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
  233. # else
  234. _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
  235. # endif
  236. # if __GLIBC__ >= 2
  237. _GL_CXXALIASWARN (localtime);
  238. # endif
  239. # endif
  240. # if 0 || @REPLACE_GMTIME@
  241. # if @REPLACE_GMTIME@
  242. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  243. # undef gmtime
  244. # define gmtime rpl_gmtime
  245. # endif
  246. _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
  247. _GL_ARG_NONNULL ((1)));
  248. _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
  249. # else
  250. _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
  251. # endif
  252. _GL_CXXALIASWARN (gmtime);
  253. # endif
  254. /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
  255. the resulting broken-down time into TM. See
  256. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
  257. # if @GNULIB_STRPTIME@
  258. # if ! @HAVE_STRPTIME@
  259. _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
  260. char const *restrict __format,
  261. struct tm *restrict __tm)
  262. _GL_ARG_NONNULL ((1, 2, 3)));
  263. # endif
  264. _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
  265. char const *restrict __format,
  266. struct tm *restrict __tm));
  267. _GL_CXXALIASWARN (strptime);
  268. # endif
  269. /* Convert *TP to a date and time string. See
  270. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
  271. # if @GNULIB_CTIME@
  272. # if @REPLACE_CTIME@
  273. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  274. # define ctime rpl_ctime
  275. # endif
  276. _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
  277. _GL_ARG_NONNULL ((1)));
  278. _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
  279. # else
  280. _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
  281. # endif
  282. # if __GLIBC__ >= 2
  283. _GL_CXXALIASWARN (ctime);
  284. # endif
  285. # endif
  286. /* Convert *TP to a date and time string. See
  287. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
  288. # if @GNULIB_STRFTIME@
  289. # if @REPLACE_STRFTIME@
  290. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  291. # define strftime rpl_strftime
  292. # endif
  293. _GL_FUNCDECL_RPL (strftime, size_t,
  294. (char *restrict __buf, size_t __bufsize,
  295. const char *restrict __fmt, const struct tm *restrict __tp)
  296. _GL_ARG_NONNULL ((1, 3, 4)));
  297. _GL_CXXALIAS_RPL (strftime, size_t,
  298. (char *restrict __buf, size_t __bufsize,
  299. const char *restrict __fmt, const struct tm *restrict __tp));
  300. # else
  301. _GL_CXXALIAS_SYS (strftime, size_t,
  302. (char *restrict __buf, size_t __bufsize,
  303. const char *restrict __fmt, const struct tm *restrict __tp));
  304. # endif
  305. # if __GLIBC__ >= 2
  306. _GL_CXXALIASWARN (strftime);
  307. # endif
  308. # endif
  309. # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
  310. typedef struct tm_zone *timezone_t;
  311. _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
  312. _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
  313. _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
  314. _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
  315. _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
  316. (timezone_t __tz, time_t const *restrict __timer,
  317. struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
  318. _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
  319. (timezone_t __tz, time_t const *restrict __timer,
  320. struct tm *restrict __result));
  321. _GL_FUNCDECL_SYS (mktime_z, time_t,
  322. (timezone_t __tz, struct tm *restrict __result)
  323. _GL_ARG_NONNULL ((2)));
  324. _GL_CXXALIAS_SYS (mktime_z, time_t,
  325. (timezone_t __tz, struct tm *restrict __result));
  326. # endif
  327. /* Convert TM to a time_t value, assuming UTC. */
  328. # if @GNULIB_TIMEGM@
  329. # if @REPLACE_TIMEGM@
  330. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  331. # undef timegm
  332. # define timegm rpl_timegm
  333. # endif
  334. _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
  335. _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
  336. # else
  337. # if ! @HAVE_TIMEGM@
  338. _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
  339. # endif
  340. _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
  341. # endif
  342. _GL_CXXALIASWARN (timegm);
  343. # endif
  344. /* Encourage applications to avoid unsafe functions that can overrun
  345. buffers when given outlandish struct tm values. Portable
  346. applications should use strftime (or even sprintf) instead. */
  347. # if defined GNULIB_POSIXCHECK
  348. # undef asctime
  349. _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
  350. "better use strftime (or even sprintf) instead");
  351. # endif
  352. # if defined GNULIB_POSIXCHECK
  353. # undef asctime_r
  354. _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
  355. "better use strftime (or even sprintf) instead");
  356. # endif
  357. # if defined GNULIB_POSIXCHECK
  358. # undef ctime
  359. _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
  360. "better use strftime (or even sprintf) instead");
  361. # endif
  362. # if defined GNULIB_POSIXCHECK
  363. # undef ctime_r
  364. _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
  365. "better use strftime (or even sprintf) instead");
  366. # endif
  367. #endif