time.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 340664 2018-11-20 07:11:23Z 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. return ((1000000000 * _sbt) >> 32);
  170. }
  171. static __inline sbintime_t
  172. nstosbt(int64_t _ns)
  173. {
  174. sbintime_t sb = 0;
  175. if (_ns >= SBT_1S) {
  176. sb = (_ns / 1000000000) * SBT_1S;
  177. _ns = _ns % 1000000000;
  178. }
  179. /* 9223372037 = ceil(2^63 / 1000000000) */
  180. sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
  181. return (sb);
  182. }
  183. static __inline int64_t
  184. sbttous(sbintime_t _sbt)
  185. {
  186. return ((1000000 * _sbt) >> 32);
  187. }
  188. static __inline sbintime_t
  189. ustosbt(int64_t _us)
  190. {
  191. sbintime_t sb = 0;
  192. if (_us >= SBT_1S) {
  193. sb = (_us / 1000000) * SBT_1S;
  194. _us = _us % 1000000;
  195. }
  196. /* 9223372036855 = ceil(2^63 / 1000000) */
  197. sb += ((_us * 9223372036855ull) + 0x7fffffff) >> 31;
  198. return (sb);
  199. }
  200. static __inline int64_t
  201. sbttoms(sbintime_t _sbt)
  202. {
  203. return ((1000 * _sbt) >> 32);
  204. }
  205. static __inline sbintime_t
  206. mstosbt(int64_t _ms)
  207. {
  208. sbintime_t sb = 0;
  209. if (_ms >= SBT_1S) {
  210. sb = (_ms / 1000) * SBT_1S;
  211. _ms = _ms % 1000;
  212. }
  213. /* 9223372036854776 = ceil(2^63 / 1000) */
  214. sb += ((_ms * 9223372036854776ull) + 0x7fffffff) >> 31;
  215. return (sb);
  216. }
  217. /*-
  218. * Background information:
  219. *
  220. * When converting between timestamps on parallel timescales of differing
  221. * resolutions it is historical and scientific practice to round down rather
  222. * than doing 4/5 rounding.
  223. *
  224. * The date changes at midnight, not at noon.
  225. *
  226. * Even at 15:59:59.999999999 it's not four'o'clock.
  227. *
  228. * time_second ticks after N.999999999 not after N.4999999999
  229. */
  230. static __inline void
  231. bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
  232. {
  233. _ts->tv_sec = _bt->sec;
  234. _ts->tv_nsec = ((uint64_t)1000000000 *
  235. (uint32_t)(_bt->frac >> 32)) >> 32;
  236. }
  237. static __inline void
  238. timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
  239. {
  240. _bt->sec = _ts->tv_sec;
  241. /* 18446744073 = int(2^64 / 1000000000) */
  242. _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
  243. }
  244. static __inline void
  245. bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
  246. {
  247. _tv->tv_sec = _bt->sec;
  248. _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
  249. }
  250. static __inline void
  251. timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
  252. {
  253. _bt->sec = _tv->tv_sec;
  254. /* 18446744073709 = int(2^64 / 1000000) */
  255. _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
  256. }
  257. static __inline struct timespec
  258. sbttots(sbintime_t _sbt)
  259. {
  260. struct timespec _ts;
  261. _ts.tv_sec = _sbt >> 32;
  262. _ts.tv_nsec = sbttons((uint32_t)_sbt);
  263. return (_ts);
  264. }
  265. static __inline sbintime_t
  266. tstosbt(struct timespec _ts)
  267. {
  268. return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
  269. }
  270. static __inline struct timeval
  271. sbttotv(sbintime_t _sbt)
  272. {
  273. struct timeval _tv;
  274. _tv.tv_sec = _sbt >> 32;
  275. _tv.tv_usec = sbttous((uint32_t)_sbt);
  276. return (_tv);
  277. }
  278. static __inline sbintime_t
  279. tvtosbt(struct timeval _tv)
  280. {
  281. return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
  282. }
  283. #endif /* __BSD_VISIBLE */
  284. /*
  285. * Names of the interval timers, and structure
  286. * defining a timer setting.
  287. */
  288. #define ITIMER_REAL 0
  289. #define ITIMER_VIRTUAL 1
  290. #define ITIMER_PROF 2
  291. struct itimerval {
  292. struct timeval it_interval; /* timer interval */
  293. struct timeval it_value; /* current value */
  294. };
  295. #ifndef _KERNEL
  296. #include <time.h>
  297. __BEGIN_DECLS
  298. int utimes (const char *__path, const struct timeval *__tvp);
  299. #if __BSD_VISIBLE
  300. int adjtime (const struct timeval *, struct timeval *);
  301. int futimes (int, const struct timeval *);
  302. int lutimes (const char *, const struct timeval *);
  303. int settimeofday (const struct timeval *, const struct timezone *);
  304. #endif
  305. #if __MISC_VISIBLE || __XSI_VISIBLE
  306. int getitimer (int __which, struct itimerval *__value);
  307. int setitimer (int __which, const struct itimerval *__restrict __value,
  308. struct itimerval *__restrict __ovalue);
  309. #endif
  310. int gettimeofday (struct timeval *__restrict __p,
  311. void *__restrict __tz);
  312. #if __GNU_VISIBLE
  313. int futimesat (int, const char *, const struct timeval [2]);
  314. #endif
  315. #ifdef _COMPILING_NEWLIB
  316. int _gettimeofday (struct timeval *__p, void *__tz);
  317. #endif
  318. __END_DECLS
  319. #endif /* !_KERNEL */
  320. #include <machine/_time.h>
  321. #endif /* !_SYS_TIME_H_ */