1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2019 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 /* 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, (char *__buf
, size_t __bufsize
,
290 const char *__fmt
, const struct tm
*__tp
)
291 _GL_ARG_NONNULL ((1, 3, 4)));
292 _GL_CXXALIAS_RPL (strftime
, size_t, (char *__buf
, size_t __bufsize
,
293 const char *__fmt
, const struct tm
*__tp
));
295 _GL_CXXALIAS_SYS (strftime
, size_t, (char *__buf
, size_t __bufsize
,
296 const char *__fmt
, const struct tm
*__tp
));
299 _GL_CXXALIASWARN (strftime
);
303 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
304 typedef struct tm_zone
*timezone_t
;
305 _GL_FUNCDECL_SYS (tzalloc
, timezone_t
, (char const *__name
));
306 _GL_CXXALIAS_SYS (tzalloc
, timezone_t
, (char const *__name
));
307 _GL_FUNCDECL_SYS (tzfree
, void, (timezone_t __tz
));
308 _GL_CXXALIAS_SYS (tzfree
, void, (timezone_t __tz
));
309 _GL_FUNCDECL_SYS (localtime_rz
, struct tm
*,
310 (timezone_t __tz
, time_t const *restrict __timer
,
311 struct tm
*restrict __result
) _GL_ARG_NONNULL ((2, 3)));
312 _GL_CXXALIAS_SYS (localtime_rz
, struct tm
*,
313 (timezone_t __tz
, time_t const *restrict __timer
,
314 struct tm
*restrict __result
));
315 _GL_FUNCDECL_SYS (mktime_z
, time_t,
316 (timezone_t __tz
, struct tm
*restrict __result
)
317 _GL_ARG_NONNULL ((2)));
318 _GL_CXXALIAS_SYS (mktime_z
, time_t,
319 (timezone_t __tz
, struct tm
*restrict __result
));
322 /* Convert TM to a time_t value, assuming UTC. */
324 # if @REPLACE_TIMEGM@
325 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
327 # define timegm rpl_timegm
329 _GL_FUNCDECL_RPL (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
330 _GL_CXXALIAS_RPL (timegm
, time_t, (struct tm
*__tm
));
333 _GL_FUNCDECL_SYS (timegm
, time_t, (struct tm
*__tm
) _GL_ARG_NONNULL ((1)));
335 _GL_CXXALIAS_SYS (timegm
, time_t, (struct tm
*__tm
));
337 _GL_CXXALIASWARN (timegm
);
340 /* Encourage applications to avoid unsafe functions that can overrun
341 buffers when given outlandish struct tm values. Portable
342 applications should use strftime (or even sprintf) instead. */
343 # if defined GNULIB_POSIXCHECK
345 _GL_WARN_ON_USE (asctime
, "asctime can overrun buffers in some cases - "
346 "better use strftime (or even sprintf) instead");
348 # if defined GNULIB_POSIXCHECK
350 _GL_WARN_ON_USE (asctime
, "asctime_r can overrun buffers in some cases - "
351 "better use strftime (or even sprintf) instead");
353 # if defined GNULIB_POSIXCHECK
355 _GL_WARN_ON_USE (asctime
, "ctime can overrun buffers in some cases - "
356 "better use strftime (or even sprintf) instead");
358 # if defined GNULIB_POSIXCHECK
360 _GL_WARN_ON_USE (asctime
, "ctime_r can overrun buffers in some cases - "
361 "better use strftime (or even sprintf) instead");