1#ifndef _TIME_H
2#include <time/time.h>
3
4#ifndef _ISOMAC
5# include <bits/types/struct_timeval.h>
6# include <struct___timespec64.h>
7# include <struct___timeval64.h>
8# include <bits/types/locale_t.h>
9# include <stdbool.h>
10# include <time/mktime-internal.h>
11# include <sys/time.h>
12# include <time-clockid.h>
13# include <sys/time.h>
14
15extern __typeof (strftime_l) __strftime_l;
16libc_hidden_proto (__strftime_l)
17extern __typeof (strptime_l) __strptime_l;
18
19libc_hidden_proto (asctime)
20libc_hidden_proto (mktime)
21libc_hidden_proto (timelocal)
22libc_hidden_proto (localtime)
23libc_hidden_proto (strftime)
24libc_hidden_proto (strptime)
25
26extern __typeof (clock_gettime) __clock_gettime;
27libc_hidden_proto (__clock_gettime)
28extern __typeof (clock_settime) __clock_settime;
29libc_hidden_proto (__clock_settime)
30
31extern __typeof (clock_getres) __clock_getres;
32libc_hidden_proto (__clock_getres)
33
34extern __typeof (clock_nanosleep) __clock_nanosleep;
35libc_hidden_proto (__clock_nanosleep);
36
37#ifdef __linux__
38extern __typeof (clock_adjtime) __clock_adjtime;
39libc_hidden_proto (__clock_adjtime);
40#endif
41
42/* Now define the internal interfaces. */
43struct tm;
44
45/* Defined in mktime.c. */
46extern const unsigned short int __mon_yday[2][13] attribute_hidden;
47
48/* Defined in localtime.c. */
49extern struct tm _tmbuf attribute_hidden;
50
51/* Defined in tzset.c. */
52extern char *__tzstring (const char *string) attribute_hidden;
53
54extern int __use_tzfile attribute_hidden;
55
56extern void __tzfile_read (const char *file, size_t extra,
57 char **extrap) attribute_hidden;
58extern void __tzfile_compute (__time64_t timer, int use_localtime,
59 long int *leap_correct, int *leap_hit,
60 struct tm *tp) attribute_hidden;
61extern void __tzfile_default (const char *std, const char *dst,
62 int stdoff, int dstoff)
63 attribute_hidden;
64extern void __tzset_parse_tz (const char *tz) attribute_hidden;
65extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
66 __THROW attribute_hidden;
67
68
69#if __TIMESIZE == 64
70# define __itimerspec64 itimerspec
71#else
72/* The glibc's internal representation of the struct itimerspec. */
73struct __itimerspec64
74{
75 struct __timespec64 it_interval;
76 struct __timespec64 it_value;
77};
78#endif
79
80#if __TIMESIZE == 64
81# define __utimbuf64 utimbuf
82# define __itimerval64 itimerval
83#else
84/* The glibc Y2038-proof struct __utimbuf64 structure for file's access
85 and modification time values. */
86struct __utimbuf64
87{
88 __time64_t actime; /* Access time. */
89 __time64_t modtime; /* Modification time. */
90};
91/* The glibc's internal representation of the struct itimerval. */
92struct __itimerval64
93{
94 struct __timeval64 it_interval;
95 struct __timeval64 it_value;
96};
97#endif
98
99#if __TIMESIZE == 64
100# define __getitimer64 __getitimer
101# define __setitimer64 __setitimer
102#else
103extern int __getitimer64 (enum __itimer_which __which,
104 struct __itimerval64 *__value);
105
106libc_hidden_proto (__getitimer64)
107extern int __setitimer64 (enum __itimer_which __which,
108 const struct __itimerval64 *__restrict __new,
109 struct __itimerval64 *__restrict __old);
110libc_hidden_proto (__setitimer64)
111#endif
112
113#if __TIMESIZE == 64
114# define __ctime64 ctime
115#else
116extern char *__ctime64 (const __time64_t *__timer) __THROW;
117libc_hidden_proto (__ctime64)
118#endif
119
120#if __TIMESIZE == 64
121# define __ctime64_r ctime_r
122#else
123extern char *__ctime64_r (const __time64_t *__restrict __timer,
124 char *__restrict __buf) __THROW;
125libc_hidden_proto (__ctime64_r)
126#endif
127
128#if __TIMESIZE == 64
129# define __localtime64 localtime
130#else
131extern struct tm *__localtime64 (const __time64_t *__timer);
132libc_hidden_proto (__localtime64)
133#endif
134
135extern struct tm *__localtime_r (const time_t *__timer,
136 struct tm *__tp) attribute_hidden;
137#if __TIMESIZE != 64
138extern struct tm *__localtime64_r (const __time64_t *__timer,
139 struct tm *__tp);
140libc_hidden_proto (__localtime64_r)
141
142extern __time64_t __mktime64 (struct tm *__tp) __THROW;
143libc_hidden_proto (__mktime64)
144#endif
145
146extern struct tm *__gmtime_r (const time_t *__restrict __timer,
147 struct tm *__restrict __tp);
148libc_hidden_proto (__gmtime_r)
149
150#if __TIMESIZE == 64
151# define __gmtime64 gmtime
152#else
153extern struct tm *__gmtime64 (const __time64_t *__timer);
154libc_hidden_proto (__gmtime64)
155
156extern struct tm *__gmtime64_r (const __time64_t *__restrict __timer,
157 struct tm *__restrict __tp);
158libc_hidden_proto (__gmtime64_r)
159
160extern __time64_t __timegm64 (struct tm *__tp) __THROW;
161libc_hidden_proto (__timegm64)
162#endif
163
164#if __TIMESIZE == 64
165# define __clock_settime64 __clock_settime
166#else
167extern int __clock_settime64 (clockid_t clock_id,
168 const struct __timespec64 *tp);
169libc_hidden_proto (__clock_settime64)
170#endif
171
172#if __TIMESIZE == 64
173# define __clock_getres64 __clock_getres
174#else
175extern int __clock_getres64 (clockid_t clock_id,
176 struct __timespec64 *tp);
177libc_hidden_proto (__clock_getres64);
178#endif
179
180#if __TIMESIZE == 64
181# define __utime64 __utime
182# define __utimes64 __utimes
183# define __utimensat64 __utimensat
184#else
185extern int __utime64 (const char *file, const struct __utimbuf64 *times);
186libc_hidden_proto (__utime64)
187extern int __utimes64 (const char *file, const struct __timeval64 tvp[2]);
188libc_hidden_proto (__utimes64)
189extern int __utimensat64 (int fd, const char *file,
190 const struct __timespec64 tsp[2], int flags);
191libc_hidden_proto (__utimensat64);
192#endif
193
194extern int __utimensat64_helper (int fd, const char *file,
195 const struct __timespec64 tsp[2], int flags);
196libc_hidden_proto (__utimensat64_helper);
197
198#if __TIMESIZE == 64
199# define __futimes64 __futimes
200# define __futimesat64 __futimesat
201# define __lutimes64 __lutimes
202# define __futimens64 __futimens
203#else
204extern int __futimes64 (int fd, const struct __timeval64 tvp64[2]);
205libc_hidden_proto (__futimes64);
206extern int __futimesat64 (int fd, const char *file,
207 const struct __timeval64 tvp[2]);
208libc_hidden_proto (__futimesat64);
209extern int __lutimes64 (const char *file, const struct __timeval64 tvp64[2]);
210libc_hidden_proto (__lutimes64);
211extern int __futimens64 (int fd, const struct __timespec64 tsp[2]);
212libc_hidden_proto (__futimens64);
213#endif
214
215#if __TIMESIZE == 64
216# define __timer_gettime64 __timer_gettime
217# define __timerfd_gettime64 __timerfd_gettime
218#else
219extern int __timer_gettime64 (timer_t timerid, struct __itimerspec64 *value);
220extern int __timerfd_gettime64 (int fd, struct __itimerspec64 *value);
221# if PTHREAD_IN_LIBC
222libc_hidden_proto (__timer_gettime64)
223# else
224librt_hidden_proto (__timer_gettime64)
225# endif
226libc_hidden_proto (__timerfd_gettime64);
227#endif
228
229#if __TIMESIZE == 64
230# define __timer_settime64 __timer_settime
231# define __timerfd_settime64 __timerfd_settime
232#else
233extern int __timer_settime64 (timer_t timerid, int flags,
234 const struct __itimerspec64 *value,
235 struct __itimerspec64 *ovalue);
236extern int __timerfd_settime64 (int fd, int flags,
237 const struct __itimerspec64 *value,
238 struct __itimerspec64 *ovalue);
239# if PTHREAD_IN_LIBC
240libc_hidden_proto (__timer_settime64)
241#else
242librt_hidden_proto (__timer_settime64)
243#endif
244libc_hidden_proto (__timerfd_settime64);
245#endif
246
247#if __TIMESIZE == 64
248# define __sched_rr_get_interval64 __sched_rr_get_interval
249#else
250extern int __sched_rr_get_interval64 (pid_t pid, struct __timespec64 *tp);
251libc_hidden_proto (__sched_rr_get_interval64);
252#endif
253
254#if __TIMESIZE == 64
255# define __settimeofday64 __settimeofday
256# define __gettimeofday64 __gettimeofday
257#else
258extern int __settimeofday64 (const struct __timeval64 *tv,
259 const struct timezone *tz);
260libc_hidden_proto (__settimeofday64)
261extern int __gettimeofday64 (struct __timeval64 *restrict tv,
262 void *restrict tz);
263libc_hidden_proto (__gettimeofday64)
264#endif
265
266/* Compute the `struct tm' representation of T,
267 offset OFFSET seconds east of UTC,
268 and store year, yday, mon, mday, wday, hour, min, sec into *TP.
269 Return nonzero if successful. */
270extern int __offtime (__time64_t __timer,
271 long int __offset,
272 struct tm *__tp) attribute_hidden;
273
274extern char *__asctime_r (const struct tm *__tp, char *__buf)
275 attribute_hidden;
276extern void __tzset (void) attribute_hidden;
277
278/* Prototype for the internal function to get information based on TZ. */
279extern struct tm *__tz_convert (__time64_t timer, int use_localtime,
280 struct tm *tp) attribute_hidden;
281
282extern int __nanosleep (const struct timespec *__requested_time,
283 struct timespec *__remaining);
284hidden_proto (__nanosleep)
285#if __TIMESIZE == 64
286# define __nanosleep64 __nanosleep
287#else
288extern int __nanosleep64 (const struct __timespec64 *__requested_time,
289 struct __timespec64 *__remaining);
290hidden_proto (__nanosleep64)
291#endif
292
293
294extern int __getdate_r (const char *__string, struct tm *__resbufp);
295libc_hidden_proto (__getdate_r);
296
297
298/* Determine CLK_TCK value. */
299extern int __getclktck (void) attribute_hidden;
300
301
302/* strptime support. */
303extern char * __strptime_internal (const char *rp, const char *fmt,
304 struct tm *tm, void *statep,
305 locale_t locparam) attribute_hidden;
306
307#if __TIMESIZE == 64
308# define __difftime64 __difftime
309#else
310extern double __difftime64 (__time64_t time1, __time64_t time0);
311libc_hidden_proto (__difftime64)
312#endif
313
314extern double __difftime (time_t time1, time_t time0);
315
316#if __TIMESIZE == 64
317# define __clock_nanosleep_time64 __clock_nanosleep
318# define __clock_gettime64 __clock_gettime
319# define __timespec_get64 __timespec_get
320# define __timespec_getres64 __timespec_getres
321#else
322extern int __clock_nanosleep_time64 (clockid_t clock_id,
323 int flags, const struct __timespec64 *req,
324 struct __timespec64 *rem);
325libc_hidden_proto (__clock_nanosleep_time64)
326extern int __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp);
327libc_hidden_proto (__clock_gettime64)
328extern int __timespec_get64 (struct __timespec64 *ts, int base);
329libc_hidden_proto (__timespec_get64)
330extern int __timespec_getres64 (struct __timespec64 *ts, int base);
331libc_hidden_proto (__timespec_getres64)
332#endif
333
334#if __TIMESIZE == 64
335# define __time64 __time
336#else
337extern __time64_t __time64 (__time64_t *timer);
338libc_hidden_proto (__time64)
339#endif
340
341/* Use in the clock_* functions. Size of the field representing the
342 actual clock ID. */
343#define CLOCK_IDFIELD_SIZE 3
344
345/* Check whether T fits in time_t. */
346static inline bool
347in_time_t_range (__time64_t t)
348{
349 time_t s = t;
350 return s == t;
351}
352
353/* Convert a known valid struct timeval into a struct __timespec64. */
354static inline struct __timespec64
355valid_timeval_to_timespec64 (const struct timeval tv)
356{
357 struct __timespec64 ts64;
358
359 ts64.tv_sec = tv.tv_sec;
360 ts64.tv_nsec = tv.tv_usec * 1000;
361
362 return ts64;
363}
364
365/* Convert a known valid struct timeval into a struct __timeval64. */
366static inline struct __timeval64
367valid_timeval_to_timeval64 (const struct timeval tv)
368{
369 struct __timeval64 tv64;
370
371 tv64.tv_sec = tv.tv_sec;
372 tv64.tv_usec = tv.tv_usec;
373
374 return tv64;
375}
376
377/* Convert a valid and within range of struct timeval, struct
378 __timeval64 into a struct timeval. */
379static inline struct timeval
380valid_timeval64_to_timeval (const struct __timeval64 tv64)
381{
382 struct timeval tv;
383
384 tv.tv_sec = (time_t) tv64.tv_sec;
385 tv.tv_usec = (suseconds_t) tv64.tv_usec;
386
387 return tv;
388}
389
390/* Convert a struct __timeval64 into a struct __timespec64. */
391static inline struct __timespec64
392timeval64_to_timespec64 (const struct __timeval64 tv64)
393{
394 struct __timespec64 ts64;
395
396 ts64.tv_sec = tv64.tv_sec;
397 ts64.tv_nsec = tv64.tv_usec * 1000;
398
399 return ts64;
400}
401
402/* Convert a known valid struct timespec into a struct __timespec64. */
403static inline struct __timespec64
404valid_timespec_to_timespec64 (const struct timespec ts)
405{
406 struct __timespec64 ts64;
407
408 ts64.tv_sec = ts.tv_sec;
409 ts64.tv_nsec = ts.tv_nsec;
410
411 return ts64;
412}
413
414/* Convert a valid and within range of struct timespec, struct
415 __timespec64 into a struct timespec. */
416static inline struct timespec
417valid_timespec64_to_timespec (const struct __timespec64 ts64)
418{
419 struct timespec ts;
420
421 ts.tv_sec = (time_t) ts64.tv_sec;
422 ts.tv_nsec = ts64.tv_nsec;
423
424 return ts;
425}
426
427/* Convert a valid and within range of struct timeval struct
428 __timespec64 into a struct timeval. */
429static inline struct timeval
430valid_timespec64_to_timeval (const struct __timespec64 ts64)
431{
432 struct timeval tv;
433
434 tv.tv_sec = (time_t) ts64.tv_sec;
435 tv.tv_usec = ts64.tv_nsec / 1000;
436
437 return tv;
438}
439
440/* Convert a struct __timespec64 into a struct __timeval64. */
441static inline struct __timeval64
442timespec64_to_timeval64 (const struct __timespec64 ts64)
443{
444 struct __timeval64 tv64;
445
446 tv64.tv_sec = ts64.tv_sec;
447 tv64.tv_usec = ts64.tv_nsec / 1000;
448
449 return tv64;
450}
451
452/* A version of 'struct timeval' with 32-bit time_t
453 and suseconds_t. */
454struct __timeval32
455{
456 __int32_t tv_sec; /* Seconds. */
457 __int32_t tv_usec; /* Microseconds. */
458};
459
460/* Conversion functions for converting to/from __timeval32 */
461static inline struct __timeval64
462valid_timeval32_to_timeval64 (const struct __timeval32 tv)
463{
464 return (struct __timeval64) { tv.tv_sec, tv.tv_usec };
465}
466
467static inline struct __timeval32
468valid_timeval64_to_timeval32 (const struct __timeval64 tv64)
469{
470 return (struct __timeval32) { tv64.tv_sec, tv64.tv_usec };
471}
472
473static inline struct timeval
474valid_timeval32_to_timeval (const struct __timeval32 tv)
475{
476 return (struct timeval) { tv.tv_sec, tv.tv_usec };
477}
478
479static inline struct __timeval32
480valid_timeval_to_timeval32 (const struct timeval tv)
481{
482 return (struct __timeval32) { tv.tv_sec, tv.tv_usec };
483}
484
485static inline struct timespec
486valid_timeval32_to_timespec (const struct __timeval32 tv)
487{
488 return (struct timespec) { tv.tv_sec, tv.tv_usec * 1000 };
489}
490
491static inline struct __timeval32
492valid_timespec_to_timeval32 (const struct timespec ts)
493{
494 return (struct __timeval32) { (time_t) ts.tv_sec, ts.tv_nsec / 1000 };
495}
496
497static inline struct __timeval64
498valid_timespec_to_timeval64 (const struct timespec ts)
499{
500 return (struct __timeval64) { (time_t) ts.tv_sec, ts.tv_nsec / 1000 };
501}
502
503/* Check if a value is in the valid nanoseconds range. Return true if
504 it is, false otherwise. */
505static inline bool
506valid_nanoseconds (__syscall_slong_t ns)
507{
508 return __glibc_likely (0 <= ns && ns < 1000000000);
509}
510
511/* Helper function to get time in seconds, similar to time. */
512static inline time_t
513time_now (void)
514{
515 struct timespec ts;
516 __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
517 return ts.tv_sec;
518}
519
520static inline __time64_t
521time64_now (void)
522{
523 struct __timespec64 ts;
524 __clock_gettime64 (TIME_CLOCK_GETTIME_CLOCKID, &ts);
525 return ts.tv_sec;
526}
527
528#define NSEC_PER_SEC 1000000000L /* Nanoseconds per second. */
529#define USEC_PER_SEC 1000000L /* Microseconds per second. */
530#define NSEC_PER_USEC 1000L /* Nanoseconds per microsecond. */
531
532#endif
533
534#endif
535