time.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* time.h -- An implementation of the standard Unix <sys/time.h> file.
  2. Written by Geoffrey Noer <noer@cygnus.com>
  3. Public domain; no rights reserved. */
  4. /*-
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. *
  7. * Copyright (c) 1982, 1986, 1993
  8. * The Regents of the University of California. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)time.h 8.5 (Berkeley) 5/4/95
  35. * $FreeBSD: head/sys/sys/time.h 346176 2019-04-13 04:46:35Z imp $
  36. */
  37. #ifndef _SYS_TIME_H_
  38. #define _SYS_TIME_H_
  39. #include <_ansi.h>
  40. #include <sys/cdefs.h>
  41. #include <sys/_timeval.h>
  42. #include <sys/types.h>
  43. #include <sys/timespec.h>
  44. #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
  45. #include <sys/select.h>
  46. #endif
  47. struct timezone {
  48. int tz_minuteswest; /* minutes west of Greenwich */
  49. int tz_dsttime; /* type of dst correction */
  50. };
  51. #define DST_NONE 0 /* not on dst */
  52. #define DST_USA 1 /* USA style dst */
  53. #define DST_AUST 2 /* Australian style dst */
  54. #define DST_WET 3 /* Western European dst */
  55. #define DST_MET 4 /* Middle European dst */
  56. #define DST_EET 5 /* Eastern European dst */
  57. #define DST_CAN 6 /* Canada */
  58. #if __BSD_VISIBLE
  59. struct bintime {
  60. time_t sec;
  61. uint64_t frac;
  62. };
  63. static __inline void
  64. bintime_addx(struct bintime *_bt, uint64_t _x)
  65. {
  66. uint64_t _u;
  67. _u = _bt->frac;
  68. _bt->frac += _x;
  69. if (_u > _bt->frac)
  70. _bt->sec++;
  71. }
  72. static __inline void
  73. bintime_add(struct bintime *_bt, const struct bintime *_bt2)
  74. {
  75. uint64_t _u;
  76. _u = _bt->frac;
  77. _bt->frac += _bt2->frac;
  78. if (_u > _bt->frac)
  79. _bt->sec++;
  80. _bt->sec += _bt2->sec;
  81. }
  82. static __inline void
  83. bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
  84. {
  85. uint64_t _u;
  86. _u = _bt->frac;
  87. _bt->frac -= _bt2->frac;
  88. if (_u < _bt->frac)
  89. _bt->sec--;
  90. _bt->sec -= _bt2->sec;
  91. }
  92. static __inline void
  93. bintime_mul(struct bintime *_bt, u_int _x)
  94. {
  95. uint64_t _p1, _p2;
  96. _p1 = (_bt->frac & 0xffffffffull) * _x;
  97. _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
  98. _bt->sec *= _x;
  99. _bt->sec += (_p2 >> 32);
  100. _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
  101. }
  102. static __inline void
  103. bintime_shift(struct bintime *_bt, int _exp)
  104. {
  105. if (_exp > 0) {
  106. _bt->sec <<= _exp;
  107. _bt->sec |= _bt->frac >> (64 - _exp);
  108. _bt->frac <<= _exp;
  109. } else if (_exp < 0) {
  110. _bt->frac >>= -_exp;
  111. _bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
  112. _bt->sec >>= -_exp;
  113. }
  114. }
  115. #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
  116. #define bintime_isset(a) ((a)->sec || (a)->frac)
  117. #define bintime_cmp(a, b, cmp) \
  118. (((a)->sec == (b)->sec) ? \
  119. ((a)->frac cmp (b)->frac) : \
  120. ((a)->sec cmp (b)->sec))
  121. #define SBT_1S ((sbintime_t)1 << 32)
  122. #define SBT_1M (SBT_1S * 60)
  123. #define SBT_1MS (SBT_1S / 1000)
  124. #define SBT_1US (SBT_1S / 1000000)
  125. #define SBT_1NS (SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
  126. #define SBT_MAX 0x7fffffffffffffffLL
  127. static __inline int
  128. sbintime_getsec(sbintime_t _sbt)
  129. {
  130. return (_sbt >> 32);
  131. }
  132. static __inline sbintime_t
  133. bttosbt(const struct bintime _bt)
  134. {
  135. return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
  136. }
  137. static __inline struct bintime
  138. sbttobt(sbintime_t _sbt)
  139. {
  140. struct bintime _bt;
  141. _bt.sec = _sbt >> 32;
  142. _bt.frac = _sbt << 32;
  143. return (_bt);
  144. }
  145. /*
  146. * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS results in
  147. * large roundoff errors which sbttons() and nstosbt() avoid. Millisecond and
  148. * microsecond functions are also provided for completeness.
  149. *
  150. * These functions return the smallest sbt larger or equal to the
  151. * number of seconds requested so that sbttoX(Xtosbt(y)) == y. Unlike
  152. * top of second computations below, which require that we tick at the
  153. * top of second, these need to be rounded up so we do whatever for at
  154. * least as long as requested.
  155. *
  156. * The naive computation we'd do is this
  157. * ((unit * 2^64 / SIFACTOR) + 2^32-1) >> 32
  158. * However, that overflows. Instead, we compute
  159. * ((unit * 2^63 / SIFACTOR) + 2^31-1) >> 32
  160. * and use pre-computed constants that are the ceil of the 2^63 / SIFACTOR
  161. * term to ensure we are using exactly the right constant. We use the lesser
  162. * evil of ull rather than a uint64_t cast to ensure we have well defined
  163. * right shift semantics. With these changes, we get all the ns, us and ms
  164. * conversions back and forth right.
  165. */
  166. static __inline int64_t
  167. sbttons(sbintime_t _sbt)
  168. {
  169. uint64_t ns;
  170. ns = _sbt;
  171. if (ns >= SBT_1S)
  172. ns = (ns >> 32) * 1000000000;
  173. else
  174. ns = 0;
  175. return (ns + (1000000000 * (_sbt & 0xffffffffu) >> 32));
  176. }
  177. static __inline sbintime_t
  178. nstosbt(int64_t _ns)
  179. {
  180. sbintime_t sb = 0;
  181. if (_ns >= SBT_1S) {
  182. sb = (_ns / 1000000000) * SBT_1S;
  183. _ns = _ns % 1000000000;
  184. }
  185. /* 9223372037 = ceil(2^63 / 1000000000) */
  186. sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
  187. return (sb);
  188. }
  189. static __inline int64_t
  190. sbttous(sbintime_t _sbt)
  191. {
  192. return ((1000000 * _sbt) >> 32);
  193. }
  194. static __inline sbintime_t
  195. ustosbt(int64_t _us)
  196. {
  197. sbintime_t sb = 0;
  198. if (_us >= SBT_1S) {
  199. sb = (_us / 1000000) * SBT_1S;
  200. _us = _us % 1000000;
  201. }
  202. /* 9223372036855 = ceil(2^63 / 1000000) */
  203. sb += ((_us * 9223372036855ull) + 0x7fffffff) >> 31;
  204. return (sb);
  205. }
  206. static __inline int64_t
  207. sbttoms(sbintime_t _sbt)
  208. {
  209. return ((1000 * _sbt) >> 32);
  210. }
  211. static __inline sbintime_t
  212. mstosbt(int64_t _ms)
  213. {
  214. sbintime_t sb = 0;
  215. if (_ms >= SBT_1S) {
  216. sb = (_ms / 1000) * SBT_1S;
  217. _ms = _ms % 1000;
  218. }
  219. /* 9223372036854776 = ceil(2^63 / 1000) */
  220. sb += ((_ms * 9223372036854776ull) + 0x7fffffff) >> 31;
  221. return (sb);
  222. }
  223. /*-
  224. * Background information:
  225. *
  226. * When converting between timestamps on parallel timescales of differing
  227. * resolutions it is historical and scientific practice to round down rather
  228. * than doing 4/5 rounding.
  229. *
  230. * The date changes at midnight, not at noon.
  231. *
  232. * Even at 15:59:59.999999999 it's not four'o'clock.
  233. *
  234. * time_second ticks after N.999999999 not after N.4999999999
  235. */
  236. static __inline void
  237. bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
  238. {
  239. _ts->tv_sec = _bt->sec;
  240. _ts->tv_nsec = ((uint64_t)1000000000 *
  241. (uint32_t)(_bt->frac >> 32)) >> 32;
  242. }
  243. static __inline void
  244. timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
  245. {
  246. _bt->sec = _ts->tv_sec;
  247. /* 18446744073 = int(2^64 / 1000000000) */
  248. _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
  249. }
  250. static __inline void
  251. bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
  252. {
  253. _tv->tv_sec = _bt->sec;
  254. _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
  255. }
  256. static __inline void
  257. timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
  258. {
  259. _bt->sec = _tv->tv_sec;
  260. /* 18446744073709 = int(2^64 / 1000000) */
  261. _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
  262. }
  263. static __inline struct timespec
  264. sbttots(sbintime_t _sbt)
  265. {
  266. struct timespec _ts;
  267. _ts.tv_sec = _sbt >> 32;
  268. _ts.tv_nsec = sbttons((uint32_t)_sbt);
  269. return (_ts);
  270. }
  271. static __inline sbintime_t
  272. tstosbt(struct timespec _ts)
  273. {
  274. return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
  275. }
  276. static __inline struct timeval
  277. sbttotv(sbintime_t _sbt)
  278. {
  279. struct timeval _tv;
  280. _tv.tv_sec = _sbt >> 32;
  281. _tv.tv_usec = sbttous((uint32_t)_sbt);
  282. return (_tv);
  283. }
  284. static __inline sbintime_t
  285. tvtosbt(struct timeval _tv)
  286. {
  287. return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
  288. }
  289. /* Operations on timespecs */
  290. #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
  291. #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
  292. #define timespeccmp(tvp, uvp, cmp) \
  293. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  294. ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
  295. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  296. #define timespecadd(tsp, usp, vsp) \
  297. do { \
  298. (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
  299. (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
  300. if ((vsp)->tv_nsec >= 1000000000L) { \
  301. (vsp)->tv_sec++; \
  302. (vsp)->tv_nsec -= 1000000000L; \
  303. } \
  304. } while (0)
  305. #define timespecsub(tsp, usp, vsp) \
  306. do { \
  307. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  308. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  309. if ((vsp)->tv_nsec < 0) { \
  310. (vsp)->tv_sec--; \
  311. (vsp)->tv_nsec += 1000000000L; \
  312. } \
  313. } while (0)
  314. #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
  315. #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  316. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  317. #define timercmp(tvp, uvp, cmp) \
  318. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  319. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  320. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  321. #define timeradd(tvp, uvp, vvp) \
  322. do { \
  323. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  324. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  325. if ((vvp)->tv_usec >= 1000000) { \
  326. (vvp)->tv_sec++; \
  327. (vvp)->tv_usec -= 1000000; \
  328. } \
  329. } while (0)
  330. #define timersub(tvp, uvp, vvp) \
  331. do { \
  332. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  333. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  334. if ((vvp)->tv_usec < 0) { \
  335. (vvp)->tv_sec--; \
  336. (vvp)->tv_usec += 1000000; \
  337. } \
  338. } while (0)
  339. #endif
  340. #endif /* __BSD_VISIBLE */
  341. /*
  342. * Names of the interval timers, and structure
  343. * defining a timer setting.
  344. */
  345. #define ITIMER_REAL 0
  346. #define ITIMER_VIRTUAL 1
  347. #define ITIMER_PROF 2
  348. struct itimerval {
  349. struct timeval it_interval; /* timer interval */
  350. struct timeval it_value; /* current value */
  351. };
  352. #ifndef _KERNEL
  353. #include <time.h>
  354. __BEGIN_DECLS
  355. int utimes (const char *__path, const struct timeval *__tvp);
  356. #if __BSD_VISIBLE
  357. int adjtime (const struct timeval *, struct timeval *);
  358. int futimes (int, const struct timeval *);
  359. int lutimes (const char *, const struct timeval *);
  360. int settimeofday (const struct timeval *, const struct timezone *);
  361. #endif
  362. #if __MISC_VISIBLE || __XSI_VISIBLE
  363. int getitimer (int __which, struct itimerval *__value);
  364. int setitimer (int __which, const struct itimerval *__restrict __value,
  365. struct itimerval *__restrict __ovalue);
  366. #endif
  367. int gettimeofday (struct timeval *__restrict __p,
  368. void *__restrict __tz);
  369. #if __GNU_VISIBLE
  370. int futimesat (int, const char *, const struct timeval [2]);
  371. #endif
  372. #ifdef _COMPILING_NEWLIB
  373. int _gettimeofday (struct timeval *__p, void *__tz);
  374. #endif
  375. __END_DECLS
  376. #endif /* !_KERNEL */
  377. #include <machine/_time.h>
  378. #endif /* !_SYS_TIME_H_ */