1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2017 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 # @INCLUDE_NEXT@ @NEXT_TIME_H@
42 /* NetBSD 5.0 mis-defines NULL. */
45 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
47 /* The definition of _GL_ARG_NONNULL is copied here. */
49 /* The definition of _GL_WARN_ON_USE is copied here. */
51 /* Some systems don't define struct timespec (e.g., AIX 4.1, Ultrix 4.3).
52 Or they define it with the wrong member names or define it in <sys/time.h>
53 (e.g., FreeBSD circa 1997). Stock Mingw prior to 3.0 does not define it,
54 but the pthreads-win32 library defines it in <pthread.h>. */
55 # if ! @TIME_H_DEFINES_STRUCT_TIMESPEC@
56 # if @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@
57 # include <sys/time.h>
58 # elif @PTHREAD_H_DEFINES_STRUCT_TIMESPEC@
60 # elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
68 # if !GNULIB_defined_struct_timespec
70 # define timespec rpl_timespec
76 # define GNULIB_defined_struct_timespec 1
86 # if !GNULIB_defined_struct_time_t_must_be_integral
87 /* Per http://austingroupbugs.net/view.php?id=327, POSIX requires
88 time_t to be an integer type, even though C99 permits floating
89 point. We don't know of any implementation that uses floating
90 point, and it is much easier to write code that doesn't have to
91 worry about that corner case, so we force the issue. */
92 struct __time_t_must_be_integral
{
93 unsigned int __floating_time_t_unsupported
: (time_t) 1;
95 # define GNULIB_defined_struct_time_t_must_be_integral 1
98 /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
99 return -1 and store the remaining time into RMTP. See
100 <http://www.opengroup.org/susv3xsh/nanosleep.html>. */
101 # if @GNULIB_NANOSLEEP@
102 # if @REPLACE_NANOSLEEP@
103 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
104 # define nanosleep rpl_nanosleep
106 _GL_FUNCDECL_RPL (nanosleep
, int,
107 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
108 _GL_ARG_NONNULL ((1)));
109 _GL_CXXALIAS_RPL (nanosleep
, int,
110 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
112 # if ! @HAVE_NANOSLEEP@
113 _GL_FUNCDECL_SYS (nanosleep
, int,
114 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
)
115 _GL_ARG_NONNULL ((1)));
117 _GL_CXXALIAS_SYS (nanosleep
, int,
118 (struct timespec
const *__rqtp
, struct timespec
*__rmtp
));
120 _GL_CXXALIASWARN (nanosleep
);
123 /* Initialize time conversion information. */
126 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
128 # define tzset rpl_tzset
130 _GL_FUNCDECL_RPL (tzset
, void, (void));
131 _GL_CXXALIAS_RPL (tzset
, void, (void));
134 _GL_FUNCDECL_SYS (tzset
, void, (void));
136 _GL_CXXALIAS_SYS (tzset
, void, (void));
138 _GL_CXXALIASWARN (tzset
);
141 /* Return the 'time_t' representation of TP and normalize TP. */
143 # if @REPLACE_MKTIME@
144 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
145 # define mktime rpl_mktime
147 _GL_FUNCDECL_RPL (mktime
, time_t, (struct tm
*__tp
) _GL_ARG_NONNULL ((1)));
148 _GL_CXXALIAS_RPL (mktime
, time_t, (struct tm
*__tp
));
150 _GL_CXXALIAS_SYS (mktime
, time_t, (struct tm
*__tp
));
152 _GL_CXXALIASWARN (mktime
);
155 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
156 <http://www.opengroup.org/susv3xsh/localtime_r.html> and
157 <http://www.opengroup.org/susv3xsh/gmtime_r.html>. */
159 # if @REPLACE_LOCALTIME_R@
160 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
162 # define localtime_r rpl_localtime_r
164 _GL_FUNCDECL_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
165 struct tm
*restrict __result
)
166 _GL_ARG_NONNULL ((1, 2)));
167 _GL_CXXALIAS_RPL (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
168 struct tm
*restrict __result
));
170 # if ! @HAVE_DECL_LOCALTIME_R@
171 _GL_FUNCDECL_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
172 struct tm
*restrict __result
)
173 _GL_ARG_NONNULL ((1, 2)));
175 _GL_CXXALIAS_SYS (localtime_r
, struct tm
*, (time_t const *restrict __timer
,
176 struct tm
*restrict __result
));
178 # if @HAVE_DECL_LOCALTIME_R@
179 _GL_CXXALIASWARN (localtime_r
);
181 # if @REPLACE_LOCALTIME_R@
182 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
184 # define gmtime_r rpl_gmtime_r
186 _GL_FUNCDECL_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
187 struct tm
*restrict __result
)
188 _GL_ARG_NONNULL ((1, 2)));
189 _GL_CXXALIAS_RPL (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
190 struct tm
*restrict __result
));
192 # if ! @HAVE_DECL_LOCALTIME_R@
193 _GL_FUNCDECL_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
194 struct tm
*restrict __result
)
195 _GL_ARG_NONNULL ((1, 2)));
197 _GL_CXXALIAS_SYS (gmtime_r
, struct tm
*, (time_t const *restrict __timer
,
198 struct tm
*restrict __result
));
200 # if @HAVE_DECL_LOCALTIME_R@
201 _GL_CXXALIASWARN (gmtime_r
);
205 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
206 <http://www.opengroup.org/susv3xsh/localtime.html> and
207 <http://www.opengroup.org/susv3xsh/gmtime.html>. */
208 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
209 # if @REPLACE_LOCALTIME@
210 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
212 # define localtime rpl_localtime
214 _GL_FUNCDECL_RPL (localtime
, struct tm
*, (time_t const *__timer
)
215 _GL_ARG_NONNULL ((1)));
216 _GL_CXXALIAS_RPL (localtime
, struct tm
*, (time_t const *__timer
));
218 _GL_CXXALIAS_SYS (localtime
, struct tm
*, (time_t const *__timer
));
220 _GL_CXXALIASWARN (localtime
);
223 # if 0 || @REPLACE_GMTIME@
224 # if @REPLACE_GMTIME@
225 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
227 # define gmtime rpl_gmtime
229 _GL_FUNCDECL_RPL (gmtime
, struct tm
*, (time_t const *__timer
)
230 _GL_ARG_NONNULL ((1)));
231 _GL_CXXALIAS_RPL (gmtime
, struct tm
*, (time_t const *__timer
));
233 _GL_CXXALIAS_SYS (gmtime
, struct tm
*, (time_t const *__timer
));
235 _GL_CXXALIASWARN (gmtime
);
238 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
239 the resulting broken-down time into TM. See
240 <http://www.opengroup.org/susv3xsh/strptime.html>. */
241 # if @GNULIB_STRPTIME@
242 # if ! @HAVE_STRPTIME@
243 _GL_FUNCDECL_SYS (strptime
, char *, (char const *restrict __buf
,
244 char const *restrict __format
,
245 struct tm
*restrict __tm
)
246 _GL_ARG_NONNULL ((1, 2, 3)));
248 _GL_CXXALIAS_SYS (strptime
, char *, (char const *restrict __buf
,
249 char const *restrict __format
,
250 struct tm
*restrict __tm
));
251 _GL_CXXALIASWARN (strptime
);
254 /* Convert *TP to a date and time string. See
255 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
258 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
259 # define ctime rpl_ctime
261 _GL_FUNCDECL_RPL (ctime
, char *, (time_t const *__tp
)
262 _GL_ARG_NONNULL ((1)));
263 _GL_CXXALIAS_RPL (ctime
, char *, (time_t const *__tp
));
265 _GL_CXXALIAS_SYS (ctime
, char *, (time_t const *__tp
));
267 _GL_CXXALIASWARN (ctime
);
270 /* Convert *TP to a date and time string. See
271 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
272 # if @GNULIB_STRFTIME@
273 # if @REPLACE_STRFTIME@
274 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
275 # define strftime rpl_strftime
277 _GL_FUNCDECL_RPL (strftime
, size_t, (char *__buf
, size_t __bufsize
,
278 const char *__fmt
, const struct tm
*__tp
)
279 _GL_ARG_NONNULL ((1, 3, 4)));
280 _GL_CXXALIAS_RPL (strftime
, size_t, (char *__buf
, size_t __bufsize
,
281 const char *__fmt
, const struct tm
*__tp
));
283 _GL_CXXALIAS_SYS (strftime
, size_t, (char *__buf
, size_t __bufsize
,
284 const char *__fmt
, const struct tm
*__tp
));
286 _GL_CXXALIASWARN (strftime
);
289 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
290 typedef struct tm_zone
*timezone_t
;
291 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
292 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
293 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
294 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
295 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
296 (timezone_t __tz
, time_t const *restrict __timer
,
297 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
298 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
299 (timezone_t __tz
, time_t const *restrict __timer
,
300 struct tm
*restrict __result
));
301 _GL_FUNCDECL_SYS (mktime_z
, time_t,
302 (timezone_t __tz
, struct tm
*restrict __result
)
303 _GL_ARG_NONNULL ((2)));
304 _GL_CXXALIAS_SYS (mktime_z
, time_t,
305 (timezone_t __tz
, struct tm
*restrict __result
));
308 /* Convert TM to a time_t value, assuming UTC. */
310 # if @REPLACE_TIMEGM@
311 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
313 # define timegm rpl_timegm
315 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
316 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
319 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
321 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
323 _GL_CXXALIASWARN (timegm
);
326 /* Encourage applications to avoid unsafe functions that can overrun
327 buffers when given outlandish struct tm values. Portable
328 applications should use strftime (or even sprintf) instead. */
329 # if defined GNULIB_POSIXCHECK
331 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
332 "better use strftime (or even sprintf) instead");
334 # if defined GNULIB_POSIXCHECK
336 _GL_WARN_ON_USE (asctime
, "asctime_r can overrun buffers in some cases - "
337 "better use strftime (or even sprintf) instead");
339 # if defined GNULIB_POSIXCHECK
341 _GL_WARN_ON_USE (asctime
, "ctime can overrun buffers in some cases - "
342 "better use strftime (or even sprintf) instead");
344 # if defined GNULIB_POSIXCHECK
346 _GL_WARN_ON_USE (asctime
, "ctime_r can overrun buffers in some cases - "
347 "better use strftime (or even sprintf) instead");