Add a generic scalb implementation
[glibc.git] / include / time.h
blob73f66277acfa52cb60c74b840aff7416efee02f5
1 #ifndef _TIME_H
2 #include <time/time.h>
4 #ifndef _ISOMAC
5 # include <bits/types/struct_timeval.h>
6 # include <bits/types/locale_t.h>
7 # include <stdbool.h>
8 # include <time/mktime-internal.h>
9 # include <endian.h>
10 # include <time-clockid.h>
11 # include <sys/time.h>
13 extern __typeof (strftime_l) __strftime_l;
14 libc_hidden_proto (__strftime_l)
15 extern __typeof (strptime_l) __strptime_l;
17 libc_hidden_proto (asctime)
18 libc_hidden_proto (mktime)
19 libc_hidden_proto (timelocal)
20 libc_hidden_proto (localtime)
21 libc_hidden_proto (strftime)
22 libc_hidden_proto (strptime)
24 extern __typeof (clock_gettime) __clock_gettime;
25 libc_hidden_proto (__clock_gettime)
26 extern __typeof (clock_settime) __clock_settime;
27 libc_hidden_proto (__clock_settime)
29 extern __typeof (clock_nanosleep) __clock_nanosleep;
30 libc_hidden_proto (__clock_nanosleep);
32 #ifdef __linux__
33 extern __typeof (clock_adjtime) __clock_adjtime;
34 libc_hidden_proto (__clock_adjtime);
35 #endif
37 /* Now define the internal interfaces. */
38 struct tm;
40 /* Defined in mktime.c. */
41 extern const unsigned short int __mon_yday[2][13] attribute_hidden;
43 /* Defined in localtime.c. */
44 extern struct tm _tmbuf attribute_hidden;
46 /* Defined in tzset.c. */
47 extern char *__tzstring (const char *string) attribute_hidden;
49 extern int __use_tzfile attribute_hidden;
51 extern void __tzfile_read (const char *file, size_t extra,
52 char **extrap) attribute_hidden;
53 extern void __tzfile_compute (__time64_t timer, int use_localtime,
54 long int *leap_correct, int *leap_hit,
55 struct tm *tp) attribute_hidden;
56 extern void __tzfile_default (const char *std, const char *dst,
57 int stdoff, int dstoff)
58 attribute_hidden;
59 extern void __tzset_parse_tz (const char *tz) attribute_hidden;
60 extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
61 __THROW attribute_hidden;
63 #if __TIMESIZE == 64
64 # define __timespec64 timespec
65 #else
66 /* The glibc Y2038-proof struct __timespec64 structure for a time value.
67 To keep things Posix-ish, we keep the nanoseconds field a 32-bit
68 signed long, but since the Linux field is a 64-bit signed int, we
69 pad our tv_nsec with a 32-bit unnamed bit-field padding.
71 As a general rule the Linux kernel is ignoring upper 32 bits of
72 tv_nsec field. */
73 struct __timespec64
75 __time64_t tv_sec; /* Seconds */
76 # if BYTE_ORDER == BIG_ENDIAN
77 __int32_t :32; /* Padding */
78 __int32_t tv_nsec; /* Nanoseconds */
79 # else
80 __int32_t tv_nsec; /* Nanoseconds */
81 __int32_t :32; /* Padding */
82 # endif
84 #endif
86 #if __TIMESIZE == 64
87 # define __itimerspec64 itimerspec
88 #else
89 /* The glibc's internal representation of the struct itimerspec. */
90 struct __itimerspec64
92 struct __timespec64 it_interval;
93 struct __timespec64 it_value;
95 #endif
97 #if __TIMESIZE == 64
98 # define __timeval64 timeval
99 #else
100 /* The glibc Y2038-proof struct __timeval64 structure for a time value.
101 This structure is NOT supposed to be passed to the Linux kernel.
102 Instead, it shall be converted to struct __timespec64 and time shall
103 be [sg]et via clock_[sg]ettime (which are now Y2038 safe). */
104 struct __timeval64
106 __time64_t tv_sec; /* Seconds */
107 __suseconds64_t tv_usec; /* Microseconds */
109 #endif
111 #if __TIMESIZE == 64
112 # define __ctime64 ctime
113 #else
114 extern char *__ctime64 (const __time64_t *__timer) __THROW;
115 libc_hidden_proto (__ctime64)
116 #endif
118 #if __TIMESIZE == 64
119 # define __ctime64_r ctime_r
120 #else
121 extern char *__ctime64_r (const __time64_t *__restrict __timer,
122 char *__restrict __buf) __THROW;
123 libc_hidden_proto (__ctime64_r)
124 #endif
126 #if __TIMESIZE == 64
127 # define __localtime64 localtime
128 #else
129 extern struct tm *__localtime64 (const __time64_t *__timer);
130 libc_hidden_proto (__localtime64)
131 #endif
133 extern struct tm *__localtime_r (const time_t *__timer,
134 struct tm *__tp) attribute_hidden;
135 #if __TIMESIZE != 64
136 extern struct tm *__localtime64_r (const __time64_t *__timer,
137 struct tm *__tp);
138 libc_hidden_proto (__localtime64_r)
140 extern __time64_t __mktime64 (struct tm *__tp) __THROW;
141 libc_hidden_proto (__mktime64)
142 #endif
144 extern struct tm *__gmtime_r (const time_t *__restrict __timer,
145 struct tm *__restrict __tp);
146 libc_hidden_proto (__gmtime_r)
148 #if __TIMESIZE == 64
149 # define __gmtime64 gmtime
150 #else
151 extern struct tm *__gmtime64 (const __time64_t *__timer);
152 libc_hidden_proto (__gmtime64)
154 extern struct tm *__gmtime64_r (const __time64_t *__restrict __timer,
155 struct tm *__restrict __tp);
156 libc_hidden_proto (__gmtime64_r)
158 extern __time64_t __timegm64 (struct tm *__tp) __THROW;
159 libc_hidden_proto (__timegm64)
160 #endif
162 #if __TIMESIZE == 64
163 # define __clock_settime64 __clock_settime
164 #else
165 extern int __clock_settime64 (clockid_t clock_id,
166 const struct __timespec64 *tp);
167 libc_hidden_proto (__clock_settime64)
168 #endif
170 #if __TIMESIZE == 64
171 # define __clock_getres64 __clock_getres
172 #else
173 extern int __clock_getres64 (clockid_t clock_id,
174 struct __timespec64 *tp);
175 libc_hidden_proto (__clock_getres64);
176 #endif
178 #if __TIMESIZE == 64
179 # define __utimensat64 __utimensat
180 #else
181 extern int __utimensat64 (int fd, const char *file,
182 const struct __timespec64 tsp[2], int flags);
183 libc_hidden_proto (__utimensat64);
184 #endif
186 extern int __utimensat64_helper (int fd, const char *file,
187 const struct __timespec64 tsp[2], int flags);
188 libc_hidden_proto (__utimensat64_helper);
190 #if __TIMESIZE == 64
191 # define __futimens64 __futimens
192 #else
193 extern int __futimens64 (int fd, const struct __timespec64 tsp[2]);
194 libc_hidden_proto (__futimens64);
195 #endif
197 #if __TIMESIZE == 64
198 # define __timer_gettime64 __timer_gettime
199 # define __timerfd_gettime64 __timerfd_gettime
200 #else
201 extern int __timer_gettime64 (timer_t timerid, struct __itimerspec64 *value);
202 extern int __timerfd_gettime64 (int fd, struct __itimerspec64 *value);
203 librt_hidden_proto (__timer_gettime64);
204 libc_hidden_proto (__timerfd_gettime64);
205 #endif
207 #if __TIMESIZE == 64
208 # define __timer_settime64 __timer_settime
209 # define __timerfd_settime64 __timerfd_settime
210 #else
211 extern int __timer_settime64 (timer_t timerid, int flags,
212 const struct __itimerspec64 *value,
213 struct __itimerspec64 *ovalue);
214 extern int __timerfd_settime64 (int fd, int flags,
215 const struct __itimerspec64 *value,
216 struct __itimerspec64 *ovalue);
217 librt_hidden_proto (__timer_settime64);
218 libc_hidden_proto (__timerfd_settime64);
219 #endif
221 #if __TIMESIZE == 64
222 # define __sched_rr_get_interval64 __sched_rr_get_interval
223 #else
224 extern int __sched_rr_get_interval64 (pid_t pid, struct __timespec64 *tp);
225 libc_hidden_proto (__sched_rr_get_interval64);
226 #endif
228 #if __TIMESIZE == 64
229 # define __settimeofday64 __settimeofday
230 #else
231 extern int __settimeofday64 (const struct __timeval64 *tv,
232 const struct timezone *tz);
233 libc_hidden_proto (__settimeofday64)
234 #endif
236 /* Compute the `struct tm' representation of T,
237 offset OFFSET seconds east of UTC,
238 and store year, yday, mon, mday, wday, hour, min, sec into *TP.
239 Return nonzero if successful. */
240 extern int __offtime (__time64_t __timer,
241 long int __offset,
242 struct tm *__tp) attribute_hidden;
244 extern char *__asctime_r (const struct tm *__tp, char *__buf)
245 attribute_hidden;
246 extern void __tzset (void) attribute_hidden;
248 /* Prototype for the internal function to get information based on TZ. */
249 extern struct tm *__tz_convert (__time64_t timer, int use_localtime,
250 struct tm *tp) attribute_hidden;
252 extern int __nanosleep (const struct timespec *__requested_time,
253 struct timespec *__remaining);
254 hidden_proto (__nanosleep)
255 extern int __getdate_r (const char *__string, struct tm *__resbufp)
256 attribute_hidden;
259 /* Determine CLK_TCK value. */
260 extern int __getclktck (void) attribute_hidden;
263 /* strptime support. */
264 extern char * __strptime_internal (const char *rp, const char *fmt,
265 struct tm *tm, void *statep,
266 locale_t locparam) attribute_hidden;
268 #if __TIMESIZE == 64
269 # define __difftime64 __difftime
270 #else
271 extern double __difftime64 (__time64_t time1, __time64_t time0);
272 libc_hidden_proto (__difftime64)
273 #endif
275 extern double __difftime (time_t time1, time_t time0);
277 #if __TIMESIZE == 64
278 # define __clock_nanosleep_time64 __clock_nanosleep
279 # define __clock_gettime64 __clock_gettime
280 # define __timespec_get64 __timespec_get
281 #else
282 extern int __clock_nanosleep_time64 (clockid_t clock_id,
283 int flags, const struct __timespec64 *req,
284 struct __timespec64 *rem);
285 libc_hidden_proto (__clock_nanosleep_time64)
286 extern int __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp);
287 libc_hidden_proto (__clock_gettime64)
288 extern int __timespec_get64 (struct __timespec64 *ts, int base);
289 libc_hidden_proto (__timespec_get64)
290 #endif
292 /* Use in the clock_* functions. Size of the field representing the
293 actual clock ID. */
294 #define CLOCK_IDFIELD_SIZE 3
296 /* Check whether T fits in time_t. */
297 static inline bool
298 in_time_t_range (__time64_t t)
300 time_t s = t;
301 return s == t;
304 /* Convert a known valid struct timeval into a struct __timespec64. */
305 static inline struct __timespec64
306 valid_timeval_to_timespec64 (const struct timeval tv)
308 struct __timespec64 ts64;
310 ts64.tv_sec = tv.tv_sec;
311 ts64.tv_nsec = tv.tv_usec * 1000;
313 return ts64;
316 /* Convert a known valid struct timeval into a struct __timeval64. */
317 static inline struct __timeval64
318 valid_timeval_to_timeval64 (const struct timeval tv)
320 struct __timeval64 tv64;
322 tv64.tv_sec = tv.tv_sec;
323 tv64.tv_usec = tv.tv_usec;
325 return tv64;
328 /* Convert a valid and within range of struct timeval, struct
329 __timeval64 into a struct timeval. */
330 static inline struct timeval
331 valid_timeval64_to_timeval (const struct __timeval64 tv64)
333 struct timeval tv;
335 tv.tv_sec = (time_t) tv64.tv_sec;
336 tv.tv_usec = (suseconds_t) tv64.tv_usec;
338 return tv;
341 /* Convert a struct __timeval64 into a struct __timespec64. */
342 static inline struct __timespec64
343 timeval64_to_timespec64 (const struct __timeval64 tv64)
345 struct __timespec64 ts64;
347 ts64.tv_sec = tv64.tv_sec;
348 ts64.tv_nsec = tv64.tv_usec * 1000;
350 return ts64;
353 /* Convert a known valid struct timespec into a struct __timespec64. */
354 static inline struct __timespec64
355 valid_timespec_to_timespec64 (const struct timespec ts)
357 struct __timespec64 ts64;
359 ts64.tv_sec = ts.tv_sec;
360 ts64.tv_nsec = ts.tv_nsec;
362 return ts64;
365 /* Convert a valid and within range of struct timespec, struct
366 __timespec64 into a struct timespec. */
367 static inline struct timespec
368 valid_timespec64_to_timespec (const struct __timespec64 ts64)
370 struct timespec ts;
372 ts.tv_sec = (time_t) ts64.tv_sec;
373 ts.tv_nsec = ts64.tv_nsec;
375 return ts;
378 /* Convert a valid and within range of struct timeval struct
379 __timespec64 into a struct timeval. */
380 static inline struct timeval
381 valid_timespec64_to_timeval (const struct __timespec64 ts64)
383 struct timeval tv;
385 tv.tv_sec = (time_t) ts64.tv_sec;
386 tv.tv_usec = ts64.tv_nsec / 1000;
388 return tv;
391 /* Convert a struct __timespec64 into a struct __timeval64. */
392 static inline struct __timeval64
393 timespec64_to_timeval64 (const struct __timespec64 ts64)
395 struct __timeval64 tv64;
397 tv64.tv_sec = ts64.tv_sec;
398 tv64.tv_usec = ts64.tv_nsec / 1000;
400 return tv64;
403 /* Check if a value is in the valid nanoseconds range. Return true if
404 it is, false otherwise. */
405 static inline bool
406 valid_nanoseconds (__syscall_slong_t ns)
408 return __glibc_likely (0 <= ns && ns < 1000000000);
411 /* Helper function to get time in seconds, similar to time. */
412 static inline time_t
413 time_now (void)
415 struct timespec ts;
416 __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
417 return ts.tv_sec;
419 #endif
421 #endif