1 | #ifndef _TIME_H |
2 | #include <time/time.h> |
3 | |
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 | |
12 | extern __typeof (strftime_l) __strftime_l; |
13 | libc_hidden_proto (__strftime_l) |
14 | extern __typeof (strptime_l) __strptime_l; |
15 | |
16 | libc_hidden_proto (asctime) |
17 | libc_hidden_proto (mktime) |
18 | libc_hidden_proto (timelocal) |
19 | libc_hidden_proto (localtime) |
20 | libc_hidden_proto (strftime) |
21 | libc_hidden_proto (strptime) |
22 | |
23 | extern __typeof (clock_gettime) __clock_gettime; |
24 | libc_hidden_proto (__clock_gettime) |
25 | extern __typeof (clock_settime) __clock_settime; |
26 | libc_hidden_proto (__clock_settime) |
27 | |
28 | extern __typeof (clock_nanosleep) __clock_nanosleep; |
29 | libc_hidden_proto (__clock_nanosleep); |
30 | |
31 | #ifdef __linux__ |
32 | extern __typeof (clock_adjtime) __clock_adjtime; |
33 | libc_hidden_proto (__clock_adjtime); |
34 | #endif |
35 | |
36 | /* Now define the internal interfaces. */ |
37 | struct tm; |
38 | |
39 | /* Defined in mktime.c. */ |
40 | extern const unsigned short int __mon_yday[2][13] attribute_hidden; |
41 | |
42 | /* Defined in localtime.c. */ |
43 | extern struct tm _tmbuf attribute_hidden; |
44 | |
45 | /* Defined in tzset.c. */ |
46 | extern char *__tzstring (const char *string) attribute_hidden; |
47 | |
48 | extern int __use_tzfile attribute_hidden; |
49 | |
50 | extern void __tzfile_read (const char *file, size_t , |
51 | char **) attribute_hidden; |
52 | extern void __tzfile_compute (__time64_t timer, int use_localtime, |
53 | long int *leap_correct, int *leap_hit, |
54 | struct tm *tp) attribute_hidden; |
55 | extern void __tzfile_default (const char *std, const char *dst, |
56 | int stdoff, int dstoff) |
57 | attribute_hidden; |
58 | extern void __tzset_parse_tz (const char *tz) attribute_hidden; |
59 | extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) |
60 | __THROW attribute_hidden; |
61 | |
62 | #if __TIMESIZE == 64 |
63 | # define __timespec64 timespec |
64 | #else |
65 | /* The glibc Y2038-proof struct __timespec64 structure for a time value. |
66 | To keep things Posix-ish, we keep the nanoseconds field a 32-bit |
67 | signed long, but since the Linux field is a 64-bit signed int, we |
68 | pad our tv_nsec with a 32-bit unnamed bit-field padding. |
69 | |
70 | As a general rule the Linux kernel is ignoring upper 32 bits of |
71 | tv_nsec field. */ |
72 | struct __timespec64 |
73 | { |
74 | __time64_t tv_sec; /* Seconds */ |
75 | # if BYTE_ORDER == BIG_ENDIAN |
76 | __int32_t :32; /* Padding */ |
77 | __int32_t tv_nsec; /* Nanoseconds */ |
78 | # else |
79 | __int32_t tv_nsec; /* Nanoseconds */ |
80 | __int32_t :32; /* Padding */ |
81 | # endif |
82 | }; |
83 | #endif |
84 | |
85 | #if __TIMESIZE == 64 |
86 | # define __itimerspec64 itimerspec |
87 | #else |
88 | /* The glibc's internal representation of the struct itimerspec. */ |
89 | struct __itimerspec64 |
90 | { |
91 | struct __timespec64 it_interval; |
92 | struct __timespec64 it_value; |
93 | }; |
94 | #endif |
95 | |
96 | #if __TIMESIZE == 64 |
97 | # define __ctime64 ctime |
98 | #else |
99 | extern char *__ctime64 (const __time64_t *__timer) __THROW; |
100 | libc_hidden_proto (__ctime64) |
101 | #endif |
102 | |
103 | #if __TIMESIZE == 64 |
104 | # define __ctime64_r ctime_r |
105 | #else |
106 | extern char *__ctime64_r (const __time64_t *__restrict __timer, |
107 | char *__restrict __buf) __THROW; |
108 | libc_hidden_proto (__ctime64_r) |
109 | #endif |
110 | |
111 | #if __TIMESIZE == 64 |
112 | # define __localtime64 localtime |
113 | #else |
114 | extern struct tm *__localtime64 (const __time64_t *__timer); |
115 | libc_hidden_proto (__localtime64) |
116 | #endif |
117 | |
118 | extern struct tm *__localtime_r (const time_t *__timer, |
119 | struct tm *__tp) attribute_hidden; |
120 | #if __TIMESIZE != 64 |
121 | extern struct tm *__localtime64_r (const __time64_t *__timer, |
122 | struct tm *__tp); |
123 | libc_hidden_proto (__localtime64_r) |
124 | |
125 | extern __time64_t __mktime64 (struct tm *__tp) __THROW; |
126 | libc_hidden_proto (__mktime64) |
127 | #endif |
128 | |
129 | extern struct tm *__gmtime_r (const time_t *__restrict __timer, |
130 | struct tm *__restrict __tp); |
131 | libc_hidden_proto (__gmtime_r) |
132 | |
133 | #if __TIMESIZE == 64 |
134 | # define __gmtime64 gmtime |
135 | #else |
136 | extern struct tm *__gmtime64 (const __time64_t *__timer); |
137 | libc_hidden_proto (__gmtime64) |
138 | |
139 | extern struct tm *__gmtime64_r (const __time64_t *__restrict __timer, |
140 | struct tm *__restrict __tp); |
141 | libc_hidden_proto (__gmtime64_r) |
142 | |
143 | extern __time64_t __timegm64 (struct tm *__tp) __THROW; |
144 | libc_hidden_proto (__timegm64) |
145 | #endif |
146 | |
147 | #if __TIMESIZE == 64 |
148 | # define __clock_settime64 __clock_settime |
149 | #else |
150 | extern int __clock_settime64 (clockid_t clock_id, |
151 | const struct __timespec64 *tp); |
152 | libc_hidden_proto (__clock_settime64) |
153 | #endif |
154 | |
155 | #if __TIMESIZE == 64 |
156 | # define __clock_getres64 __clock_getres |
157 | #else |
158 | extern int __clock_getres64 (clockid_t clock_id, |
159 | struct __timespec64 *tp); |
160 | libc_hidden_proto (__clock_getres64); |
161 | #endif |
162 | |
163 | #if __TIMESIZE == 64 |
164 | # define __utimensat64 __utimensat |
165 | #else |
166 | extern int __utimensat64 (int fd, const char *file, |
167 | const struct __timespec64 tsp[2], int flags); |
168 | libc_hidden_proto (__utimensat64); |
169 | #endif |
170 | |
171 | extern int __utimensat64_helper (int fd, const char *file, |
172 | const struct __timespec64 tsp[2], int flags); |
173 | libc_hidden_proto (__utimensat64_helper); |
174 | |
175 | #if __TIMESIZE == 64 |
176 | # define __futimens64 __futimens |
177 | #else |
178 | extern int __futimens64 (int fd, const struct __timespec64 tsp[2]); |
179 | libc_hidden_proto (__futimens64); |
180 | #endif |
181 | |
182 | #if __TIMESIZE == 64 |
183 | # define __timer_gettime64 __timer_gettime |
184 | #else |
185 | extern int __timer_gettime64 (timer_t timerid, struct __itimerspec64 *value); |
186 | libc_hidden_proto (__timer_gettime64); |
187 | #endif |
188 | |
189 | #if __TIMESIZE == 64 |
190 | # define __timer_settime64 __timer_settime |
191 | #else |
192 | extern int __timer_settime64 (timer_t timerid, int flags, |
193 | const struct __itimerspec64 *value, |
194 | struct __itimerspec64 *ovalue); |
195 | libc_hidden_proto (__timer_settime64); |
196 | #endif |
197 | |
198 | /* Compute the `struct tm' representation of T, |
199 | offset OFFSET seconds east of UTC, |
200 | and store year, yday, mon, mday, wday, hour, min, sec into *TP. |
201 | Return nonzero if successful. */ |
202 | extern int __offtime (__time64_t __timer, |
203 | long int __offset, |
204 | struct tm *__tp) attribute_hidden; |
205 | |
206 | extern char *__asctime_r (const struct tm *__tp, char *__buf) |
207 | attribute_hidden; |
208 | extern void __tzset (void) attribute_hidden; |
209 | |
210 | /* Prototype for the internal function to get information based on TZ. */ |
211 | extern struct tm *__tz_convert (__time64_t timer, int use_localtime, |
212 | struct tm *tp) attribute_hidden; |
213 | |
214 | extern int __nanosleep (const struct timespec *__requested_time, |
215 | struct timespec *__remaining); |
216 | hidden_proto (__nanosleep) |
217 | extern int __getdate_r (const char *__string, struct tm *__resbufp) |
218 | attribute_hidden; |
219 | |
220 | |
221 | /* Determine CLK_TCK value. */ |
222 | extern int __getclktck (void) attribute_hidden; |
223 | |
224 | |
225 | /* strptime support. */ |
226 | extern char * __strptime_internal (const char *rp, const char *fmt, |
227 | struct tm *tm, void *statep, |
228 | locale_t locparam) attribute_hidden; |
229 | |
230 | #if __TIMESIZE == 64 |
231 | # define __difftime64 __difftime |
232 | #else |
233 | extern double __difftime64 (__time64_t time1, __time64_t time0); |
234 | libc_hidden_proto (__difftime64) |
235 | #endif |
236 | |
237 | extern double __difftime (time_t time1, time_t time0); |
238 | |
239 | #if __TIMESIZE == 64 |
240 | # define __clock_nanosleep_time64 __clock_nanosleep |
241 | # define __clock_gettime64 __clock_gettime |
242 | #else |
243 | extern int __clock_nanosleep_time64 (clockid_t clock_id, |
244 | int flags, const struct __timespec64 *req, |
245 | struct __timespec64 *rem); |
246 | libc_hidden_proto (__clock_nanosleep_time64) |
247 | extern int __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp); |
248 | libc_hidden_proto (__clock_gettime64) |
249 | #endif |
250 | |
251 | /* Use in the clock_* functions. Size of the field representing the |
252 | actual clock ID. */ |
253 | #define CLOCK_IDFIELD_SIZE 3 |
254 | |
255 | /* Check whether T fits in time_t. */ |
256 | static inline bool |
257 | in_time_t_range (__time64_t t) |
258 | { |
259 | time_t s = t; |
260 | return s == t; |
261 | } |
262 | |
263 | /* Convert a known valid struct timeval into a struct __timespec64. */ |
264 | static inline struct __timespec64 |
265 | valid_timeval_to_timespec64 (const struct timeval tv) |
266 | { |
267 | struct __timespec64 ts64; |
268 | |
269 | ts64.tv_sec = tv.tv_sec; |
270 | ts64.tv_nsec = tv.tv_usec * 1000; |
271 | |
272 | return ts64; |
273 | } |
274 | |
275 | /* Convert a known valid struct timespec into a struct __timespec64. */ |
276 | static inline struct __timespec64 |
277 | valid_timespec_to_timespec64 (const struct timespec ts) |
278 | { |
279 | struct __timespec64 ts64; |
280 | |
281 | ts64.tv_sec = ts.tv_sec; |
282 | ts64.tv_nsec = ts.tv_nsec; |
283 | |
284 | return ts64; |
285 | } |
286 | |
287 | /* Convert a valid and within range of struct timespec, struct |
288 | __timespec64 into a struct timespec. */ |
289 | static inline struct timespec |
290 | valid_timespec64_to_timespec (const struct __timespec64 ts64) |
291 | { |
292 | struct timespec ts; |
293 | |
294 | ts.tv_sec = (time_t) ts64.tv_sec; |
295 | ts.tv_nsec = ts64.tv_nsec; |
296 | |
297 | return ts; |
298 | } |
299 | |
300 | /* Convert a valid and within range of struct timeval struct |
301 | __timespec64 into a struct timeval. */ |
302 | static inline struct timeval |
303 | valid_timespec64_to_timeval (const struct __timespec64 ts64) |
304 | { |
305 | struct timeval tv; |
306 | |
307 | tv.tv_sec = (time_t) ts64.tv_sec; |
308 | tv.tv_usec = ts64.tv_nsec / 1000; |
309 | |
310 | return tv; |
311 | } |
312 | |
313 | /* Check if a value is in the valid nanoseconds range. Return true if |
314 | it is, false otherwise. */ |
315 | static inline bool |
316 | valid_nanoseconds (__syscall_slong_t ns) |
317 | { |
318 | return __glibc_likely (0 <= ns && ns < 1000000000); |
319 | } |
320 | |
321 | /* Helper function to get time in seconds, similar to time. */ |
322 | static inline time_t |
323 | time_now (void) |
324 | { |
325 | struct timespec ts; |
326 | __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts); |
327 | return ts.tv_sec; |
328 | } |
329 | #endif |
330 | |
331 | #endif |
332 | |