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