Commit | Line | Data |
---|---|---|
09607c9e TT |
1 | /* A more-standard <time.h>. |
2 | ||
c0c3707f | 3 | Copyright (C) 2007-2019 Free Software Foundation, Inc. |
09607c9e TT |
4 | |
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) | |
8 | any later version. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
c0c3707f | 16 | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
09607c9e TT |
17 | |
18 | #if __GNUC__ >= 3 | |
19 | @PRAGMA_SYSTEM_HEADER@ | |
20 | #endif | |
21 | @PRAGMA_COLUMNS@ | |
22 | ||
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 | |
4a626d0a PA |
25 | symbols. (However, skip this for MinGW as it treats __need_time_t |
26 | incompatibly.) Also, Solaris 8 <time.h> eventually includes itself | |
09607c9e TT |
27 | recursively; if that is happening, just include the system <time.h> |
28 | without adding our own declarations. */ | |
4a626d0a PA |
29 | #if (((defined __need_time_t || defined __need_clock_t \ |
30 | || defined __need_timespec) \ | |
31 | && !defined __MINGW32__) \ | |
09607c9e TT |
32 | || defined _@GUARD_PREFIX@_TIME_H) |
33 | ||
34 | # @INCLUDE_NEXT@ @NEXT_TIME_H@ | |
35 | ||
36 | #else | |
37 | ||
38 | # define _@GUARD_PREFIX@_TIME_H | |
39 | ||
c0c3707f CB |
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__ | |
43 | # include <unistd.h> | |
44 | # endif | |
45 | ||
09607c9e TT |
46 | # @INCLUDE_NEXT@ @NEXT_TIME_H@ |
47 | ||
48 | /* NetBSD 5.0 mis-defines NULL. */ | |
49 | # include <stddef.h> | |
50 | ||
51 | /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ | |
52 | ||
53 | /* The definition of _GL_ARG_NONNULL is copied here. */ | |
54 | ||
55 | /* The definition of _GL_WARN_ON_USE is copied here. */ | |
56 | ||
c0c3707f | 57 | /* Some systems don't define struct timespec (e.g., AIX 4.1). |
09607c9e | 58 | Or they define it with the wrong member names or define it in <sys/time.h> |
4a626d0a PA |
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>. */ | |
09607c9e TT |
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@ | |
65 | # include <pthread.h> | |
4a626d0a PA |
66 | # elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@ |
67 | # include <unistd.h> | |
09607c9e TT |
68 | # else |
69 | ||
70 | # ifdef __cplusplus | |
71 | extern "C" { | |
72 | # endif | |
73 | ||
74 | # if !GNULIB_defined_struct_timespec | |
75 | # undef timespec | |
76 | # define timespec rpl_timespec | |
77 | struct timespec | |
78 | { | |
79 | time_t tv_sec; | |
80 | long int tv_nsec; | |
81 | }; | |
82 | # define GNULIB_defined_struct_timespec 1 | |
83 | # endif | |
84 | ||
85 | # ifdef __cplusplus | |
86 | } | |
87 | # endif | |
88 | ||
89 | # endif | |
90 | # endif | |
91 | ||
92 | # if !GNULIB_defined_struct_time_t_must_be_integral | |
c0c3707f CB |
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 | |
09607c9e TT |
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; | |
100 | }; | |
101 | # define GNULIB_defined_struct_time_t_must_be_integral 1 | |
102 | # endif | |
103 | ||
104 | /* Sleep for at least RQTP seconds unless interrupted, If interrupted, | |
105 | return -1 and store the remaining time into RMTP. See | |
c0c3707f | 106 | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */ |
09607c9e TT |
107 | # if @GNULIB_NANOSLEEP@ |
108 | # if @REPLACE_NANOSLEEP@ | |
109 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
110 | # define nanosleep rpl_nanosleep | |
111 | # endif | |
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)); | |
117 | # else | |
118 | # if ! @HAVE_NANOSLEEP@ | |
119 | _GL_FUNCDECL_SYS (nanosleep, int, | |
120 | (struct timespec const *__rqtp, struct timespec *__rmtp) | |
121 | _GL_ARG_NONNULL ((1))); | |
122 | # endif | |
123 | _GL_CXXALIAS_SYS (nanosleep, int, | |
124 | (struct timespec const *__rqtp, struct timespec *__rmtp)); | |
125 | # endif | |
126 | _GL_CXXALIASWARN (nanosleep); | |
127 | # endif | |
128 | ||
c0c3707f CB |
129 | /* Initialize time conversion information. */ |
130 | # if @GNULIB_TZSET@ | |
131 | # if @REPLACE_TZSET@ | |
132 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
133 | # undef tzset | |
134 | # define tzset rpl_tzset | |
135 | # endif | |
136 | _GL_FUNCDECL_RPL (tzset, void, (void)); | |
137 | _GL_CXXALIAS_RPL (tzset, void, (void)); | |
138 | # else | |
139 | # if ! @HAVE_TZSET@ | |
140 | _GL_FUNCDECL_SYS (tzset, void, (void)); | |
141 | # endif | |
142 | _GL_CXXALIAS_SYS (tzset, void, (void)); | |
143 | # endif | |
144 | _GL_CXXALIASWARN (tzset); | |
145 | # endif | |
146 | ||
09607c9e TT |
147 | /* Return the 'time_t' representation of TP and normalize TP. */ |
148 | # if @GNULIB_MKTIME@ | |
149 | # if @REPLACE_MKTIME@ | |
150 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
151 | # define mktime rpl_mktime | |
152 | # endif | |
153 | _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1))); | |
154 | _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp)); | |
155 | # else | |
156 | _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp)); | |
157 | # endif | |
c0c3707f | 158 | # if __GLIBC__ >= 2 |
09607c9e | 159 | _GL_CXXALIASWARN (mktime); |
c0c3707f | 160 | # endif |
09607c9e TT |
161 | # endif |
162 | ||
163 | /* Convert TIMER to RESULT, assuming local time and UTC respectively. See | |
c0c3707f CB |
164 | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and |
165 | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */ | |
09607c9e TT |
166 | # if @GNULIB_TIME_R@ |
167 | # if @REPLACE_LOCALTIME_R@ | |
168 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
169 | # undef localtime_r | |
170 | # define localtime_r rpl_localtime_r | |
171 | # endif | |
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)); | |
177 | # else | |
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))); | |
182 | # endif | |
183 | _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer, | |
184 | struct tm *restrict __result)); | |
185 | # endif | |
186 | # if @HAVE_DECL_LOCALTIME_R@ | |
187 | _GL_CXXALIASWARN (localtime_r); | |
188 | # endif | |
189 | # if @REPLACE_LOCALTIME_R@ | |
190 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
191 | # undef gmtime_r | |
192 | # define gmtime_r rpl_gmtime_r | |
193 | # endif | |
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)); | |
199 | # else | |
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))); | |
204 | # endif | |
205 | _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer, | |
206 | struct tm *restrict __result)); | |
207 | # endif | |
208 | # if @HAVE_DECL_LOCALTIME_R@ | |
209 | _GL_CXXALIASWARN (gmtime_r); | |
210 | # endif | |
211 | # endif | |
212 | ||
4a626d0a | 213 | /* Convert TIMER to RESULT, assuming local time and UTC respectively. See |
c0c3707f CB |
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@ | |
4a626d0a PA |
217 | # if @REPLACE_LOCALTIME@ |
218 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
219 | # undef localtime | |
220 | # define localtime rpl_localtime | |
221 | # endif | |
222 | _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer) | |
c0c3707f | 223 | _GL_ARG_NONNULL ((1))); |
4a626d0a PA |
224 | _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer)); |
225 | # else | |
226 | _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer)); | |
227 | # endif | |
c0c3707f | 228 | # if __GLIBC__ >= 2 |
4a626d0a | 229 | _GL_CXXALIASWARN (localtime); |
c0c3707f | 230 | # endif |
4a626d0a PA |
231 | # endif |
232 | ||
c0c3707f | 233 | # if 0 || @REPLACE_GMTIME@ |
4a626d0a PA |
234 | # if @REPLACE_GMTIME@ |
235 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
236 | # undef gmtime | |
237 | # define gmtime rpl_gmtime | |
238 | # endif | |
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)); | |
242 | # else | |
243 | _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer)); | |
244 | # endif | |
245 | _GL_CXXALIASWARN (gmtime); | |
246 | # endif | |
247 | ||
c0c3707f | 248 | /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store |
09607c9e | 249 | the resulting broken-down time into TM. See |
c0c3707f | 250 | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */ |
09607c9e TT |
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))); | |
257 | # endif | |
258 | _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf, | |
259 | char const *restrict __format, | |
260 | struct tm *restrict __tm)); | |
261 | _GL_CXXALIASWARN (strptime); | |
262 | # endif | |
263 | ||
c0c3707f CB |
264 | /* Convert *TP to a date and time string. See |
265 | <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */ | |
266 | # if @GNULIB_CTIME@ | |
267 | # if @REPLACE_CTIME@ | |
268 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
269 | # define ctime rpl_ctime | |
270 | # endif | |
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)); | |
274 | # else | |
275 | _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp)); | |
276 | # endif | |
277 | # if __GLIBC__ >= 2 | |
278 | _GL_CXXALIASWARN (ctime); | |
279 | # endif | |
280 | # endif | |
281 | ||
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 | |
288 | # endif | |
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)); | |
294 | # else | |
295 | _GL_CXXALIAS_SYS (strftime, size_t, (char *__buf, size_t __bufsize, | |
296 | const char *__fmt, const struct tm *__tp)); | |
297 | # endif | |
298 | # if __GLIBC__ >= 2 | |
299 | _GL_CXXALIASWARN (strftime); | |
300 | # endif | |
301 | # endif | |
302 | ||
4a626d0a PA |
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)); | |
320 | # endif | |
321 | ||
09607c9e TT |
322 | /* Convert TM to a time_t value, assuming UTC. */ |
323 | # if @GNULIB_TIMEGM@ | |
324 | # if @REPLACE_TIMEGM@ | |
325 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
326 | # undef timegm | |
327 | # define timegm rpl_timegm | |
328 | # endif | |
329 | _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1))); | |
330 | _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm)); | |
331 | # else | |
332 | # if ! @HAVE_TIMEGM@ | |
333 | _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1))); | |
334 | # endif | |
335 | _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm)); | |
336 | # endif | |
337 | _GL_CXXALIASWARN (timegm); | |
338 | # endif | |
339 | ||
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 | |
344 | # undef asctime | |
345 | _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - " | |
346 | "better use strftime (or even sprintf) instead"); | |
347 | # endif | |
348 | # if defined GNULIB_POSIXCHECK | |
349 | # undef asctime_r | |
350 | _GL_WARN_ON_USE (asctime, "asctime_r can overrun buffers in some cases - " | |
351 | "better use strftime (or even sprintf) instead"); | |
352 | # endif | |
353 | # if defined GNULIB_POSIXCHECK | |
354 | # undef ctime | |
355 | _GL_WARN_ON_USE (asctime, "ctime can overrun buffers in some cases - " | |
356 | "better use strftime (or even sprintf) instead"); | |
357 | # endif | |
358 | # if defined GNULIB_POSIXCHECK | |
359 | # undef ctime_r | |
360 | _GL_WARN_ON_USE (asctime, "ctime_r can overrun buffers in some cases - " | |
361 | "better use strftime (or even sprintf) instead"); | |
362 | # endif | |
363 | ||
364 | #endif |