1 /* A more-standard <time.h>.
2 
3    Copyright (C) 2007-2021 Free Software Foundation, Inc.
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
16    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
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
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)
33 
34 # @INCLUDE_NEXT@ @NEXT_TIME_H@
35 
36 #else
37 
38 # define _@GUARD_PREFIX@_TIME_H
39 
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 
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 
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@
65 #   include <pthread.h>
66 #  elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
67 #   include <unistd.h>
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
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;
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
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
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 
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 #  elif defined _WIN32 && !defined __CYGWIN__
139 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
140 #    undef tzset
141 #    define tzset _tzset
142 #   endif
143 _GL_CXXALIAS_MDA (tzset, void, (void));
144 #  else
145 _GL_CXXALIAS_SYS (tzset, void, (void));
146 #  endif
147 _GL_CXXALIASWARN (tzset);
148 # elif @GNULIB_MDA_TZSET@
149 /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
150    required.  In C++ with GNULIB_NAMESPACE, avoid differences between
151    platforms by defining GNULIB_NAMESPACE::tzset always.  */
152 #  if defined _WIN32 && !defined __CYGWIN__
153 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
154 #    undef tzset
155 #    define tzset _tzset
156 #   endif
157 _GL_CXXALIAS_MDA (tzset, void, (void));
158 #  else
159 _GL_CXXALIAS_SYS (tzset, void, (void));
160 #  endif
161 _GL_CXXALIASWARN (tzset);
162 # endif
163 
164 /* Return the 'time_t' representation of TP and normalize TP.  */
165 # if @GNULIB_MKTIME@
166 #  if @REPLACE_MKTIME@
167 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
168 #    define mktime rpl_mktime
169 #   endif
170 _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
171 _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
172 #  else
173 _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
174 #  endif
175 #  if __GLIBC__ >= 2
176 _GL_CXXALIASWARN (mktime);
177 #  endif
178 # endif
179 
180 /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
181    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
182    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>.  */
183 # if @GNULIB_TIME_R@
184 #  if @REPLACE_LOCALTIME_R@
185 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
186 #    undef localtime_r
187 #    define localtime_r rpl_localtime_r
188 #   endif
189 _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
190                                              struct tm *restrict __result)
191                                             _GL_ARG_NONNULL ((1, 2)));
192 _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
193                                              struct tm *restrict __result));
194 #  else
195 #   if ! @HAVE_DECL_LOCALTIME_R@
196 _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
197                                              struct tm *restrict __result)
198                                             _GL_ARG_NONNULL ((1, 2)));
199 #   endif
200 _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
201                                              struct tm *restrict __result));
202 #  endif
203 #  if @HAVE_DECL_LOCALTIME_R@
204 _GL_CXXALIASWARN (localtime_r);
205 #  endif
206 #  if @REPLACE_LOCALTIME_R@
207 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
208 #    undef gmtime_r
209 #    define gmtime_r rpl_gmtime_r
210 #   endif
211 _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
212                                           struct tm *restrict __result)
213                                          _GL_ARG_NONNULL ((1, 2)));
214 _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
215                                           struct tm *restrict __result));
216 #  else
217 #   if ! @HAVE_DECL_LOCALTIME_R@
218 _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
219                                           struct tm *restrict __result)
220                                          _GL_ARG_NONNULL ((1, 2)));
221 #   endif
222 _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
223                                           struct tm *restrict __result));
224 #  endif
225 #  if @HAVE_DECL_LOCALTIME_R@
226 _GL_CXXALIASWARN (gmtime_r);
227 #  endif
228 # endif
229 
230 /* Convert TIMER to RESULT, assuming local time and UTC respectively.  See
231    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
232    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>.  */
233 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
234 #  if @REPLACE_LOCALTIME@
235 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
236 #    undef localtime
237 #    define localtime rpl_localtime
238 #   endif
239 _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
240                                           _GL_ARG_NONNULL ((1)));
241 _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
242 #  else
243 _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
244 #  endif
245 #  if __GLIBC__ >= 2
246 _GL_CXXALIASWARN (localtime);
247 #  endif
248 # endif
249 
250 # if 0 || @REPLACE_GMTIME@
251 #  if @REPLACE_GMTIME@
252 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
253 #    undef gmtime
254 #    define gmtime rpl_gmtime
255 #   endif
256 _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
257                                        _GL_ARG_NONNULL ((1)));
258 _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
259 #  else
260 _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
261 #  endif
262 _GL_CXXALIASWARN (gmtime);
263 # endif
264 
265 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
266    the resulting broken-down time into TM.  See
267    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>.  */
268 # if @GNULIB_STRPTIME@
269 #  if ! @HAVE_STRPTIME@
270 _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
271                                      char const *restrict __format,
272                                      struct tm *restrict __tm)
273                                     _GL_ARG_NONNULL ((1, 2, 3)));
274 #  endif
275 _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
276                                      char const *restrict __format,
277                                      struct tm *restrict __tm));
278 _GL_CXXALIASWARN (strptime);
279 # endif
280 
281 /* Convert *TP to a date and time string.  See
282    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>.  */
283 # if @GNULIB_CTIME@
284 #  if @REPLACE_CTIME@
285 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
286 #    define ctime rpl_ctime
287 #   endif
288 _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
289                                  _GL_ARG_NONNULL ((1)));
290 _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
291 #  else
292 _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
293 #  endif
294 #  if __GLIBC__ >= 2
295 _GL_CXXALIASWARN (ctime);
296 #  endif
297 # endif
298 
299 /* Convert *TP to a date and time string.  See
300    <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>.  */
301 # if @GNULIB_STRFTIME@
302 #  if @REPLACE_STRFTIME@
303 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 #    define strftime rpl_strftime
305 #   endif
306 _GL_FUNCDECL_RPL (strftime, size_t,
307                   (char *restrict __buf, size_t __bufsize,
308                    const char *restrict __fmt, const struct tm *restrict __tp)
309                   _GL_ARG_NONNULL ((1, 3, 4)));
310 _GL_CXXALIAS_RPL (strftime, size_t,
311                   (char *restrict __buf, size_t __bufsize,
312                    const char *restrict __fmt, const struct tm *restrict __tp));
313 #  else
314 _GL_CXXALIAS_SYS (strftime, size_t,
315                   (char *restrict __buf, size_t __bufsize,
316                    const char *restrict __fmt, const struct tm *restrict __tp));
317 #  endif
318 #  if __GLIBC__ >= 2
319 _GL_CXXALIASWARN (strftime);
320 #  endif
321 # endif
322 
323 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
324 typedef struct tm_zone *timezone_t;
325 _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
326 _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
327 _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
328 _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
329 _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
330                   (timezone_t __tz, time_t const *restrict __timer,
331                    struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
332 _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
333                   (timezone_t __tz, time_t const *restrict __timer,
334                    struct tm *restrict __result));
335 _GL_FUNCDECL_SYS (mktime_z, time_t,
336                   (timezone_t __tz, struct tm *restrict __result)
337                   _GL_ARG_NONNULL ((2)));
338 _GL_CXXALIAS_SYS (mktime_z, time_t,
339                   (timezone_t __tz, struct tm *restrict __result));
340 # endif
341 
342 /* Convert TM to a time_t value, assuming UTC.  */
343 # if @GNULIB_TIMEGM@
344 #  if @REPLACE_TIMEGM@
345 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
346 #    undef timegm
347 #    define timegm rpl_timegm
348 #   endif
349 _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
350 _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
351 #  else
352 #   if ! @HAVE_TIMEGM@
353 _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
354 #   endif
355 _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
356 #  endif
357 _GL_CXXALIASWARN (timegm);
358 # endif
359 
360 /* Encourage applications to avoid unsafe functions that can overrun
361    buffers when given outlandish struct tm values.  Portable
362    applications should use strftime (or even sprintf) instead.  */
363 # if defined GNULIB_POSIXCHECK
364 #  undef asctime
365 _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
366                  "better use strftime (or even sprintf) instead");
367 # endif
368 # if defined GNULIB_POSIXCHECK
369 #  undef asctime_r
370 _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
371                  "better use strftime (or even sprintf) instead");
372 # endif
373 # if defined GNULIB_POSIXCHECK
374 #  undef ctime
375 _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
376                  "better use strftime (or even sprintf) instead");
377 # endif
378 # if defined GNULIB_POSIXCHECK
379 #  undef ctime_r
380 _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
381                  "better use strftime (or even sprintf) instead");
382 # endif
383 
384 #endif
385