1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2022 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 /* Set *TS to the current time resolution, and return BASE.
124 Upon failure, return 0. */
125 # if @GNULIB_TIMESPEC_GETRES@
126 # if ! @HAVE_TIMESPEC_GETRES@
127 _GL_FUNCDECL_SYS (timespec_getres
, int, (struct timespec
*ts
, int base
)
128 _GL_ARG_NONNULL ((1)));
130 _GL_CXXALIAS_SYS (timespec_getres
, int, (struct timespec
*ts
, int base
));
131 _GL_CXXALIASWARN (timespec_getres
);
134 /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
135 return -1 and store the remaining time into RMTP. See
136 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
137 # if @GNULIB_NANOSLEEP@
138 # if @REPLACE_NANOSLEEP@
139 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
140 # define nanosleep rpl_nanosleep
142 _GL_FUNCDECL_RPL (nanosleep
, int,
143 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
144 _GL_ARG_NONNULL ((1)));
145 _GL_CXXALIAS_RPL (nanosleep
, int,
146 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
148 # if ! @HAVE_NANOSLEEP@
149 _GL_FUNCDECL_SYS (nanosleep
, int,
150 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
151 _GL_ARG_NONNULL ((1)));
153 _GL_CXXALIAS_SYS (nanosleep
, int,
154 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
156 _GL_CXXALIASWARN (nanosleep
);
159 /* Initialize time conversion information. */
162 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
164 # define tzset rpl_tzset
166 _GL_FUNCDECL_RPL (tzset
, void, (void));
167 _GL_CXXALIAS_RPL (tzset
, void, (void));
168 # elif defined _WIN32 && !defined __CYGWIN__
169 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
171 # define tzset _tzset
173 _GL_CXXALIAS_MDA (tzset
, void, (void));
175 _GL_CXXALIAS_SYS (tzset
, void, (void));
177 _GL_CXXALIASWARN (tzset
);
178 # elif @GNULIB_MDA_TZSET@
179 /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
180 required. In C++ with GNULIB_NAMESPACE, avoid differences between
181 platforms by defining GNULIB_NAMESPACE::tzset always. */
182 # if defined _WIN32 && !defined __CYGWIN__
183 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
185 # define tzset _tzset
187 _GL_CXXALIAS_MDA (tzset
, void, (void));
189 _GL_CXXALIAS_SYS (tzset
, void, (void));
191 _GL_CXXALIASWARN (tzset
);
194 /* Return the 'time_t' representation of TP and normalize TP. */
196 # if @REPLACE_MKTIME@
197 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
198 # define mktime rpl_mktime
200 _GL_FUNCDECL_RPL (mktime
, time_t, (struct tm
*__tp
) _GL_ARG_NONNULL ((1)));
201 _GL_CXXALIAS_RPL (mktime
, time_t, (struct tm
*__tp
));
203 _GL_CXXALIAS_SYS (mktime
, time_t, (struct tm
*__tp
));
206 _GL_CXXALIASWARN (mktime
);
210 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
211 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
212 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
214 # if @REPLACE_LOCALTIME_R@
215 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
217 # define localtime_r rpl_localtime_r
219 _GL_FUNCDECL_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
220 struct tm
*restrict __result
)
221 _GL_ARG_NONNULL ((1, 2)));
222 _GL_CXXALIAS_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
223 struct tm
*restrict __result
));
225 # if ! @HAVE_DECL_LOCALTIME_R@
226 _GL_FUNCDECL_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
227 struct tm
*restrict __result
)
228 _GL_ARG_NONNULL ((1, 2)));
230 _GL_CXXALIAS_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
231 struct tm
*restrict __result
));
233 # if @HAVE_DECL_LOCALTIME_R@
234 _GL_CXXALIASWARN (localtime_r
);
236 # if @REPLACE_LOCALTIME_R@
237 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
239 # define gmtime_r rpl_gmtime_r
241 _GL_FUNCDECL_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
242 struct tm
*restrict __result
)
243 _GL_ARG_NONNULL ((1, 2)));
244 _GL_CXXALIAS_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
245 struct tm
*restrict __result
));
247 # if ! @HAVE_DECL_LOCALTIME_R@
248 _GL_FUNCDECL_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
249 struct tm
*restrict __result
)
250 _GL_ARG_NONNULL ((1, 2)));
252 _GL_CXXALIAS_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
253 struct tm
*restrict __result
));
255 # if @HAVE_DECL_LOCALTIME_R@
256 _GL_CXXALIASWARN (gmtime_r
);
260 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
261 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
262 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
263 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
264 # if @REPLACE_LOCALTIME@
265 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
267 # define localtime rpl_localtime
269 _GL_FUNCDECL_RPL (localtime
, struct tm
*, (time_t const *__timer
)
270 _GL_ARG_NONNULL ((1)));
271 _GL_CXXALIAS_RPL (localtime
, struct tm
*, (time_t const *__timer
));
273 _GL_CXXALIAS_SYS (localtime
, struct tm
*, (time_t const *__timer
));
276 _GL_CXXALIASWARN (localtime
);
280 # if 0 || @REPLACE_GMTIME@
281 # if @REPLACE_GMTIME@
282 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
284 # define gmtime rpl_gmtime
286 _GL_FUNCDECL_RPL (gmtime
, struct tm
*, (time_t const *__timer
)
287 _GL_ARG_NONNULL ((1)));
288 _GL_CXXALIAS_RPL (gmtime
, struct tm
*, (time_t const *__timer
));
290 _GL_CXXALIAS_SYS (gmtime
, struct tm
*, (time_t const *__timer
));
292 _GL_CXXALIASWARN (gmtime
);
295 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
296 the resulting broken-down time into TM. See
297 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
298 # if @GNULIB_STRPTIME@
299 # if ! @HAVE_STRPTIME@
300 _GL_FUNCDECL_SYS (strptime
, char *, (char const *restrict __buf
,
301 char const *restrict __format
,
302 struct tm
*restrict __tm
)
303 _GL_ARG_NONNULL ((1, 2, 3)));
305 _GL_CXXALIAS_SYS (strptime
, char *, (char const *restrict __buf
,
306 char const *restrict __format
,
307 struct tm
*restrict __tm
));
308 _GL_CXXALIASWARN (strptime
);
311 /* Convert *TP to a date and time string. See
312 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
315 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
316 # define ctime rpl_ctime
318 _GL_FUNCDECL_RPL (ctime
, char *, (time_t const *__tp
)
319 _GL_ARG_NONNULL ((1)));
320 _GL_CXXALIAS_RPL (ctime
, char *, (time_t const *__tp
));
322 _GL_CXXALIAS_SYS (ctime
, char *, (time_t const *__tp
));
325 _GL_CXXALIASWARN (ctime
);
329 /* Convert *TP to a date and time string. See
330 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
331 # if @GNULIB_STRFTIME@
332 # if @REPLACE_STRFTIME@
333 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
334 # define strftime rpl_strftime
336 _GL_FUNCDECL_RPL (strftime
, size_t,
337 (char *restrict __buf
, size_t __bufsize
,
338 const char *restrict __fmt
, const struct tm
*restrict __tp
)
339 _GL_ARG_NONNULL ((1, 3, 4)));
340 _GL_CXXALIAS_RPL (strftime
, size_t,
341 (char *restrict __buf
, size_t __bufsize
,
342 const char *restrict __fmt
, const struct tm
*restrict __tp
));
344 _GL_CXXALIAS_SYS (strftime
, size_t,
345 (char *restrict __buf
, size_t __bufsize
,
346 const char *restrict __fmt
, const struct tm
*restrict __tp
));
349 _GL_CXXALIASWARN (strftime
);
353 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
354 /* Functions that use a first-class time zone data type, instead of
355 relying on an implicit global time zone.
356 Inspired by NetBSD. */
358 /* Represents a time zone.
359 (timezone_t) NULL stands for UTC. */
360 typedef struct tm_zone
*timezone_t
;
363 Returns a time zone object for the given time zone NAME. This object
364 represents the time zone that other functions would use it the TZ
365 environment variable was set to NAME.
366 If NAME is NULL, the result represents the time zone that other functions
367 would use it the TZ environment variable was unset.
368 May return NULL if NAME is invalid (this is platform dependent) or
369 upon memory allocation failure. */
370 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
371 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
374 Frees a time zone object.
375 The argument must have been returned by tzalloc(). */
376 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
377 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
379 /* localtime_rz (tz, &t, &result)
380 Converts an absolute time T to a broken-down time RESULT, assuming the
382 This function is like 'localtime_r', but relies on the argument TZ instead
383 of an implicit global time zone. */
384 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
385 (timezone_t __tz
, time_t const *restrict __timer
,
386 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
387 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
388 (timezone_t __tz
, time_t const *restrict __timer
,
389 struct tm
*restrict __result
));
391 /* mktime_z (tz, &tm)
392 Normalizes the broken-down time TM and converts it to an absolute time,
393 assuming the time zone TZ. Returns the absolute time.
394 This function is like 'mktime', but relies on the argument TZ instead
395 of an implicit global time zone. */
396 _GL_FUNCDECL_SYS (mktime_z
, time_t,
397 (timezone_t __tz
, struct tm
*restrict __tm
)
398 _GL_ARG_NONNULL ((2)));
399 _GL_CXXALIAS_SYS (mktime_z
, time_t,
400 (timezone_t __tz
, struct tm
*restrict __tm
));
402 /* Time zone abbreviation strings (returned by 'localtime_rz' or 'mktime_z'
403 in the 'tm_zone' member of 'struct tm') are valid as long as
404 - the 'struct tm' argument is not destroyed or overwritten,
406 - the 'timezone_t' argument is not freed through tzfree(). */
410 /* Convert TM to a time_t value, assuming UTC. */
412 # if @REPLACE_TIMEGM@
413 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
415 # define timegm rpl_timegm
417 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
418 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
421 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
423 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
425 _GL_CXXALIASWARN (timegm
);
428 /* Encourage applications to avoid unsafe functions that can overrun
429 buffers when given outlandish struct tm values. Portable
430 applications should use strftime (or even sprintf) instead. */
431 # if defined GNULIB_POSIXCHECK
433 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
434 "better use strftime (or even sprintf) instead");
436 # if defined GNULIB_POSIXCHECK
438 _GL_WARN_ON_USE (asctime_r
, "asctime_r can overrun buffers in some cases - "
439 "better use strftime (or even sprintf) instead");
441 # if defined GNULIB_POSIXCHECK
443 _GL_WARN_ON_USE (ctime
, "ctime can overrun buffers in some cases - "
444 "better use strftime (or even sprintf) instead");
446 # if defined GNULIB_POSIXCHECK
448 _GL_WARN_ON_USE (ctime_r
, "ctime_r can overrun buffers in some cases - "
449 "better use strftime (or even sprintf) instead");