time.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* $OpenBSD: time.h,v 1.35 2013/10/25 04:42:48 guenther Exp $ */
  2. /* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)time.h 8.2 (Berkeley) 7/10/94
  32. */
  33. #ifndef _SYS_TIME_H_
  34. #define _SYS_TIME_H_
  35. #include <sys/types.h>
  36. /*
  37. * Structure returned by gettimeofday(2) system call,
  38. * and used in other calls.
  39. */
  40. struct timeval {
  41. time_t tv_sec; /* seconds */
  42. suseconds_t tv_usec; /* and microseconds */
  43. };
  44. #ifndef _TIMESPEC_DECLARED
  45. #define _TIMESPEC_DECLARED
  46. /*
  47. * Structure defined by POSIX.1b to be like a timeval.
  48. */
  49. struct timespec {
  50. time_t tv_sec; /* seconds */
  51. long tv_nsec; /* and nanoseconds */
  52. };
  53. #endif
  54. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  55. (ts)->tv_sec = (tv)->tv_sec; \
  56. (ts)->tv_nsec = (tv)->tv_usec * 1000; \
  57. }
  58. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  59. (tv)->tv_sec = (ts)->tv_sec; \
  60. (tv)->tv_usec = (ts)->tv_nsec / 1000; \
  61. }
  62. struct timezone {
  63. int tz_minuteswest; /* minutes west of Greenwich */
  64. int tz_dsttime; /* type of dst correction */
  65. };
  66. #define DST_NONE 0 /* not on dst */
  67. #define DST_USA 1 /* USA style dst */
  68. #define DST_AUST 2 /* Australian style dst */
  69. #define DST_WET 3 /* Western European dst */
  70. #define DST_MET 4 /* Middle European dst */
  71. #define DST_EET 5 /* Eastern European dst */
  72. #define DST_CAN 6 /* Canada */
  73. /* Operations on timevals. */
  74. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  75. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  76. #define timercmp(tvp, uvp, cmp) \
  77. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  78. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  79. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  80. #define timeradd(tvp, uvp, vvp) \
  81. do { \
  82. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  83. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  84. if ((vvp)->tv_usec >= 1000000) { \
  85. (vvp)->tv_sec++; \
  86. (vvp)->tv_usec -= 1000000; \
  87. } \
  88. } while (0)
  89. #define timersub(tvp, uvp, vvp) \
  90. do { \
  91. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  92. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  93. if ((vvp)->tv_usec < 0) { \
  94. (vvp)->tv_sec--; \
  95. (vvp)->tv_usec += 1000000; \
  96. } \
  97. } while (0)
  98. /* Operations on timespecs. */
  99. #define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
  100. #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
  101. #define timespeccmp(tsp, usp, cmp) \
  102. (((tsp)->tv_sec == (usp)->tv_sec) ? \
  103. ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
  104. ((tsp)->tv_sec cmp (usp)->tv_sec))
  105. #define timespecadd(tsp, usp, vsp) \
  106. do { \
  107. (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
  108. (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
  109. if ((vsp)->tv_nsec >= 1000000000L) { \
  110. (vsp)->tv_sec++; \
  111. (vsp)->tv_nsec -= 1000000000L; \
  112. } \
  113. } while (0)
  114. #define timespecsub(tsp, usp, vsp) \
  115. do { \
  116. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  117. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  118. if ((vsp)->tv_nsec < 0) { \
  119. (vsp)->tv_sec--; \
  120. (vsp)->tv_nsec += 1000000000L; \
  121. } \
  122. } while (0)
  123. /*
  124. * Names of the interval timers, and structure
  125. * defining a timer setting.
  126. */
  127. #define ITIMER_REAL 0
  128. #define ITIMER_VIRTUAL 1
  129. #define ITIMER_PROF 2
  130. struct itimerval {
  131. struct timeval it_interval; /* timer interval */
  132. struct timeval it_value; /* current value */
  133. };
  134. #if __BSD_VISIBLE
  135. /*
  136. * clock information structure for sysctl({CTL_KERN, KERN_CLOCKRATE})
  137. */
  138. struct clockinfo {
  139. int hz; /* clock frequency */
  140. int tick; /* micro-seconds per hz tick */
  141. int tickadj; /* clock skew rate for adjtime() */
  142. int stathz; /* statistics clock frequency */
  143. int profhz; /* profiling clock frequency */
  144. };
  145. #endif /* __BSD_VISIBLE */
  146. #if defined(_KERNEL) || defined(_STANDALONE)
  147. #include <sys/_time.h>
  148. /* Time expressed as seconds and fractions of a second + operations on it. */
  149. struct bintime {
  150. time_t sec;
  151. uint64_t frac;
  152. };
  153. static __inline void
  154. bintime_addx(struct bintime *bt, uint64_t x)
  155. {
  156. uint64_t u;
  157. u = bt->frac;
  158. bt->frac += x;
  159. if (u > bt->frac)
  160. bt->sec++;
  161. }
  162. static __inline void
  163. bintime_add(struct bintime *bt, struct bintime *bt2)
  164. {
  165. uint64_t u;
  166. u = bt->frac;
  167. bt->frac += bt2->frac;
  168. if (u > bt->frac)
  169. bt->sec++;
  170. bt->sec += bt2->sec;
  171. }
  172. static __inline void
  173. bintime_sub(struct bintime *bt, struct bintime *bt2)
  174. {
  175. uint64_t u;
  176. u = bt->frac;
  177. bt->frac -= bt2->frac;
  178. if (u < bt->frac)
  179. bt->sec--;
  180. bt->sec -= bt2->sec;
  181. }
  182. /*-
  183. * Background information:
  184. *
  185. * When converting between timestamps on parallel timescales of differing
  186. * resolutions it is historical and scientific practice to round down rather
  187. * than doing 4/5 rounding.
  188. *
  189. * The date changes at midnight, not at noon.
  190. *
  191. * Even at 15:59:59.999999999 it's not four'o'clock.
  192. *
  193. * time_second ticks after N.999999999 not after N.4999999999
  194. */
  195. static __inline void
  196. bintime2timespec(struct bintime *bt, struct timespec *ts)
  197. {
  198. ts->tv_sec = bt->sec;
  199. ts->tv_nsec = (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
  200. }
  201. static __inline void
  202. timespec2bintime(struct timespec *ts, struct bintime *bt)
  203. {
  204. bt->sec = ts->tv_sec;
  205. /* 18446744073 = int(2^64 / 1000000000) */
  206. bt->frac = (uint64_t)ts->tv_nsec * (uint64_t)18446744073ULL;
  207. }
  208. static __inline void
  209. bintime2timeval(struct bintime *bt, struct timeval *tv)
  210. {
  211. tv->tv_sec = bt->sec;
  212. tv->tv_usec = (long)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
  213. }
  214. static __inline void
  215. timeval2bintime(struct timeval *tv, struct bintime *bt)
  216. {
  217. bt->sec = (time_t)tv->tv_sec;
  218. /* 18446744073709 = int(2^64 / 1000000) */
  219. bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL;
  220. }
  221. extern volatile time_t time_second; /* Seconds since epoch, wall time. */
  222. extern volatile time_t time_uptime; /* Seconds since reboot. */
  223. /*
  224. * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
  225. *
  226. * Functions without the "get" prefix returns the best timestamp
  227. * we can produce in the given format.
  228. *
  229. * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
  230. * "nano" == struct timespec == seconds + nanoseconds.
  231. * "micro" == struct timeval == seconds + microseconds.
  232. *
  233. * Functions containing "up" returns time relative to boot and
  234. * should be used for calculating time intervals.
  235. *
  236. * Functions without "up" returns GMT time.
  237. *
  238. * Functions with the "get" prefix returns a less precise result
  239. * much faster than the functions without "get" prefix and should
  240. * be used where a precision of 10 msec is acceptable or where
  241. * performance is priority. (NB: "precision", _not_ "resolution" !)
  242. */
  243. void bintime(struct bintime *);
  244. void nanotime(struct timespec *);
  245. void microtime(struct timeval *);
  246. void getnanotime(struct timespec *);
  247. void getmicrotime(struct timeval *);
  248. void binuptime(struct bintime *);
  249. void nanouptime(struct timespec *);
  250. void microuptime(struct timeval *);
  251. void getnanouptime(struct timespec *);
  252. void getmicrouptime(struct timeval *);
  253. struct proc;
  254. int clock_gettime(struct proc *, clockid_t, struct timespec *);
  255. int timespecfix(struct timespec *);
  256. int itimerfix(struct timeval *);
  257. int itimerdecr(struct itimerval *itp, int usec);
  258. void itimerround(struct timeval *);
  259. int settime(struct timespec *);
  260. int ratecheck(struct timeval *, const struct timeval *);
  261. int ppsratecheck(struct timeval *, int *, int);
  262. /*
  263. * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
  264. */
  265. struct clock_ymdhms {
  266. u_short dt_year;
  267. u_char dt_mon;
  268. u_char dt_day;
  269. u_char dt_wday; /* Day of week */
  270. u_char dt_hour;
  271. u_char dt_min;
  272. u_char dt_sec;
  273. };
  274. time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
  275. void clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
  276. /*
  277. * BCD to decimal and decimal to BCD.
  278. */
  279. #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
  280. #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
  281. /* Some handy constants. */
  282. #define SECDAY 86400L
  283. #define SECYR (SECDAY * 365)
  284. /* Traditional POSIX base year */
  285. #define POSIX_BASE_YEAR 1970
  286. #else /* !_KERNEL */
  287. #include <time.h>
  288. #if __XPG_VISIBLE >= 420 && __XPG_VISIBLE < 600
  289. #include <sys/select.h> /* must be after type declarations */
  290. #endif
  291. #if __BSD_VISIBLE || __XPG_VISIBLE
  292. __BEGIN_DECLS
  293. #if __BSD_VISIBLE
  294. int adjtime(const struct timeval *, struct timeval *);
  295. int adjfreq(const int64_t *, int64_t *);
  296. #endif
  297. #if __XPG_VISIBLE
  298. int futimes(int, const struct timeval *);
  299. int getitimer(int, struct itimerval *);
  300. int gettimeofday(struct timeval *, struct timezone *);
  301. int setitimer(int, const struct itimerval *, struct itimerval *);
  302. int settimeofday(const struct timeval *, const struct timezone *);
  303. int utimes(const char *, const struct timeval *);
  304. #endif /* __XPG_VISIBLE */
  305. __END_DECLS
  306. #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
  307. #endif /* !_KERNEL */
  308. #endif /* !_SYS_TIME_H_ */