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)
274extern int __getdate_r (const char *__string, struct tm *__resbufp)
275 attribute_hidden;
276
277
278/* Determine CLK_TCK value. */
279extern int __getclktck (void) attribute_hidden;
280
281
282/* strptime support. */
283extern char * __strptime_internal (const char *rp, const char *fmt,
284 struct tm *tm, void *statep,
285 locale_t locparam) attribute_hidden;
286
287#if __TIMESIZE == 64
288# define __difftime64 __difftime
289#else
290extern double __difftime64 (__time64_t time1, __time64_t time0);
291libc_hidden_proto (__difftime64)
292#endif
293
294extern double __difftime (time_t time1, time_t time0);
295
296#if __TIMESIZE == 64
297# define __clock_nanosleep_time64 __clock_nanosleep
298# define __clock_gettime64 __clock_gettime
299# define __timespec_get64 __timespec_get
300#else
301extern int __clock_nanosleep_time64 (clockid_t clock_id,
302 int flags, const struct __timespec64 *req,
303 struct __timespec64 *rem);
304libc_hidden_proto (__clock_nanosleep_time64)
305extern int __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp);
306libc_hidden_proto (__clock_gettime64)
307extern int __timespec_get64 (struct __timespec64 *ts, int base);
308libc_hidden_proto (__timespec_get64)
309#endif
310
311/* Use in the clock_* functions. Size of the field representing the
312 actual clock ID. */
313#define CLOCK_IDFIELD_SIZE 3
314
315/* Check whether T fits in time_t. */
316static inline bool
317in_time_t_range (__time64_t t)
318{
319 time_t s = t;
320 return s == t;
321}
322
323/* Convert a known valid struct timeval into a struct __timespec64. */
324static inline struct __timespec64
325valid_timeval_to_timespec64 (const struct timeval tv)
326{
327 struct __timespec64 ts64;
328
329 ts64.tv_sec = tv.tv_sec;
330 ts64.tv_nsec = tv.tv_usec * 1000;
331
332 return ts64;
333}
334
335/* Convert a known valid struct timeval into a struct __timeval64. */
336static inline struct __timeval64
337valid_timeval_to_timeval64 (const struct timeval tv)
338{
339 struct __timeval64 tv64;
340
341 tv64.tv_sec = tv.tv_sec;
342 tv64.tv_usec = tv.tv_usec;
343
344 return tv64;
345}
346
347/* Convert a valid and within range of struct timeval, struct
348 __timeval64 into a struct timeval. */
349static inline struct timeval
350valid_timeval64_to_timeval (const struct __timeval64 tv64)
351{
352 struct timeval tv;
353
354 tv.tv_sec = (time_t) tv64.tv_sec;
355 tv.tv_usec = (suseconds_t) tv64.tv_usec;
356
357 return tv;
358}
359
360/* Convert a struct __timeval64 into a struct __timespec64. */
361static inline struct __timespec64
362timeval64_to_timespec64 (const struct __timeval64 tv64)
363{
364 struct __timespec64 ts64;
365
366 ts64.tv_sec = tv64.tv_sec;
367 ts64.tv_nsec = tv64.tv_usec * 1000;
368
369 return ts64;
370}
371
372/* Convert a known valid struct timespec into a struct __timespec64. */
373static inline struct __timespec64
374valid_timespec_to_timespec64 (const struct timespec ts)
375{
376 struct __timespec64 ts64;
377
378 ts64.tv_sec = ts.tv_sec;
379 ts64.tv_nsec = ts.tv_nsec;
380
381 return ts64;
382}
383
384/* Convert a valid and within range of struct timespec, struct
385 __timespec64 into a struct timespec. */
386static inline struct timespec
387valid_timespec64_to_timespec (const struct __timespec64 ts64)
388{
389 struct timespec ts;
390
391 ts.tv_sec = (time_t) ts64.tv_sec;
392 ts.tv_nsec = ts64.tv_nsec;
393
394 return ts;
395}
396
397/* Convert a valid and within range of struct timeval struct
398 __timespec64 into a struct timeval. */
399static inline struct timeval
400valid_timespec64_to_timeval (const struct __timespec64 ts64)
401{
402 struct timeval tv;
403
404 tv.tv_sec = (time_t) ts64.tv_sec;
405 tv.tv_usec = ts64.tv_nsec / 1000;
406
407 return tv;
408}
409
410/* Convert a struct __timespec64 into a struct __timeval64. */
411static inline struct __timeval64
412timespec64_to_timeval64 (const struct __timespec64 ts64)
413{
414 struct __timeval64 tv64;
415
416 tv64.tv_sec = ts64.tv_sec;
417 tv64.tv_usec = ts64.tv_nsec / 1000;
418
419 return tv64;
420}
421
422/* A version of 'struct timeval' with 32-bit time_t
423 and suseconds_t. */
424struct __timeval32
425{
426 __int32_t tv_sec; /* Seconds. */
427 __int32_t tv_usec; /* Microseconds. */
428};
429
430/* Conversion functions for converting to/from __timeval32 */
431static inline struct __timeval64
432valid_timeval32_to_timeval64 (const struct __timeval32 tv)
433{
434 return (struct __timeval64) { tv.tv_sec, tv.tv_usec };
435}
436
437static inline struct __timeval32
438valid_timeval64_to_timeval32 (const struct __timeval64 tv64)
439{
440 return (struct __timeval32) { tv64.tv_sec, tv64.tv_usec };
441}
442
443static inline struct timeval
444valid_timeval32_to_timeval (const struct __timeval32 tv)
445{
446 return (struct timeval) { tv.tv_sec, tv.tv_usec };
447}
448
449static inline struct __timeval32
450valid_timeval_to_timeval32 (const struct timeval tv)
451{
452 return (struct __timeval32) { tv.tv_sec, tv.tv_usec };
453}
454
455static inline struct timespec
456valid_timeval32_to_timespec (const struct __timeval32 tv)
457{
458 return (struct timespec) { tv.tv_sec, tv.tv_usec * 1000 };
459}
460
461static inline struct __timeval32
462valid_timespec_to_timeval32 (const struct timespec ts)
463{
464 return (struct __timeval32) { (time_t) ts.tv_sec, ts.tv_nsec / 1000 };
465}
466
467/* Check if a value is in the valid nanoseconds range. Return true if
468 it is, false otherwise. */
469static inline bool
470valid_nanoseconds (__syscall_slong_t ns)
471{
472 return __glibc_likely (0 <= ns && ns < 1000000000);
473}
474
475/* Helper function to get time in seconds, similar to time. */
476static inline time_t
477time_now (void)
478{
479 struct timespec ts;
480 __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
481 return ts.tv_sec;
482}
483#endif
484
485#endif
486