1 /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.23 Date and time <time.h>
31 /* This defines CLOCKS_PER_SEC, which is the number of processor clock
32 ticks per second, and possibly a number of other constants. */
33 #include <bits/time.h>
35 /* Many of the typedefs and structs whose official home is this header
36 may also need to be defined by other headers. */
37 #include <bits/types/clock_t.h>
38 #include <bits/types/time_t.h>
39 #include <bits/types/struct_tm.h>
41 #if defined __USE_POSIX199309 || defined __USE_ISOC11
42 # include <bits/types/struct_timespec.h>
45 #ifdef __USE_POSIX199309
46 # include <bits/types/clockid_t.h>
47 # include <bits/types/timer_t.h>
48 # include <bits/types/struct_itimerspec.h>
53 # ifndef __pid_t_defined
54 typedef __pid_t pid_t
;
55 # define __pid_t_defined
60 # include <bits/types/locale_t.h>
64 /* Time base values for timespec_get. */
70 /* Time used by the program so far (user time + system time).
71 The result / CLOCKS_PER_SEC is program time in seconds. */
72 extern clock_t clock (void) __THROW
;
74 #ifndef __USE_TIME_BITS64
75 /* Return the current time and put it in *TIMER if TIMER is not NULL. */
76 extern time_t time (time_t *__timer
) __THROW
;
78 /* Return the difference between TIME1 and TIME0. */
79 extern double difftime (time_t __time1
, time_t __time0
)
80 __THROW
__attribute__ ((__const__
));
82 /* Return the `time_t' representation of TP and normalize TP. */
83 extern time_t mktime (struct tm
*__tp
) __THROW
;
85 # ifdef __REDIRECT_NTH
86 extern time_t __REDIRECT_NTH (time
, (time_t *__timer
), __time64
);
87 extern double __REDIRECT_NTH (difftime
, (time_t __time1
, time_t __time0
),
88 __difftime64
) __attribute__ ((__const__
));
89 extern time_t __REDIRECT_NTH (mktime
, (struct tm
*__tp
), __mktime64
);
91 # define time __time64
92 # define difftime __difftime64
93 # define mktime __mktime64
97 /* Format TP into S according to FORMAT.
98 Write no more than MAXSIZE characters and return the number
99 of characters written, or 0 if it would exceed MAXSIZE. */
100 extern size_t strftime (char *__restrict __s
, size_t __maxsize
,
101 const char *__restrict __format
,
102 const struct tm
*__restrict __tp
) __THROW
;
105 /* Parse S according to FORMAT and store binary time information in TP.
106 The return value is a pointer to the first unparsed character in S. */
107 extern char *strptime (const char *__restrict __s
,
108 const char *__restrict __fmt
, struct tm
*__tp
)
112 #ifdef __USE_XOPEN2K8
113 /* Similar to the two functions above but take the information from
114 the provided locale and not the global locale. */
116 extern size_t strftime_l (char *__restrict __s
, size_t __maxsize
,
117 const char *__restrict __format
,
118 const struct tm
*__restrict __tp
,
119 locale_t __loc
) __THROW
;
123 extern char *strptime_l (const char *__restrict __s
,
124 const char *__restrict __fmt
, struct tm
*__tp
,
125 locale_t __loc
) __THROW
;
129 #ifndef __USE_TIME_BITS64
130 /* Return the `struct tm' representation of *TIMER
131 in Universal Coordinated Time (aka Greenwich Mean Time). */
132 extern struct tm
*gmtime (const time_t *__timer
) __THROW
;
134 /* Return the `struct tm' representation
135 of *TIMER in the local timezone. */
136 extern struct tm
*localtime (const time_t *__timer
) __THROW
;
139 # ifdef __REDIRECT_NTH
140 extern struct tm
*__REDIRECT_NTH (gmtime
, (const time_t *__timer
), __gmtime64
);
141 extern struct tm
*__REDIRECT_NTH (localtime
, (const time_t *__timer
),
144 # define gmtime __gmtime64
145 # define localtime __localtime64
150 #if defined __USE_POSIX || __GLIBC_USE (ISOC2X)
151 # ifndef __USE_TIME_BITS64
152 /* Return the `struct tm' representation of *TIMER in UTC,
153 using *TP to store the result. */
154 extern struct tm
*gmtime_r (const time_t *__restrict __timer
,
155 struct tm
*__restrict __tp
) __THROW
;
157 /* Return the `struct tm' representation of *TIMER in local time,
158 using *TP to store the result. */
159 extern struct tm
*localtime_r (const time_t *__restrict __timer
,
160 struct tm
*__restrict __tp
) __THROW
;
162 # ifdef __REDIRECT_NTH
163 extern struct tm
*__REDIRECT_NTH (gmtime_r
, (const time_t *__restrict __timer
,
164 struct tm
*__restrict __tp
),
167 extern struct tm
*__REDIRECT_NTH (localtime_r
, (const time_t *__restrict __t
,
168 struct tm
*__restrict __tp
),
171 # define gmtime_r __gmtime64_r
172 # define localtime_r __localtime_r
175 #endif /* POSIX || C2X */
177 /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
178 that is the representation of TP in this format. */
179 extern char *asctime (const struct tm
*__tp
) __THROW
;
181 /* Equivalent to `asctime (localtime (timer))'. */
182 #ifndef __USE_TIME_BITS64
183 extern char *ctime (const time_t *__timer
) __THROW
;
185 # ifdef __REDIRECT_NTH
186 extern char *__REDIRECT_NTH (ctime
, (const time_t *__timer
), __ctime64
);
188 # define ctime __ctime64
193 /* Reentrant versions of the above functions. */
195 /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"
196 that is the representation of TP in this format. */
197 extern char *asctime_r (const struct tm
*__restrict __tp
,
198 char *__restrict __buf
) __THROW
;
200 /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */
201 #ifndef __USE_TIME_BITS64
202 extern char *ctime_r (const time_t *__restrict __timer
,
203 char *__restrict __buf
) __THROW
;
205 # ifdef __REDIRECT_NTH
206 extern char *__REDIRECT_NTH (ctime_r
, (const time_t *__restrict __timer
,
207 char *__restrict __buf
), __ctime64_r
);
209 # define ctime_r __ctime64_r
216 /* Defined in localtime.c. */
217 extern char *__tzname
[2]; /* Current timezone names. */
218 extern int __daylight
; /* If daylight-saving time is ever in use. */
219 extern long int __timezone
; /* Seconds west of UTC. */
224 extern char *tzname
[2];
226 /* Set time conversion information from the TZ environment variable.
227 If TZ is not defined, a locale-dependent default is used. */
228 extern void tzset (void) __THROW
;
231 #if defined __USE_MISC || defined __USE_XOPEN
233 extern long int timezone
;
237 /* Nonzero if YEAR is a leap year (every 4 years,
238 except every 100th isn't, and every 400th is). */
239 #define __isleap(year) \
240 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
244 /* Miscellaneous functions many Unices inherited from the public domain
245 localtime package. These are included only for compatibility. */
247 #ifndef __USE_TIME_BITS64
248 /* Like `mktime', but for TP represents Universal Time, not local time. */
249 extern time_t timegm (struct tm
*__tp
) __THROW
;
250 /* Another name for `mktime'. */
251 extern time_t timelocal (struct tm
*__tp
) __THROW
;
253 # ifdef __REDIRECT_NTH
254 extern time_t __REDIRECT_NTH (timegm
, (struct tm
*__tp
), __timegm64
);
255 extern time_t __REDIRECT_NTH (timelocal
, (struct tm
*__tp
), __mktime64
);
257 # define timegm __timegm64
261 /* Return the number of days in YEAR. */
262 extern int dysize (int __year
) __THROW
__attribute__ ((__const__
));
266 #ifdef __USE_POSIX199309
267 # ifndef __USE_TIME_BITS64
268 /* Pause execution for a number of nanoseconds.
270 This function is a cancellation point and therefore not marked with
272 extern int nanosleep (const struct timespec
*__requested_time
,
273 struct timespec
*__remaining
);
275 /* Get resolution of clock CLOCK_ID. */
276 extern int clock_getres (clockid_t __clock_id
, struct timespec
*__res
) __THROW
;
278 /* Get current value of clock CLOCK_ID and store it in TP. */
279 extern int clock_gettime (clockid_t __clock_id
, struct timespec
*__tp
)
280 __THROW
__nonnull((2));
282 /* Set clock CLOCK_ID to value TP. */
283 extern int clock_settime (clockid_t __clock_id
, const struct timespec
*__tp
)
284 __THROW
__nonnull((2));
287 extern int __REDIRECT (nanosleep
, (const struct timespec
*__requested_time
,
288 struct timespec
*__remaining
),
290 extern int __REDIRECT_NTH (clock_getres
, (clockid_t __clock_id
,
291 struct timespec
*__res
),
293 extern int __REDIRECT_NTH (clock_gettime
, (clockid_t __clock_id
, struct
294 timespec
*__tp
), __clock_gettime64
)
296 extern int __REDIRECT_NTH (clock_settime
, (clockid_t __clock_id
, const struct
297 timespec
*__tp
), __clock_settime64
)
300 # define nanosleep __nanosleep64
301 # define clock_getres __clock_getres64
302 # define clock_gettime __clock_gettime64
303 # define clock_settime __clock_settime64
308 # ifdef __USE_XOPEN2K
309 /* High-resolution sleep with the specified clock.
311 This function is a cancellation point and therefore not marked with
313 # ifndef __USE_TIME_BITS64
314 extern int clock_nanosleep (clockid_t __clock_id
, int __flags
,
315 const struct timespec
*__req
,
316 struct timespec
*__rem
);
319 extern int __REDIRECT (clock_nanosleep
, (clockid_t __clock_id
, int __flags
,
320 const struct timespec
*__req
,
321 struct timespec
*__rem
),
322 __clock_nanosleep_time64
);
324 # define clock_nanosleep __clock_nanosleep_time64
328 /* Return clock ID for CPU-time clock. */
329 extern int clock_getcpuclockid (pid_t __pid
, clockid_t
*__clock_id
) __THROW
;
333 /* Create new per-process timer using CLOCK_ID. */
334 extern int timer_create (clockid_t __clock_id
,
335 struct sigevent
*__restrict __evp
,
336 timer_t
*__restrict __timerid
) __THROW
;
338 /* Delete timer TIMERID. */
339 extern int timer_delete (timer_t __timerid
) __THROW
;
341 /* Set timer TIMERID to VALUE, returning old value in OVALUE. */
342 # ifndef __USE_TIME_BITS64
343 extern int timer_settime (timer_t __timerid
, int __flags
,
344 const struct itimerspec
*__restrict __value
,
345 struct itimerspec
*__restrict __ovalue
) __THROW
;
347 /* Get current value of timer TIMERID and store it in VALUE. */
348 extern int timer_gettime (timer_t __timerid
, struct itimerspec
*__value
)
351 # ifdef __REDIRECT_NTH
352 extern int __REDIRECT_NTH (timer_settime
, (timer_t __timerid
, int __flags
,
353 const struct itimerspec
*__restrict __value
,
354 struct itimerspec
*__restrict __ovalue
),
357 extern int __REDIRECT_NTH (timer_gettime
, (timer_t __timerid
,
358 struct itimerspec
*__value
),
361 # define timer_settime __timer_settime64
362 # define timer_gettime __timer_gettime64
366 /* Get expiration overrun for timer TIMERID. */
367 extern int timer_getoverrun (timer_t __timerid
) __THROW
;
372 # ifndef __USE_TIME_BITS64
373 /* Set TS to calendar time based in time base BASE. */
374 extern int timespec_get (struct timespec
*__ts
, int __base
)
375 __THROW
__nonnull ((1));
377 # ifdef __REDIRECT_NTH
378 extern int __REDIRECT_NTH (timespec_get
, (struct timespec
*__ts
, int __base
),
379 __timespec_get64
) __nonnull ((1));
381 # define timespec_get __timespec_get64
387 #if __GLIBC_USE (ISOC2X)
388 # ifndef __USE_TIME_BITS64
389 /* Set TS to resolution of time base BASE. */
390 extern int timespec_getres (struct timespec
*__ts
, int __base
)
393 # ifdef __REDIRECT_NTH
394 extern int __REDIRECT_NTH (timespec_getres
, (struct timespec
*__ts
,
396 __timespec_getres64
);
398 # define timespec_getres __timespec_getres64
404 #ifdef __USE_XOPEN_EXTENDED
405 /* Set to one of the following values to indicate an error.
406 1 the DATEMSK environment variable is null or undefined,
407 2 the template file cannot be opened for reading,
408 3 failed to get file status information,
409 4 the template file is not a regular file,
410 5 an error is encountered while reading the template file,
411 6 memory allication failed (not enough memory available),
412 7 there is no line in the template that matches the input,
413 8 invalid input specification Example: February 31 or a time is
414 specified that can not be represented in a time_t (representing
415 the time in seconds since 00:00:00 UTC, January 1, 1970) */
416 extern int getdate_err
;
418 /* Parse the given string as a date specification and return a value
419 representing the value. The templates from the file identified by
420 the environment variable DATEMSK are used. In case of an error
421 `getdate_err' is set.
423 This function is a possible cancellation point and therefore not
424 marked with __THROW. */
425 extern struct tm
*getdate (const char *__string
);
429 /* Since `getdate' is not reentrant because of the use of `getdate_err'
430 and the static buffer to return the result in, we provide a thread-safe
431 variant. The functionality is the same. The result is returned in
432 the buffer pointed to by RESBUFP and in case of an error the return
433 value is != 0 with the same values as given above for `getdate_err'.
435 This function is not part of POSIX and therefore no official
436 cancellation point. But due to similarity with an POSIX interface
437 or due to the implementation it is a cancellation point and
438 therefore not marked with __THROW. */
439 extern int getdate_r (const char *__restrict __string
,
440 struct tm
*__restrict __resbufp
);