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 | |
15 | extern __typeof (strftime_l) __strftime_l; |
16 | libc_hidden_proto (__strftime_l) |
17 | extern __typeof (strptime_l) __strptime_l; |
18 | |
19 | libc_hidden_proto (asctime) |
20 | libc_hidden_proto (mktime) |
21 | libc_hidden_proto (timelocal) |
22 | libc_hidden_proto (localtime) |
23 | libc_hidden_proto (strftime) |
24 | libc_hidden_proto (strptime) |
25 | |
26 | extern __typeof (clock_gettime) __clock_gettime; |
27 | libc_hidden_proto (__clock_gettime) |
28 | extern __typeof (clock_settime) __clock_settime; |
29 | libc_hidden_proto (__clock_settime) |
30 | |
31 | extern __typeof (clock_getres) __clock_getres; |
32 | libc_hidden_proto (__clock_getres) |
33 | |
34 | extern __typeof (clock_nanosleep) __clock_nanosleep; |
35 | libc_hidden_proto (__clock_nanosleep); |
36 | |
37 | #ifdef __linux__ |
38 | extern __typeof (clock_adjtime) __clock_adjtime; |
39 | libc_hidden_proto (__clock_adjtime); |
40 | #endif |
41 | |
42 | /* Now define the internal interfaces. */ |
43 | struct tm; |
44 | |
45 | /* Defined in mktime.c. */ |
46 | extern const unsigned short int __mon_yday[2][13] attribute_hidden; |
47 | |
48 | /* Defined in localtime.c. */ |
49 | extern struct tm _tmbuf attribute_hidden; |
50 | |
51 | /* Defined in tzset.c. */ |
52 | extern char *__tzstring (const char *string) attribute_hidden; |
53 | |
54 | extern int __use_tzfile attribute_hidden; |
55 | |
56 | extern void __tzfile_read (const char *file, size_t , |
57 | char **) attribute_hidden; |
58 | extern void __tzfile_compute (__time64_t timer, int use_localtime, |
59 | long int *leap_correct, int *leap_hit, |
60 | struct tm *tp) attribute_hidden; |
61 | extern void __tzfile_default (const char *std, const char *dst, |
62 | int stdoff, int dstoff) |
63 | attribute_hidden; |
64 | extern void __tzset_parse_tz (const char *tz) attribute_hidden; |
65 | extern 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. */ |
73 | struct __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. */ |
86 | struct __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. */ |
92 | struct __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 |
103 | extern int __getitimer64 (enum __itimer_which __which, |
104 | struct __itimerval64 *__value); |
105 | |
106 | libc_hidden_proto (__getitimer64) |
107 | extern int __setitimer64 (enum __itimer_which __which, |
108 | const struct __itimerval64 *__restrict __new, |
109 | struct __itimerval64 *__restrict __old); |
110 | libc_hidden_proto (__setitimer64) |
111 | #endif |
112 | |
113 | #if __TIMESIZE == 64 |
114 | # define __ctime64 ctime |
115 | #else |
116 | extern char *__ctime64 (const __time64_t *__timer) __THROW; |
117 | libc_hidden_proto (__ctime64) |
118 | #endif |
119 | |
120 | #if __TIMESIZE == 64 |
121 | # define __ctime64_r ctime_r |
122 | #else |
123 | extern char *__ctime64_r (const __time64_t *__restrict __timer, |
124 | char *__restrict __buf) __THROW; |
125 | libc_hidden_proto (__ctime64_r) |
126 | #endif |
127 | |
128 | #if __TIMESIZE == 64 |
129 | # define __localtime64 localtime |
130 | #else |
131 | extern struct tm *__localtime64 (const __time64_t *__timer); |
132 | libc_hidden_proto (__localtime64) |
133 | #endif |
134 | |
135 | extern struct tm *__localtime_r (const time_t *__timer, |
136 | struct tm *__tp) attribute_hidden; |
137 | #if __TIMESIZE != 64 |
138 | extern struct tm *__localtime64_r (const __time64_t *__timer, |
139 | struct tm *__tp); |
140 | libc_hidden_proto (__localtime64_r) |
141 | |
142 | extern __time64_t __mktime64 (struct tm *__tp) __THROW; |
143 | libc_hidden_proto (__mktime64) |
144 | #endif |
145 | |
146 | extern struct tm *__gmtime_r (const time_t *__restrict __timer, |
147 | struct tm *__restrict __tp); |
148 | libc_hidden_proto (__gmtime_r) |
149 | |
150 | #if __TIMESIZE == 64 |
151 | # define __gmtime64 gmtime |
152 | #else |
153 | extern struct tm *__gmtime64 (const __time64_t *__timer); |
154 | libc_hidden_proto (__gmtime64) |
155 | |
156 | extern struct tm *__gmtime64_r (const __time64_t *__restrict __timer, |
157 | struct tm *__restrict __tp); |
158 | libc_hidden_proto (__gmtime64_r) |
159 | |
160 | extern __time64_t __timegm64 (struct tm *__tp) __THROW; |
161 | libc_hidden_proto (__timegm64) |
162 | #endif |
163 | |
164 | #if __TIMESIZE == 64 |
165 | # define __clock_settime64 __clock_settime |
166 | #else |
167 | extern int __clock_settime64 (clockid_t clock_id, |
168 | const struct __timespec64 *tp); |
169 | libc_hidden_proto (__clock_settime64) |
170 | #endif |
171 | |
172 | #if __TIMESIZE == 64 |
173 | # define __clock_getres64 __clock_getres |
174 | #else |
175 | extern int __clock_getres64 (clockid_t clock_id, |
176 | struct __timespec64 *tp); |
177 | libc_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 |
185 | extern int __utime64 (const char *file, const struct __utimbuf64 *times); |
186 | libc_hidden_proto (__utime64) |
187 | extern int __utimes64 (const char *file, const struct __timeval64 tvp[2]); |
188 | libc_hidden_proto (__utimes64) |
189 | extern int __utimensat64 (int fd, const char *file, |
190 | const struct __timespec64 tsp[2], int flags); |
191 | libc_hidden_proto (__utimensat64); |
192 | #endif |
193 | |
194 | extern int __utimensat64_helper (int fd, const char *file, |
195 | const struct __timespec64 tsp[2], int flags); |
196 | libc_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 |
204 | extern int __futimes64 (int fd, const struct __timeval64 tvp64[2]); |
205 | libc_hidden_proto (__futimes64); |
206 | extern int __futimesat64 (int fd, const char *file, |
207 | const struct __timeval64 tvp[2]); |
208 | libc_hidden_proto (__futimesat64); |
209 | extern int __lutimes64 (const char *file, const struct __timeval64 tvp64[2]); |
210 | libc_hidden_proto (__lutimes64); |
211 | extern int __futimens64 (int fd, const struct __timespec64 tsp[2]); |
212 | libc_hidden_proto (__futimens64); |
213 | #endif |
214 | |
215 | #if __TIMESIZE == 64 |
216 | # define __timer_gettime64 __timer_gettime |
217 | # define __timerfd_gettime64 __timerfd_gettime |
218 | #else |
219 | extern int __timer_gettime64 (timer_t timerid, struct __itimerspec64 *value); |
220 | extern int __timerfd_gettime64 (int fd, struct __itimerspec64 *value); |
221 | # if PTHREAD_IN_LIBC |
222 | libc_hidden_proto (__timer_gettime64) |
223 | # else |
224 | librt_hidden_proto (__timer_gettime64) |
225 | # endif |
226 | libc_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 |
233 | extern int __timer_settime64 (timer_t timerid, int flags, |
234 | const struct __itimerspec64 *value, |
235 | struct __itimerspec64 *ovalue); |
236 | extern int __timerfd_settime64 (int fd, int flags, |
237 | const struct __itimerspec64 *value, |
238 | struct __itimerspec64 *ovalue); |
239 | # if PTHREAD_IN_LIBC |
240 | libc_hidden_proto (__timer_settime64) |
241 | #else |
242 | librt_hidden_proto (__timer_settime64) |
243 | #endif |
244 | libc_hidden_proto (__timerfd_settime64); |
245 | #endif |
246 | |
247 | #if __TIMESIZE == 64 |
248 | # define __sched_rr_get_interval64 __sched_rr_get_interval |
249 | #else |
250 | extern int __sched_rr_get_interval64 (pid_t pid, struct __timespec64 *tp); |
251 | libc_hidden_proto (__sched_rr_get_interval64); |
252 | #endif |
253 | |
254 | #if __TIMESIZE == 64 |
255 | # define __settimeofday64 __settimeofday |
256 | # define __gettimeofday64 __gettimeofday |
257 | #else |
258 | extern int __settimeofday64 (const struct __timeval64 *tv, |
259 | const struct timezone *tz); |
260 | libc_hidden_proto (__settimeofday64) |
261 | extern int __gettimeofday64 (struct __timeval64 *restrict tv, |
262 | void *restrict tz); |
263 | libc_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. */ |
270 | extern int __offtime (__time64_t __timer, |
271 | long int __offset, |
272 | struct tm *__tp) attribute_hidden; |
273 | |
274 | extern char *__asctime_r (const struct tm *__tp, char *__buf) |
275 | attribute_hidden; |
276 | extern void __tzset (void) attribute_hidden; |
277 | |
278 | /* Prototype for the internal function to get information based on TZ. */ |
279 | extern struct tm *__tz_convert (__time64_t timer, int use_localtime, |
280 | struct tm *tp) attribute_hidden; |
281 | |
282 | extern int __nanosleep (const struct timespec *__requested_time, |
283 | struct timespec *__remaining); |
284 | hidden_proto (__nanosleep) |
285 | #if __TIMESIZE == 64 |
286 | # define __nanosleep64 __nanosleep |
287 | #else |
288 | extern int __nanosleep64 (const struct __timespec64 *__requested_time, |
289 | struct __timespec64 *__remaining); |
290 | hidden_proto (__nanosleep64) |
291 | #endif |
292 | |
293 | |
294 | extern int __getdate_r (const char *__string, struct tm *__resbufp); |
295 | libc_hidden_proto (__getdate_r); |
296 | |
297 | |
298 | /* Determine CLK_TCK value. */ |
299 | extern int __getclktck (void) attribute_hidden; |
300 | |
301 | |
302 | /* strptime support. */ |
303 | extern 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 |
310 | extern double __difftime64 (__time64_t time1, __time64_t time0); |
311 | libc_hidden_proto (__difftime64) |
312 | #endif |
313 | |
314 | extern 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 |
322 | extern int __clock_nanosleep_time64 (clockid_t clock_id, |
323 | int flags, const struct __timespec64 *req, |
324 | struct __timespec64 *rem); |
325 | libc_hidden_proto (__clock_nanosleep_time64) |
326 | extern int __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp); |
327 | libc_hidden_proto (__clock_gettime64) |
328 | extern int __timespec_get64 (struct __timespec64 *ts, int base); |
329 | libc_hidden_proto (__timespec_get64) |
330 | extern int __timespec_getres64 (struct __timespec64 *ts, int base); |
331 | libc_hidden_proto (__timespec_getres64) |
332 | #endif |
333 | |
334 | #if __TIMESIZE == 64 |
335 | # define __time64 __time |
336 | #else |
337 | extern __time64_t __time64 (__time64_t *timer); |
338 | libc_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. */ |
346 | static inline bool |
347 | in_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. */ |
354 | static inline struct __timespec64 |
355 | valid_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. */ |
366 | static inline struct __timeval64 |
367 | valid_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. */ |
379 | static inline struct timeval |
380 | valid_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. */ |
391 | static inline struct __timespec64 |
392 | timeval64_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. */ |
403 | static inline struct __timespec64 |
404 | valid_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. */ |
416 | static inline struct timespec |
417 | valid_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. */ |
429 | static inline struct timeval |
430 | valid_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. */ |
441 | static inline struct __timeval64 |
442 | timespec64_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. */ |
454 | struct __timeval32 |
455 | { |
456 | __int32_t tv_sec; /* Seconds. */ |
457 | __int32_t tv_usec; /* Microseconds. */ |
458 | }; |
459 | |
460 | /* Conversion functions for converting to/from __timeval32 */ |
461 | static inline struct __timeval64 |
462 | valid_timeval32_to_timeval64 (const struct __timeval32 tv) |
463 | { |
464 | return (struct __timeval64) { tv.tv_sec, tv.tv_usec }; |
465 | } |
466 | |
467 | static inline struct __timeval32 |
468 | valid_timeval64_to_timeval32 (const struct __timeval64 tv64) |
469 | { |
470 | return (struct __timeval32) { tv64.tv_sec, tv64.tv_usec }; |
471 | } |
472 | |
473 | static inline struct timeval |
474 | valid_timeval32_to_timeval (const struct __timeval32 tv) |
475 | { |
476 | return (struct timeval) { tv.tv_sec, tv.tv_usec }; |
477 | } |
478 | |
479 | static inline struct __timeval32 |
480 | valid_timeval_to_timeval32 (const struct timeval tv) |
481 | { |
482 | return (struct __timeval32) { tv.tv_sec, tv.tv_usec }; |
483 | } |
484 | |
485 | static inline struct timespec |
486 | valid_timeval32_to_timespec (const struct __timeval32 tv) |
487 | { |
488 | return (struct timespec) { tv.tv_sec, tv.tv_usec * 1000 }; |
489 | } |
490 | |
491 | static inline struct __timeval32 |
492 | valid_timespec_to_timeval32 (const struct timespec ts) |
493 | { |
494 | return (struct __timeval32) { (time_t) ts.tv_sec, ts.tv_nsec / 1000 }; |
495 | } |
496 | |
497 | static inline struct __timeval64 |
498 | valid_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. */ |
505 | static inline bool |
506 | valid_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. */ |
512 | static inline time_t |
513 | time_now (void) |
514 | { |
515 | struct timespec ts; |
516 | __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts); |
517 | return ts.tv_sec; |
518 | } |
519 | |
520 | static inline __time64_t |
521 | time64_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 | |