1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2021 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
19 @PRAGMA_SYSTEM_HEADER@
23 /* Don't get in the way of glibc when it includes time.h merely to
24 declare a few standard symbols, rather than to declare all the
25 symbols. (However, skip this for MinGW as it treats __need_time_t
26 incompatibly.) Also, Solaris 8 <time.h> eventually includes itself
27 recursively; if that is happening, just include the system <time.h>
28 without adding our own declarations. */
29 #if (((defined __need_time_t || defined __need_clock_t \
30 || defined __need_timespec) \
31 && !defined __MINGW32__) \
32 || defined _@GUARD_PREFIX@_TIME_H)
34 # @INCLUDE_NEXT@ @NEXT_TIME_H@
38 # define _@GUARD_PREFIX@_TIME_H
40 /* mingw's <time.h> provides the functions asctime_r, ctime_r, gmtime_r,
41 localtime_r only if <unistd.h> or <pthread.h> has been included before. */
42 # if defined __MINGW32__
46 # @INCLUDE_NEXT@ @NEXT_TIME_H@
48 /* NetBSD 5.0 mis-defines NULL. */
51 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
53 /* The definition of _GL_ARG_NONNULL is copied here. */
55 /* The definition of _GL_WARN_ON_USE is copied here. */
57 /* Some systems don't define struct timespec (e.g., AIX 4.1).
58 Or they define it with the wrong member names or define it in <sys/time.h>
59 (e.g., FreeBSD circa 1997). Stock Mingw prior to 3.0 does not define it,
60 but the pthreads-win32 library defines it in <pthread.h>. */
61 # if ! @TIME_H_DEFINES_STRUCT_TIMESPEC@
62 # if @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@
63 # include <sys/time.h>
64 # elif @PTHREAD_H_DEFINES_STRUCT_TIMESPEC@
66 # elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
74 # if !GNULIB_defined_struct_timespec
76 # define timespec rpl_timespec
82 # define GNULIB_defined_struct_timespec 1
92 # if !GNULIB_defined_struct_time_t_must_be_integral
93 /* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
94 requires time_t to be an integer type, even though C99 permits floating
95 point. We don't know of any implementation that uses floating
96 point, and it is much easier to write code that doesn't have to
97 worry about that corner case, so we force the issue. */
98 struct __time_t_must_be_integral {
99 unsigned int __floating_time_t_unsupported : (time_t) 1;
101 # define GNULIB_defined_struct_time_t_must_be_integral 1
104 /* Define TIME_UTC, a positive integer constant used for timespec_get(). */
105 # if ! @TIME_H_DEFINES_TIME_UTC@
106 # if !GNULIB_defined_TIME_UTC
108 # define GNULIB_defined_TIME_UTC 1
112 /* Set *TS to the current time, and return BASE.
113 Upon failure, return 0. */
114 # if @GNULIB_TIMESPEC_GET@
115 # if ! @HAVE_TIMESPEC_GET@
116 _GL_FUNCDECL_SYS (timespec_get, int, (struct timespec *ts, int base)
117 _GL_ARG_NONNULL ((1)));
119 _GL_CXXALIAS_SYS (timespec_get, int, (struct timespec *ts, int base));
120 _GL_CXXALIASWARN (timespec_get);
123 /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
124 return -1 and store the remaining time into RMTP. See
125 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
126 # if @GNULIB_NANOSLEEP@
127 # if @REPLACE_NANOSLEEP@
128 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
129 # define nanosleep rpl_nanosleep
131 _GL_FUNCDECL_RPL (nanosleep, int,
132 (struct timespec const *__rqtp, struct timespec *__rmtp)
133 _GL_ARG_NONNULL ((1)));
134 _GL_CXXALIAS_RPL (nanosleep, int,
135 (struct timespec const *__rqtp, struct timespec *__rmtp));
137 # if ! @HAVE_NANOSLEEP@
138 _GL_FUNCDECL_SYS (nanosleep, int,
139 (struct timespec const *__rqtp, struct timespec *__rmtp)
140 _GL_ARG_NONNULL ((1)));
142 _GL_CXXALIAS_SYS (nanosleep, int,
143 (struct timespec const *__rqtp, struct timespec *__rmtp));
145 _GL_CXXALIASWARN (nanosleep);
148 /* Initialize time conversion information. */
151 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
153 # define tzset rpl_tzset
155 _GL_FUNCDECL_RPL (tzset, void, (void));
156 _GL_CXXALIAS_RPL (tzset, void, (void));
157 # elif defined _WIN32 && !defined __CYGWIN__
158 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
160 # define tzset _tzset
162 _GL_CXXALIAS_MDA (tzset, void, (void));
164 _GL_CXXALIAS_SYS (tzset, void, (void));
166 _GL_CXXALIASWARN (tzset);
167 # elif @GNULIB_MDA_TZSET@
168 /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
169 required. In C++ with GNULIB_NAMESPACE, avoid differences between
170 platforms by defining GNULIB_NAMESPACE::tzset always. */
171 # if defined _WIN32 && !defined __CYGWIN__
172 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
174 # define tzset _tzset
176 _GL_CXXALIAS_MDA (tzset, void, (void));
178 _GL_CXXALIAS_SYS (tzset, void, (void));
180 _GL_CXXALIASWARN (tzset);
183 /* Return the 'time_t' representation of TP and normalize TP. */
185 # if @REPLACE_MKTIME@
186 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
187 # define mktime rpl_mktime
189 _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
190 _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
192 _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
195 _GL_CXXALIASWARN (mktime);
199 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
200 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
201 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
203 # if @REPLACE_LOCALTIME_R@
204 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
206 # define localtime_r rpl_localtime_r
208 _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
209 struct tm *restrict __result)
210 _GL_ARG_NONNULL ((1, 2)));
211 _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
212 struct tm *restrict __result));
214 # if ! @HAVE_DECL_LOCALTIME_R@
215 _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
216 struct tm *restrict __result)
217 _GL_ARG_NONNULL ((1, 2)));
219 _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
220 struct tm *restrict __result));
222 # if @HAVE_DECL_LOCALTIME_R@
223 _GL_CXXALIASWARN (localtime_r);
225 # if @REPLACE_LOCALTIME_R@
226 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
228 # define gmtime_r rpl_gmtime_r
230 _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
231 struct tm *restrict __result)
232 _GL_ARG_NONNULL ((1, 2)));
233 _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
234 struct tm *restrict __result));
236 # if ! @HAVE_DECL_LOCALTIME_R@
237 _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
238 struct tm *restrict __result)
239 _GL_ARG_NONNULL ((1, 2)));
241 _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
242 struct tm *restrict __result));
244 # if @HAVE_DECL_LOCALTIME_R@
245 _GL_CXXALIASWARN (gmtime_r);
249 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
250 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
251 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
252 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
253 # if @REPLACE_LOCALTIME@
254 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
256 # define localtime rpl_localtime
258 _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
259 _GL_ARG_NONNULL ((1)));
260 _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
262 _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
265 _GL_CXXALIASWARN (localtime);
269 # if 0 || @REPLACE_GMTIME@
270 # if @REPLACE_GMTIME@
271 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
273 # define gmtime rpl_gmtime
275 _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
276 _GL_ARG_NONNULL ((1)));
277 _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
279 _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
281 _GL_CXXALIASWARN (gmtime);
284 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
285 the resulting broken-down time into TM. See
286 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
287 # if @GNULIB_STRPTIME@
288 # if ! @HAVE_STRPTIME@
289 _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
290 char const *restrict __format,
291 struct tm *restrict __tm)
292 _GL_ARG_NONNULL ((1, 2, 3)));
294 _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
295 char const *restrict __format,
296 struct tm *restrict __tm));
297 _GL_CXXALIASWARN (strptime);
300 /* Convert *TP to a date and time string. See
301 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
304 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
305 # define ctime rpl_ctime
307 _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
308 _GL_ARG_NONNULL ((1)));
309 _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
311 _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
314 _GL_CXXALIASWARN (ctime);
318 /* Convert *TP to a date and time string. See
319 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
320 # if @GNULIB_STRFTIME@
321 # if @REPLACE_STRFTIME@
322 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
323 # define strftime rpl_strftime
325 _GL_FUNCDECL_RPL (strftime, size_t,
326 (char *restrict __buf, size_t __bufsize,
327 const char *restrict __fmt, const struct tm *restrict __tp)
328 _GL_ARG_NONNULL ((1, 3, 4)));
329 _GL_CXXALIAS_RPL (strftime, size_t,
330 (char *restrict __buf, size_t __bufsize,
331 const char *restrict __fmt, const struct tm *restrict __tp));
333 _GL_CXXALIAS_SYS (strftime, size_t,
334 (char *restrict __buf, size_t __bufsize,
335 const char *restrict __fmt, const struct tm *restrict __tp));
338 _GL_CXXALIASWARN (strftime);
342 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
343 /* Functions that use a first-class time zone data type, instead of
344 relying on an implicit global time zone.
345 Inspired by NetBSD. */
347 /* Represents a time zone.
348 (timezone_t) NULL stands for UTC. */
349 typedef struct tm_zone *timezone_t;
352 Returns a time zone object for the given time zone NAME. This object
353 represents the time zone that other functions would use it the TZ
354 environment variable was set to NAME.
355 If NAME is NULL, the result represents the time zone that other functions
356 would use it the TZ environment variable was unset.
357 May return NULL if NAME is invalid (this is platform dependent) or
358 upon memory allocation failure. */
359 _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
360 _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
363 Frees a time zone object.
364 The argument must have been returned by tzalloc(). */
365 _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
366 _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
368 /* localtime_rz (tz, &t, &result)
369 Converts an absolute time T to a broken-down time RESULT, assuming the
371 This function is like 'localtime_r', but relies on the argument TZ instead
372 of an implicit global time zone. */
373 _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
374 (timezone_t __tz, time_t const *restrict __timer,
375 struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
376 _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
377 (timezone_t __tz, time_t const *restrict __timer,
378 struct tm *restrict __result));
380 /* mktime_z (tz, &tm)
381 Normalizes the broken-down time TM and converts it to an absolute time,
382 assuming the time zone TZ. Returns the absolute time.
383 This function is like 'mktime', but relies on the argument TZ instead
384 of an implicit global time zone. */
385 _GL_FUNCDECL_SYS (mktime_z, time_t,
386 (timezone_t __tz, struct tm *restrict __tm)
387 _GL_ARG_NONNULL ((2)));
388 _GL_CXXALIAS_SYS (mktime_z, time_t,
389 (timezone_t __tz, struct tm *restrict __tm));
391 /* Time zone abbreviation strings (returned by 'localtime_rz' or 'mktime_z'
392 in the 'tm_zone' member of 'struct tm') are valid as long as
393 - the 'struct tm' argument is not destroyed or overwritten,
395 - the 'timezone_t' argument is not freed through tzfree(). */
399 /* Convert TM to a time_t value, assuming UTC. */
401 # if @REPLACE_TIMEGM@
402 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
404 # define timegm rpl_timegm
406 _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
407 _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
410 _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
412 _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
414 _GL_CXXALIASWARN (timegm);
417 /* Encourage applications to avoid unsafe functions that can overrun
418 buffers when given outlandish struct tm values. Portable
419 applications should use strftime (or even sprintf) instead. */
420 # if defined GNULIB_POSIXCHECK
422 _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
423 "better use strftime (or even sprintf) instead");
425 # if defined GNULIB_POSIXCHECK
427 _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
428 "better use strftime (or even sprintf) instead");
430 # if defined GNULIB_POSIXCHECK
432 _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
433 "better use strftime (or even sprintf) instead");
435 # if defined GNULIB_POSIXCHECK
437 _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
438 "better use strftime (or even sprintf) instead");