2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)time.h 8.5 (Berkeley) 5/4/95
36 #include <sys/_timeval.h>
37 #include <sys/types.h>
38 #include <sys/timespec.h>
41 int tz_minuteswest; /* minutes west of Greenwich */
42 int tz_dsttime; /* type of dst correction */
44 #define DST_NONE 0 /* not on dst */
45 #define DST_USA 1 /* USA style dst */
46 #define DST_AUST 2 /* Australian style dst */
47 #define DST_WET 3 /* Western European dst */
48 #define DST_MET 4 /* Middle European dst */
49 #define DST_EET 5 /* Eastern European dst */
50 #define DST_CAN 6 /* Canada */
59 bintime_addx(struct bintime *bt, uint64_t x)
70 bintime_add(struct bintime *bt, const struct bintime *bt2)
75 bt->frac += bt2->frac;
82 bintime_sub(struct bintime *bt, const struct bintime *bt2)
87 bt->frac -= bt2->frac;
94 bintime_mul(struct bintime *bt, u_int x)
98 p1 = (bt->frac & 0xffffffffull) * x;
99 p2 = (bt->frac >> 32) * x + (p1 >> 32);
101 bt->sec += (p2 >> 32);
102 bt->frac = (p2 << 32) | (p1 & 0xffffffffull);
105 #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
106 #define bintime_isset(a) ((a)->sec || (a)->frac)
107 #define bintime_cmp(a, b, cmp) \
108 (((a)->sec == (b)->sec) ? \
109 ((a)->frac cmp (b)->frac) : \
110 ((a)->sec cmp (b)->sec))
113 * Background information:
115 * When converting between timestamps on parallel timescales of differing
116 * resolutions it is historical and scientific practice to round down rather
117 * than doing 4/5 rounding.
119 * The date changes at midnight, not at noon.
121 * Even at 15:59:59.999999999 it's not four'o'clock.
123 * time_second ticks after N.999999999 not after N.4999999999
127 bintime2timespec(const struct bintime *bt, struct timespec *ts)
130 ts->tv_sec = bt->sec;
131 ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
135 timespec2bintime(const struct timespec *ts, struct bintime *bt)
138 bt->sec = ts->tv_sec;
139 /* 18446744073 = int(2^64 / 1000000000) */
140 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
144 bintime2timeval(const struct bintime *bt, struct timeval *tv)
147 tv->tv_sec = bt->sec;
148 tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
152 timeval2bintime(const struct timeval *tv, struct bintime *bt)
155 bt->sec = tv->tv_sec;
156 /* 18446744073709 = int(2^64 / 1000000) */
157 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
159 #endif /* __BSD_VISIBLE */
163 /* Operations on timespecs */
164 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
165 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
166 #define timespeccmp(tvp, uvp, cmp) \
167 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
168 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
169 ((tvp)->tv_sec cmp (uvp)->tv_sec))
170 #define timespecadd(vvp, uvp) \
172 (vvp)->tv_sec += (uvp)->tv_sec; \
173 (vvp)->tv_nsec += (uvp)->tv_nsec; \
174 if ((vvp)->tv_nsec >= 1000000000) { \
176 (vvp)->tv_nsec -= 1000000000; \
179 #define timespecsub(vvp, uvp) \
181 (vvp)->tv_sec -= (uvp)->tv_sec; \
182 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
183 if ((vvp)->tv_nsec < 0) { \
185 (vvp)->tv_nsec += 1000000000; \
189 /* Operations on timevals. */
191 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
192 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
193 #define timevalcmp(tvp, uvp, cmp) \
194 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
195 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
196 ((tvp)->tv_sec cmp (uvp)->tv_sec))
198 /* timevaladd and timevalsub are not inlined */
202 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
204 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
205 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
206 #define timercmp(tvp, uvp, cmp) \
207 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
208 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
209 ((tvp)->tv_sec cmp (uvp)->tv_sec))
210 #define timeradd(tvp, uvp, vvp) \
212 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
213 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
214 if ((vvp)->tv_usec >= 1000000) { \
216 (vvp)->tv_usec -= 1000000; \
219 #define timersub(tvp, uvp, vvp) \
221 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
222 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
223 if ((vvp)->tv_usec < 0) { \
225 (vvp)->tv_usec += 1000000; \
231 * Names of the interval timers, and structure
232 * defining a timer setting.
234 #define ITIMER_REAL 0
235 #define ITIMER_VIRTUAL 1
236 #define ITIMER_PROF 2
239 struct timeval it_interval; /* timer interval */
240 struct timeval it_value; /* current value */
244 * Getkerninfo clock information structure
247 int hz; /* clock frequency */
248 int tick; /* micro-seconds per hz tick */
250 int stathz; /* statistics clock frequency */
251 int profhz; /* profiling clock frequency */
254 /* These macros are also in time.h. */
255 #ifndef CLOCK_REALTIME
256 #define CLOCK_REALTIME 0
257 #define CLOCK_VIRTUAL 1
259 #define CLOCK_MONOTONIC 4
260 #define CLOCK_UPTIME 5 /* FreeBSD-specific. */
261 #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
262 #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
263 #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
264 #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
265 #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
266 #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
267 #define CLOCK_SECOND 13 /* FreeBSD-specific. */
268 #define CLOCK_THREAD_CPUTIME_ID 14
269 #define CLOCK_PROCESS_CPUTIME_ID 15
272 #ifndef TIMER_ABSTIME
273 #define TIMER_RELTIME 0x0 /* relative timer */
274 #define TIMER_ABSTIME 0x1 /* absolute timer */
278 #define CPUCLOCK_WHICH_PID 0
279 #define CPUCLOCK_WHICH_TID 1
285 * Kernel to clock driver interface.
287 void inittodr(time_t base);
288 void resettodr(void);
290 extern volatile time_t time_second;
291 extern volatile time_t time_uptime;
292 extern struct bintime boottimebin;
293 extern struct timeval boottime;
296 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
298 * Functions without the "get" prefix returns the best timestamp
299 * we can produce in the given format.
301 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
302 * "nano" == struct timespec == seconds + nanoseconds.
303 * "micro" == struct timeval == seconds + microseconds.
305 * Functions containing "up" returns time relative to boot and
306 * should be used for calculating time intervals.
308 * Functions without "up" returns GMT time.
310 * Functions with the "get" prefix returns a less precise result
311 * much faster than the functions without "get" prefix and should
312 * be used where a precision of 1/hz seconds is acceptable or where
313 * performance is priority. (NB: "precision", _not_ "resolution" !)
317 void binuptime(struct bintime *bt);
318 void nanouptime(struct timespec *tsp);
319 void microuptime(struct timeval *tvp);
321 void bintime(struct bintime *bt);
322 void nanotime(struct timespec *tsp);
323 void microtime(struct timeval *tvp);
325 void getbinuptime(struct bintime *bt);
326 void getnanouptime(struct timespec *tsp);
327 void getmicrouptime(struct timeval *tvp);
329 void getbintime(struct bintime *bt);
330 void getnanotime(struct timespec *tsp);
331 void getmicrotime(struct timeval *tvp);
333 /* Other functions */
334 int itimerdecr(struct itimerval *itp, int usec);
335 int itimerfix(struct timeval *tv);
336 int ppsratecheck(struct timeval *, int *, int);
337 int ratecheck(struct timeval *, const struct timeval *);
338 void timevaladd(struct timeval *t1, const struct timeval *t2);
339 void timevalsub(struct timeval *t1, const struct timeval *t2);
340 int tvtohz(struct timeval *tv);
344 #include <sys/cdefs.h>
345 #include <sys/select.h>
348 int setitimer(int, const struct itimerval *, struct itimerval *);
349 int utimes(const char *, const struct timeval *);
352 int adjtime(const struct timeval *, struct timeval *);
353 int clock_getcpuclockid2(id_t, int, clockid_t *);
354 int futimes(int, const struct timeval *);
355 int futimesat(int, const char *, const struct timeval [2]);
356 int lutimes(const char *, const struct timeval *);
357 int settimeofday(const struct timeval *, const struct timezone *);
361 int getitimer(int, struct itimerval *);
362 int gettimeofday(struct timeval *, struct timezone *);
367 #endif /* !_KERNEL */
369 #endif /* !_SYS_TIME_H_ */