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));
140 _GL_FUNCDECL_SYS (tzset
, void, (void));
142 _GL_CXXALIAS_SYS (tzset
, void, (void));
144 _GL_CXXALIASWARN (tzset
);
147 /* Return the 'time_t' representation of TP and normalize TP. */
149 # if @REPLACE_MKTIME@
150 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
151 # define mktime rpl_mktime
153 _GL_FUNCDECL_RPL (mktime
, time_t, (struct tm
*__tp
) _GL_ARG_NONNULL ((1)));
154 _GL_CXXALIAS_RPL (mktime
, time_t, (struct tm
*__tp
));
156 _GL_CXXALIAS_SYS (mktime
, time_t, (struct tm
*__tp
));
159 _GL_CXXALIASWARN (mktime
);
163 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
164 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
165 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
167 # if @REPLACE_LOCALTIME_R@
168 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
170 # define localtime_r rpl_localtime_r
172 _GL_FUNCDECL_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
173 struct tm
*restrict __result
)
174 _GL_ARG_NONNULL ((1, 2)));
175 _GL_CXXALIAS_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
176 struct tm
*restrict __result
));
178 # if ! @HAVE_DECL_LOCALTIME_R@
179 _GL_FUNCDECL_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
180 struct tm
*restrict __result
)
181 _GL_ARG_NONNULL ((1, 2)));
183 _GL_CXXALIAS_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
184 struct tm
*restrict __result
));
186 # if @HAVE_DECL_LOCALTIME_R@
187 _GL_CXXALIASWARN (localtime_r
);
189 # if @REPLACE_LOCALTIME_R@
190 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
192 # define gmtime_r rpl_gmtime_r
194 _GL_FUNCDECL_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
195 struct tm
*restrict __result
)
196 _GL_ARG_NONNULL ((1, 2)));
197 _GL_CXXALIAS_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
198 struct tm
*restrict __result
));
200 # if ! @HAVE_DECL_LOCALTIME_R@
201 _GL_FUNCDECL_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
202 struct tm
*restrict __result
)
203 _GL_ARG_NONNULL ((1, 2)));
205 _GL_CXXALIAS_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
206 struct tm
*restrict __result
));
208 # if @HAVE_DECL_LOCALTIME_R@
209 _GL_CXXALIASWARN (gmtime_r
);
213 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
214 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
215 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
216 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
217 # if @REPLACE_LOCALTIME@
218 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
220 # define localtime rpl_localtime
222 _GL_FUNCDECL_RPL (localtime
, struct tm
*, (time_t const *__timer
)
223 _GL_ARG_NONNULL ((1)));
224 _GL_CXXALIAS_RPL (localtime
, struct tm
*, (time_t const *__timer
));
226 _GL_CXXALIAS_SYS (localtime
, struct tm
*, (time_t const *__timer
));
229 _GL_CXXALIASWARN (localtime
);
233 # if 0 || @REPLACE_GMTIME@
234 # if @REPLACE_GMTIME@
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define gmtime rpl_gmtime
239 _GL_FUNCDECL_RPL (gmtime
, struct tm
*, (time_t const *__timer
)
240 _GL_ARG_NONNULL ((1)));
241 _GL_CXXALIAS_RPL (gmtime
, struct tm
*, (time_t const *__timer
));
243 _GL_CXXALIAS_SYS (gmtime
, struct tm
*, (time_t const *__timer
));
245 _GL_CXXALIASWARN (gmtime
);
248 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
249 the resulting broken-down time into TM. See
250 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
251 # if @GNULIB_STRPTIME@
252 # if ! @HAVE_STRPTIME@
253 _GL_FUNCDECL_SYS (strptime
, char *, (char const *restrict __buf
,
254 char const *restrict __format
,
255 struct tm
*restrict __tm
)
256 _GL_ARG_NONNULL ((1, 2, 3)));
258 _GL_CXXALIAS_SYS (strptime
, char *, (char const *restrict __buf
,
259 char const *restrict __format
,
260 struct tm
*restrict __tm
));
261 _GL_CXXALIASWARN (strptime
);
264 /* Convert *TP to a date and time string. See
265 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
268 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
269 # define ctime rpl_ctime
271 _GL_FUNCDECL_RPL (ctime
, char *, (time_t const *__tp
)
272 _GL_ARG_NONNULL ((1)));
273 _GL_CXXALIAS_RPL (ctime
, char *, (time_t const *__tp
));
275 _GL_CXXALIAS_SYS (ctime
, char *, (time_t const *__tp
));
278 _GL_CXXALIASWARN (ctime
);
282 /* Convert *TP to a date and time string. See
283 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
284 # if @GNULIB_STRFTIME@
285 # if @REPLACE_STRFTIME@
286 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
287 # define strftime rpl_strftime
289 _GL_FUNCDECL_RPL (strftime
, size_t,
290 (char *restrict __buf
, size_t __bufsize
,
291 const char *restrict __fmt
, const struct tm
*restrict __tp
)
292 _GL_ARG_NONNULL ((1, 3, 4)));
293 _GL_CXXALIAS_RPL (strftime
, size_t,
294 (char *restrict __buf
, size_t __bufsize
,
295 const char *restrict __fmt
, const struct tm
*restrict __tp
));
297 _GL_CXXALIAS_SYS (strftime
, size_t,
298 (char *restrict __buf
, size_t __bufsize
,
299 const char *restrict __fmt
, const struct tm
*restrict __tp
));
302 _GL_CXXALIASWARN (strftime
);
306 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
307 typedef struct tm_zone
*timezone_t
;
308 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
309 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
310 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
311 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
312 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
313 (timezone_t __tz
, time_t const *restrict __timer
,
314 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
315 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
316 (timezone_t __tz
, time_t const *restrict __timer
,
317 struct tm
*restrict __result
));
318 _GL_FUNCDECL_SYS (mktime_z
, time_t,
319 (timezone_t __tz
, struct tm
*restrict __result
)
320 _GL_ARG_NONNULL ((2)));
321 _GL_CXXALIAS_SYS (mktime_z
, time_t,
322 (timezone_t __tz
, struct tm
*restrict __result
));
325 /* Convert TM to a time_t value, assuming UTC. */
327 # if @REPLACE_TIMEGM@
328 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
330 # define timegm rpl_timegm
332 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
333 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
336 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
338 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
340 _GL_CXXALIASWARN (timegm
);
343 /* Encourage applications to avoid unsafe functions that can overrun
344 buffers when given outlandish struct tm values. Portable
345 applications should use strftime (or even sprintf) instead. */
346 # if defined GNULIB_POSIXCHECK
348 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
349 "better use strftime (or even sprintf) instead");
351 # if defined GNULIB_POSIXCHECK
353 _GL_WARN_ON_USE (asctime
, "asctime_r can overrun buffers in some cases - "
354 "better use strftime (or even sprintf) instead");
356 # if defined GNULIB_POSIXCHECK
358 _GL_WARN_ON_USE (asctime
, "ctime can overrun buffers in some cases - "
359 "better use strftime (or even sprintf) instead");
361 # if defined GNULIB_POSIXCHECK
363 _GL_WARN_ON_USE (asctime
, "ctime_r can overrun buffers in some cases - "
364 "better use strftime (or even sprintf) instead");