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