time.h 15 KB

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