1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2020 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 2, 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 /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
105 return -1 and store the remaining time into RMTP. See
106 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
107 # if @GNULIB_NANOSLEEP@
108 # if @REPLACE_NANOSLEEP@
109 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
110 # define nanosleep rpl_nanosleep
112 _GL_FUNCDECL_RPL (nanosleep
, int,
113 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
114 _GL_ARG_NONNULL ((1)));
115 _GL_CXXALIAS_RPL (nanosleep
, int,
116 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
118 # if ! @HAVE_NANOSLEEP@
119 _GL_FUNCDECL_SYS (nanosleep
, int,
120 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
121 _GL_ARG_NONNULL ((1)));
123 _GL_CXXALIAS_SYS (nanosleep
, int,
124 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
126 _GL_CXXALIASWARN (nanosleep
);
129 /* Initialize time conversion information. */
132 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
134 # define tzset rpl_tzset
136 _GL_FUNCDECL_RPL (tzset
, void, (void));
137 _GL_CXXALIAS_RPL (tzset
, void, (void));
138 # elif defined _WIN32 && !defined __CYGWIN__
139 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
141 # define tzset _tzset
143 _GL_CXXALIAS_MDA (tzset
, void, (void));
145 _GL_CXXALIAS_SYS (tzset
, void, (void));
147 _GL_CXXALIASWARN (tzset
);
148 # elif @GNULIB_MDA_TZSET@
149 /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
150 required. In C++ with GNULIB_NAMESPACE, avoid differences between
151 platforms by defining GNULIB_NAMESPACE::tzset always. */
152 # if defined _WIN32 && !defined __CYGWIN__
153 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
155 # define tzset _tzset
157 _GL_CXXALIAS_MDA (tzset
, void, (void));
159 _GL_CXXALIAS_SYS (tzset
, void, (void));
161 _GL_CXXALIASWARN (tzset
);
164 /* Return the 'time_t' representation of TP and normalize TP. */
166 # if @REPLACE_MKTIME@
167 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
168 # define mktime rpl_mktime
170 _GL_FUNCDECL_RPL (mktime
, time_t, (struct tm
*__tp
) _GL_ARG_NONNULL ((1)));
171 _GL_CXXALIAS_RPL (mktime
, time_t, (struct tm
*__tp
));
173 _GL_CXXALIAS_SYS (mktime
, time_t, (struct tm
*__tp
));
176 _GL_CXXALIASWARN (mktime
);
180 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
181 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
182 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
184 # if @REPLACE_LOCALTIME_R@
185 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
187 # define localtime_r rpl_localtime_r
189 _GL_FUNCDECL_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
190 struct tm
*restrict __result
)
191 _GL_ARG_NONNULL ((1, 2)));
192 _GL_CXXALIAS_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
193 struct tm
*restrict __result
));
195 # if ! @HAVE_DECL_LOCALTIME_R@
196 _GL_FUNCDECL_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
197 struct tm
*restrict __result
)
198 _GL_ARG_NONNULL ((1, 2)));
200 _GL_CXXALIAS_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
201 struct tm
*restrict __result
));
203 # if @HAVE_DECL_LOCALTIME_R@
204 _GL_CXXALIASWARN (localtime_r
);
206 # if @REPLACE_LOCALTIME_R@
207 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
209 # define gmtime_r rpl_gmtime_r
211 _GL_FUNCDECL_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
212 struct tm
*restrict __result
)
213 _GL_ARG_NONNULL ((1, 2)));
214 _GL_CXXALIAS_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
215 struct tm
*restrict __result
));
217 # if ! @HAVE_DECL_LOCALTIME_R@
218 _GL_FUNCDECL_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
219 struct tm
*restrict __result
)
220 _GL_ARG_NONNULL ((1, 2)));
222 _GL_CXXALIAS_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
223 struct tm
*restrict __result
));
225 # if @HAVE_DECL_LOCALTIME_R@
226 _GL_CXXALIASWARN (gmtime_r
);
230 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
231 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
232 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
233 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
234 # if @REPLACE_LOCALTIME@
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define localtime rpl_localtime
239 _GL_FUNCDECL_RPL (localtime
, struct tm
*, (time_t const *__timer
)
240 _GL_ARG_NONNULL ((1)));
241 _GL_CXXALIAS_RPL (localtime
, struct tm
*, (time_t const *__timer
));
243 _GL_CXXALIAS_SYS (localtime
, struct tm
*, (time_t const *__timer
));
246 _GL_CXXALIASWARN (localtime
);
250 # if 0 || @REPLACE_GMTIME@
251 # if @REPLACE_GMTIME@
252 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
254 # define gmtime rpl_gmtime
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
));
260 _GL_CXXALIAS_SYS (gmtime
, struct tm
*, (time_t const *__timer
));
262 _GL_CXXALIASWARN (gmtime
);
265 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
266 the resulting broken-down time into TM. See
267 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
268 # if @GNULIB_STRPTIME@
269 # if ! @HAVE_STRPTIME@
270 _GL_FUNCDECL_SYS (strptime
, char *, (char const *restrict __buf
,
271 char const *restrict __format
,
272 struct tm
*restrict __tm
)
273 _GL_ARG_NONNULL ((1, 2, 3)));
275 _GL_CXXALIAS_SYS (strptime
, char *, (char const *restrict __buf
,
276 char const *restrict __format
,
277 struct tm
*restrict __tm
));
278 _GL_CXXALIASWARN (strptime
);
281 /* Convert *TP to a date and time string. See
282 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
285 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
286 # define ctime rpl_ctime
288 _GL_FUNCDECL_RPL (ctime
, char *, (time_t const *__tp
)
289 _GL_ARG_NONNULL ((1)));
290 _GL_CXXALIAS_RPL (ctime
, char *, (time_t const *__tp
));
292 _GL_CXXALIAS_SYS (ctime
, char *, (time_t const *__tp
));
295 _GL_CXXALIASWARN (ctime
);
299 /* Convert *TP to a date and time string. See
300 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
301 # if @GNULIB_STRFTIME@
302 # if @REPLACE_STRFTIME@
303 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 # define strftime rpl_strftime
306 _GL_FUNCDECL_RPL (strftime
, size_t,
307 (char *restrict __buf
, size_t __bufsize
,
308 const char *restrict __fmt
, const struct tm
*restrict __tp
)
309 _GL_ARG_NONNULL ((1, 3, 4)));
310 _GL_CXXALIAS_RPL (strftime
, size_t,
311 (char *restrict __buf
, size_t __bufsize
,
312 const char *restrict __fmt
, const struct tm
*restrict __tp
));
314 _GL_CXXALIAS_SYS (strftime
, size_t,
315 (char *restrict __buf
, size_t __bufsize
,
316 const char *restrict __fmt
, const struct tm
*restrict __tp
));
319 _GL_CXXALIASWARN (strftime
);
323 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
324 typedef struct tm_zone
*timezone_t
;
325 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
326 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
327 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
328 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
329 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
330 (timezone_t __tz
, time_t const *restrict __timer
,
331 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
332 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
333 (timezone_t __tz
, time_t const *restrict __timer
,
334 struct tm
*restrict __result
));
335 _GL_FUNCDECL_SYS (mktime_z
, time_t,
336 (timezone_t __tz
, struct tm
*restrict __result
)
337 _GL_ARG_NONNULL ((2)));
338 _GL_CXXALIAS_SYS (mktime_z
, time_t,
339 (timezone_t __tz
, struct tm
*restrict __result
));
342 /* Convert TM to a time_t value, assuming UTC. */
344 # if @REPLACE_TIMEGM@
345 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
347 # define timegm rpl_timegm
349 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
350 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
353 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
355 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
357 _GL_CXXALIASWARN (timegm
);
360 /* Encourage applications to avoid unsafe functions that can overrun
361 buffers when given outlandish struct tm values. Portable
362 applications should use strftime (or even sprintf) instead. */
363 # if defined GNULIB_POSIXCHECK
365 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
366 "better use strftime (or even sprintf) instead");
368 # if defined GNULIB_POSIXCHECK
370 _GL_WARN_ON_USE (asctime_r
, "asctime_r can overrun buffers in some cases - "
371 "better use strftime (or even sprintf) instead");
373 # if defined GNULIB_POSIXCHECK
375 _GL_WARN_ON_USE (ctime
, "ctime can overrun buffers in some cases - "
376 "better use strftime (or even sprintf) instead");
378 # if defined GNULIB_POSIXCHECK
380 _GL_WARN_ON_USE (ctime_r
, "ctime_r can overrun buffers in some cases - "
381 "better use strftime (or even sprintf) instead");