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 defined _WIN32 && !defined __CYGWIN__
150 # define tzset _tzset
153 /* Return the 'time_t' representation of TP and normalize TP. */
155 # if @REPLACE_MKTIME@
156 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
157 # define mktime rpl_mktime
159 _GL_FUNCDECL_RPL (mktime
, time_t, (struct tm
*__tp
) _GL_ARG_NONNULL ((1)));
160 _GL_CXXALIAS_RPL (mktime
, time_t, (struct tm
*__tp
));
162 _GL_CXXALIAS_SYS (mktime
, time_t, (struct tm
*__tp
));
165 _GL_CXXALIASWARN (mktime
);
169 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
170 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
171 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
173 # if @REPLACE_LOCALTIME_R@
174 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
176 # define localtime_r rpl_localtime_r
178 _GL_FUNCDECL_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
179 struct tm
*restrict __result
)
180 _GL_ARG_NONNULL ((1, 2)));
181 _GL_CXXALIAS_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
182 struct tm
*restrict __result
));
184 # if ! @HAVE_DECL_LOCALTIME_R@
185 _GL_FUNCDECL_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
186 struct tm
*restrict __result
)
187 _GL_ARG_NONNULL ((1, 2)));
189 _GL_CXXALIAS_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
190 struct tm
*restrict __result
));
192 # if @HAVE_DECL_LOCALTIME_R@
193 _GL_CXXALIASWARN (localtime_r
);
195 # if @REPLACE_LOCALTIME_R@
196 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
198 # define gmtime_r rpl_gmtime_r
200 _GL_FUNCDECL_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
201 struct tm
*restrict __result
)
202 _GL_ARG_NONNULL ((1, 2)));
203 _GL_CXXALIAS_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
204 struct tm
*restrict __result
));
206 # if ! @HAVE_DECL_LOCALTIME_R@
207 _GL_FUNCDECL_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
208 struct tm
*restrict __result
)
209 _GL_ARG_NONNULL ((1, 2)));
211 _GL_CXXALIAS_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
212 struct tm
*restrict __result
));
214 # if @HAVE_DECL_LOCALTIME_R@
215 _GL_CXXALIASWARN (gmtime_r
);
219 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
220 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
221 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
222 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
223 # if @REPLACE_LOCALTIME@
224 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
226 # define localtime rpl_localtime
228 _GL_FUNCDECL_RPL (localtime
, struct tm
*, (time_t const *__timer
)
229 _GL_ARG_NONNULL ((1)));
230 _GL_CXXALIAS_RPL (localtime
, struct tm
*, (time_t const *__timer
));
232 _GL_CXXALIAS_SYS (localtime
, struct tm
*, (time_t const *__timer
));
235 _GL_CXXALIASWARN (localtime
);
239 # if 0 || @REPLACE_GMTIME@
240 # if @REPLACE_GMTIME@
241 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
243 # define gmtime rpl_gmtime
245 _GL_FUNCDECL_RPL (gmtime
, struct tm
*, (time_t const *__timer
)
246 _GL_ARG_NONNULL ((1)));
247 _GL_CXXALIAS_RPL (gmtime
, struct tm
*, (time_t const *__timer
));
249 _GL_CXXALIAS_SYS (gmtime
, struct tm
*, (time_t const *__timer
));
251 _GL_CXXALIASWARN (gmtime
);
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)));
264 _GL_CXXALIAS_SYS (strptime
, char *, (char const *restrict __buf
,
265 char const *restrict __format
,
266 struct tm
*restrict __tm
));
267 _GL_CXXALIASWARN (strptime
);
270 /* Convert *TP to a date and time string. See
271 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
274 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
275 # define ctime rpl_ctime
277 _GL_FUNCDECL_RPL (ctime
, char *, (time_t const *__tp
)
278 _GL_ARG_NONNULL ((1)));
279 _GL_CXXALIAS_RPL (ctime
, char *, (time_t const *__tp
));
281 _GL_CXXALIAS_SYS (ctime
, char *, (time_t const *__tp
));
284 _GL_CXXALIASWARN (ctime
);
288 /* Convert *TP to a date and time string. See
289 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
290 # if @GNULIB_STRFTIME@
291 # if @REPLACE_STRFTIME@
292 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
293 # define strftime rpl_strftime
295 _GL_FUNCDECL_RPL (strftime
, size_t,
296 (char *restrict __buf
, size_t __bufsize
,
297 const char *restrict __fmt
, const struct tm
*restrict __tp
)
298 _GL_ARG_NONNULL ((1, 3, 4)));
299 _GL_CXXALIAS_RPL (strftime
, size_t,
300 (char *restrict __buf
, size_t __bufsize
,
301 const char *restrict __fmt
, const struct tm
*restrict __tp
));
303 _GL_CXXALIAS_SYS (strftime
, size_t,
304 (char *restrict __buf
, size_t __bufsize
,
305 const char *restrict __fmt
, const struct tm
*restrict __tp
));
308 _GL_CXXALIASWARN (strftime
);
312 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
313 typedef struct tm_zone
*timezone_t
;
314 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
315 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
316 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
317 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
318 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
319 (timezone_t __tz
, time_t const *restrict __timer
,
320 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
321 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
322 (timezone_t __tz
, time_t const *restrict __timer
,
323 struct tm
*restrict __result
));
324 _GL_FUNCDECL_SYS (mktime_z
, time_t,
325 (timezone_t __tz
, struct tm
*restrict __result
)
326 _GL_ARG_NONNULL ((2)));
327 _GL_CXXALIAS_SYS (mktime_z
, time_t,
328 (timezone_t __tz
, struct tm
*restrict __result
));
331 /* Convert TM to a time_t value, assuming UTC. */
333 # if @REPLACE_TIMEGM@
334 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
336 # define timegm rpl_timegm
338 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
339 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
342 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
344 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
346 _GL_CXXALIASWARN (timegm
);
349 /* Encourage applications to avoid unsafe functions that can overrun
350 buffers when given outlandish struct tm values. Portable
351 applications should use strftime (or even sprintf) instead. */
352 # if defined GNULIB_POSIXCHECK
354 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
355 "better use strftime (or even sprintf) instead");
357 # if defined GNULIB_POSIXCHECK
359 _GL_WARN_ON_USE (asctime
, "asctime_r can overrun buffers in some cases - "
360 "better use strftime (or even sprintf) instead");
362 # if defined GNULIB_POSIXCHECK
364 _GL_WARN_ON_USE (asctime
, "ctime can overrun buffers in some cases - "
365 "better use strftime (or even sprintf) instead");
367 # if defined GNULIB_POSIXCHECK
369 _GL_WARN_ON_USE (asctime
, "ctime_r can overrun buffers in some cases - "
370 "better use strftime (or even sprintf) instead");