1/* A more-standard <time.h>.
2
3   Copyright (C) 2007-2020 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
71extern "C" {
72#   endif
73
74#   if !GNULIB_defined_struct_timespec
75#    undef timespec
76#    define timespec rpl_timespec
77struct 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.  */
98struct __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#  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
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
158#  if __GLIBC__ >= 2
159_GL_CXXALIASWARN (mktime);
160#  endif
161# endif
162
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>.  */
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
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)
219#    undef localtime
220#    define localtime rpl_localtime
221#   endif
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));
225#  else
226_GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
227#  endif
228#  if __GLIBC__ >= 2
229_GL_CXXALIASWARN (localtime);
230#  endif
231# endif
232
233# if 0 || @REPLACE_GMTIME@
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
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)));
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
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,
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));
296#  else
297_GL_CXXALIAS_SYS (strftime, size_t,
298                  (char *restrict __buf, size_t __bufsize,
299                   const char *restrict __fmt, const struct tm *restrict __tp));
300#  endif
301#  if __GLIBC__ >= 2
302_GL_CXXALIASWARN (strftime);
303#  endif
304# endif
305
306# if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
307typedef 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));
323# endif
324
325/* Convert TM to a time_t value, assuming UTC.  */
326# if @GNULIB_TIMEGM@
327#  if @REPLACE_TIMEGM@
328#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
329#    undef timegm
330#    define timegm rpl_timegm
331#   endif
332_GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
333_GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
334#  else
335#   if ! @HAVE_TIMEGM@
336_GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
337#   endif
338_GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
339#  endif
340_GL_CXXALIASWARN (timegm);
341# endif
342
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
347#  undef asctime
348_GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
349                 "better use strftime (or even sprintf) instead");
350# endif
351# if defined GNULIB_POSIXCHECK
352#  undef asctime_r
353_GL_WARN_ON_USE (asctime, "asctime_r can overrun buffers in some cases - "
354                 "better use strftime (or even sprintf) instead");
355# endif
356# if defined GNULIB_POSIXCHECK
357#  undef ctime
358_GL_WARN_ON_USE (asctime, "ctime can overrun buffers in some cases - "
359                 "better use strftime (or even sprintf) instead");
360# endif
361# if defined GNULIB_POSIXCHECK
362#  undef ctime_r
363_GL_WARN_ON_USE (asctime, "ctime_r can overrun buffers in some cases - "
364                 "better use strftime (or even sprintf) instead");
365# endif
366
367#endif
368