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 typedef struct tm_zone
*timezone_t
;
344 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
345 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
346 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
347 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
348 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
349 (timezone_t __tz
, time_t const *restrict __timer
,
350 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
351 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
352 (timezone_t __tz
, time_t const *restrict __timer
,
353 struct tm
*restrict __result
));
354 _GL_FUNCDECL_SYS (mktime_z
, time_t,
355 (timezone_t __tz
, struct tm
*restrict __result
)
356 _GL_ARG_NONNULL ((2)));
357 _GL_CXXALIAS_SYS (mktime_z
, time_t,
358 (timezone_t __tz
, struct tm
*restrict __result
));
361 /* Convert TM to a time_t value, assuming UTC. */
363 # if @REPLACE_TIMEGM@
364 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
366 # define timegm rpl_timegm
368 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
369 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
372 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
374 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
376 _GL_CXXALIASWARN (timegm
);
379 /* Encourage applications to avoid unsafe functions that can overrun
380 buffers when given outlandish struct tm values. Portable
381 applications should use strftime (or even sprintf) instead. */
382 # if defined GNULIB_POSIXCHECK
384 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
385 "better use strftime (or even sprintf) instead");
387 # if defined GNULIB_POSIXCHECK
389 _GL_WARN_ON_USE (asctime_r
, "asctime_r can overrun buffers in some cases - "
390 "better use strftime (or even sprintf) instead");
392 # if defined GNULIB_POSIXCHECK
394 _GL_WARN_ON_USE (ctime
, "ctime can overrun buffers in some cases - "
395 "better use strftime (or even sprintf) instead");
397 # if defined GNULIB_POSIXCHECK
399 _GL_WARN_ON_USE (ctime_r
, "ctime_r can overrun buffers in some cases - "
400 "better use strftime (or even sprintf) instead");