timespec_get: New module.
[gnulib.git] / lib / time.in.h
blob4da1172bded1b0bb74f32aaf2f7320f62c365b03
1 /* A more-standard <time.h>.
3 Copyright (C) 2007-2021 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 2, or (at your option)
8 any later version.
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/>. */
18 #if __GNUC__ >= 3
19 @PRAGMA_SYSTEM_HEADER@
20 #endif
21 @PRAGMA_COLUMNS@
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@
36 #else
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__
43 # include <unistd.h>
44 # endif
46 # @INCLUDE_NEXT@ @NEXT_TIME_H@
48 /* NetBSD 5.0 mis-defines NULL. */
49 # include <stddef.h>
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@
65 # include <pthread.h>
66 # elif @UNISTD_H_DEFINES_STRUCT_TIMESPEC@
67 # include <unistd.h>
68 # else
70 # ifdef __cplusplus
71 extern "C" {
72 # endif
74 # if !GNULIB_defined_struct_timespec
75 # undef timespec
76 # define timespec rpl_timespec
77 struct timespec
79 time_t tv_sec;
80 long int tv_nsec;
82 # define GNULIB_defined_struct_timespec 1
83 # endif
85 # ifdef __cplusplus
87 # endif
89 # endif
90 # endif
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
102 # endif
104 /* Define TIME_UTC, a positive integer constant used for timespec_get(). */
105 # if ! @TIME_H_DEFINES_TIME_UTC@
106 # if !GNULIB_defined_TIME_UTC
107 # define TIME_UTC 1
108 # define GNULIB_defined_TIME_UTC 1
109 # endif
110 # endif
112 /* Set *TS to the current time, and return BASE.
113 Upon failure, return 0. */
114 # if @GNULIB_TIMESPEC_GET@
115 # if ! @HAVE_TIMESPEC_GET@
116 _GL_FUNCDECL_SYS (timespec_get, int, (struct timespec *ts, int base)
117 _GL_ARG_NONNULL ((1)));
118 # endif
119 _GL_CXXALIAS_SYS (timespec_get, int, (struct timespec *ts, int base));
120 _GL_CXXALIASWARN (timespec_get);
121 # endif
123 /* Sleep for at least RQTP seconds unless interrupted, If interrupted,
124 return -1 and store the remaining time into RMTP. See
125 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
126 # if @GNULIB_NANOSLEEP@
127 # if @REPLACE_NANOSLEEP@
128 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
129 # define nanosleep rpl_nanosleep
130 # endif
131 _GL_FUNCDECL_RPL (nanosleep, int,
132 (struct timespec const *__rqtp, struct timespec *__rmtp)
133 _GL_ARG_NONNULL ((1)));
134 _GL_CXXALIAS_RPL (nanosleep, int,
135 (struct timespec const *__rqtp, struct timespec *__rmtp));
136 # else
137 # if ! @HAVE_NANOSLEEP@
138 _GL_FUNCDECL_SYS (nanosleep, int,
139 (struct timespec const *__rqtp, struct timespec *__rmtp)
140 _GL_ARG_NONNULL ((1)));
141 # endif
142 _GL_CXXALIAS_SYS (nanosleep, int,
143 (struct timespec const *__rqtp, struct timespec *__rmtp));
144 # endif
145 _GL_CXXALIASWARN (nanosleep);
146 # endif
148 /* Initialize time conversion information. */
149 # if @GNULIB_TZSET@
150 # if @REPLACE_TZSET@
151 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
152 # undef tzset
153 # define tzset rpl_tzset
154 # endif
155 _GL_FUNCDECL_RPL (tzset, void, (void));
156 _GL_CXXALIAS_RPL (tzset, void, (void));
157 # elif defined _WIN32 && !defined __CYGWIN__
158 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
159 # undef tzset
160 # define tzset _tzset
161 # endif
162 _GL_CXXALIAS_MDA (tzset, void, (void));
163 # else
164 _GL_CXXALIAS_SYS (tzset, void, (void));
165 # endif
166 _GL_CXXALIASWARN (tzset);
167 # elif @GNULIB_MDA_TZSET@
168 /* On native Windows, map 'tzset' to '_tzset', so that -loldnames is not
169 required. In C++ with GNULIB_NAMESPACE, avoid differences between
170 platforms by defining GNULIB_NAMESPACE::tzset always. */
171 # if defined _WIN32 && !defined __CYGWIN__
172 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
173 # undef tzset
174 # define tzset _tzset
175 # endif
176 _GL_CXXALIAS_MDA (tzset, void, (void));
177 # else
178 _GL_CXXALIAS_SYS (tzset, void, (void));
179 # endif
180 _GL_CXXALIASWARN (tzset);
181 # endif
183 /* Return the 'time_t' representation of TP and normalize TP. */
184 # if @GNULIB_MKTIME@
185 # if @REPLACE_MKTIME@
186 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
187 # define mktime rpl_mktime
188 # endif
189 _GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1)));
190 _GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp));
191 # else
192 _GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp));
193 # endif
194 # if __GLIBC__ >= 2
195 _GL_CXXALIASWARN (mktime);
196 # endif
197 # endif
199 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
200 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html> and
201 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html>. */
202 # if @GNULIB_TIME_R@
203 # if @REPLACE_LOCALTIME_R@
204 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
205 # undef localtime_r
206 # define localtime_r rpl_localtime_r
207 # endif
208 _GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
209 struct tm *restrict __result)
210 _GL_ARG_NONNULL ((1, 2)));
211 _GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer,
212 struct tm *restrict __result));
213 # else
214 # if ! @HAVE_DECL_LOCALTIME_R@
215 _GL_FUNCDECL_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
216 struct tm *restrict __result)
217 _GL_ARG_NONNULL ((1, 2)));
218 # endif
219 _GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer,
220 struct tm *restrict __result));
221 # endif
222 # if @HAVE_DECL_LOCALTIME_R@
223 _GL_CXXALIASWARN (localtime_r);
224 # endif
225 # if @REPLACE_LOCALTIME_R@
226 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
227 # undef gmtime_r
228 # define gmtime_r rpl_gmtime_r
229 # endif
230 _GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
231 struct tm *restrict __result)
232 _GL_ARG_NONNULL ((1, 2)));
233 _GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer,
234 struct tm *restrict __result));
235 # else
236 # if ! @HAVE_DECL_LOCALTIME_R@
237 _GL_FUNCDECL_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
238 struct tm *restrict __result)
239 _GL_ARG_NONNULL ((1, 2)));
240 # endif
241 _GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer,
242 struct tm *restrict __result));
243 # endif
244 # if @HAVE_DECL_LOCALTIME_R@
245 _GL_CXXALIASWARN (gmtime_r);
246 # endif
247 # endif
249 /* Convert TIMER to RESULT, assuming local time and UTC respectively. See
250 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html> and
251 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html>. */
252 # if @GNULIB_LOCALTIME@ || @REPLACE_LOCALTIME@
253 # if @REPLACE_LOCALTIME@
254 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
255 # undef localtime
256 # define localtime rpl_localtime
257 # endif
258 _GL_FUNCDECL_RPL (localtime, struct tm *, (time_t const *__timer)
259 _GL_ARG_NONNULL ((1)));
260 _GL_CXXALIAS_RPL (localtime, struct tm *, (time_t const *__timer));
261 # else
262 _GL_CXXALIAS_SYS (localtime, struct tm *, (time_t const *__timer));
263 # endif
264 # if __GLIBC__ >= 2
265 _GL_CXXALIASWARN (localtime);
266 # endif
267 # endif
269 # if 0 || @REPLACE_GMTIME@
270 # if @REPLACE_GMTIME@
271 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
272 # undef gmtime
273 # define gmtime rpl_gmtime
274 # endif
275 _GL_FUNCDECL_RPL (gmtime, struct tm *, (time_t const *__timer)
276 _GL_ARG_NONNULL ((1)));
277 _GL_CXXALIAS_RPL (gmtime, struct tm *, (time_t const *__timer));
278 # else
279 _GL_CXXALIAS_SYS (gmtime, struct tm *, (time_t const *__timer));
280 # endif
281 _GL_CXXALIASWARN (gmtime);
282 # endif
284 /* Parse BUF as a timestamp, assuming FORMAT specifies its layout, and store
285 the resulting broken-down time into TM. See
286 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>. */
287 # if @GNULIB_STRPTIME@
288 # if ! @HAVE_STRPTIME@
289 _GL_FUNCDECL_SYS (strptime, char *, (char const *restrict __buf,
290 char const *restrict __format,
291 struct tm *restrict __tm)
292 _GL_ARG_NONNULL ((1, 2, 3)));
293 # endif
294 _GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf,
295 char const *restrict __format,
296 struct tm *restrict __tm));
297 _GL_CXXALIASWARN (strptime);
298 # endif
300 /* Convert *TP to a date and time string. See
301 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime.html>. */
302 # if @GNULIB_CTIME@
303 # if @REPLACE_CTIME@
304 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
305 # define ctime rpl_ctime
306 # endif
307 _GL_FUNCDECL_RPL (ctime, char *, (time_t const *__tp)
308 _GL_ARG_NONNULL ((1)));
309 _GL_CXXALIAS_RPL (ctime, char *, (time_t const *__tp));
310 # else
311 _GL_CXXALIAS_SYS (ctime, char *, (time_t const *__tp));
312 # endif
313 # if __GLIBC__ >= 2
314 _GL_CXXALIASWARN (ctime);
315 # endif
316 # endif
318 /* Convert *TP to a date and time string. See
319 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>. */
320 # if @GNULIB_STRFTIME@
321 # if @REPLACE_STRFTIME@
322 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
323 # define strftime rpl_strftime
324 # endif
325 _GL_FUNCDECL_RPL (strftime, size_t,
326 (char *restrict __buf, size_t __bufsize,
327 const char *restrict __fmt, const struct tm *restrict __tp)
328 _GL_ARG_NONNULL ((1, 3, 4)));
329 _GL_CXXALIAS_RPL (strftime, size_t,
330 (char *restrict __buf, size_t __bufsize,
331 const char *restrict __fmt, const struct tm *restrict __tp));
332 # else
333 _GL_CXXALIAS_SYS (strftime, size_t,
334 (char *restrict __buf, size_t __bufsize,
335 const char *restrict __fmt, const struct tm *restrict __tp));
336 # endif
337 # if __GLIBC__ >= 2
338 _GL_CXXALIASWARN (strftime);
339 # endif
340 # endif
342 # if defined _GNU_SOURCE && @GNULIB_TIME_RZ@ && ! @HAVE_TIMEZONE_T@
343 typedef struct tm_zone *timezone_t;
344 _GL_FUNCDECL_SYS (tzalloc, timezone_t, (char const *__name));
345 _GL_CXXALIAS_SYS (tzalloc, timezone_t, (char const *__name));
346 _GL_FUNCDECL_SYS (tzfree, void, (timezone_t __tz));
347 _GL_CXXALIAS_SYS (tzfree, void, (timezone_t __tz));
348 _GL_FUNCDECL_SYS (localtime_rz, struct tm *,
349 (timezone_t __tz, time_t const *restrict __timer,
350 struct tm *restrict __result) _GL_ARG_NONNULL ((2, 3)));
351 _GL_CXXALIAS_SYS (localtime_rz, struct tm *,
352 (timezone_t __tz, time_t const *restrict __timer,
353 struct tm *restrict __result));
354 _GL_FUNCDECL_SYS (mktime_z, time_t,
355 (timezone_t __tz, struct tm *restrict __result)
356 _GL_ARG_NONNULL ((2)));
357 _GL_CXXALIAS_SYS (mktime_z, time_t,
358 (timezone_t __tz, struct tm *restrict __result));
359 # endif
361 /* Convert TM to a time_t value, assuming UTC. */
362 # if @GNULIB_TIMEGM@
363 # if @REPLACE_TIMEGM@
364 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
365 # undef timegm
366 # define timegm rpl_timegm
367 # endif
368 _GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
369 _GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm));
370 # else
371 # if ! @HAVE_TIMEGM@
372 _GL_FUNCDECL_SYS (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1)));
373 # endif
374 _GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm));
375 # endif
376 _GL_CXXALIASWARN (timegm);
377 # endif
379 /* Encourage applications to avoid unsafe functions that can overrun
380 buffers when given outlandish struct tm values. Portable
381 applications should use strftime (or even sprintf) instead. */
382 # if defined GNULIB_POSIXCHECK
383 # undef asctime
384 _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
385 "better use strftime (or even sprintf) instead");
386 # endif
387 # if defined GNULIB_POSIXCHECK
388 # undef asctime_r
389 _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
390 "better use strftime (or even sprintf) instead");
391 # endif
392 # if defined GNULIB_POSIXCHECK
393 # undef ctime
394 _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
395 "better use strftime (or even sprintf) instead");
396 # endif
397 # if defined GNULIB_POSIXCHECK
398 # undef ctime_r
399 _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
400 "better use strftime (or even sprintf) instead");
401 # endif
403 #endif