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 * Background information:
96 * When converting between timestamps on parallel timescales of differing
97 * resolutions it is historical and scientific practice to round down rather
98 * than doing 4/5 rounding.
100 * The date changes at midnight, not at noon.
102 * Even at 15:59:59.999999999 it's not four'o'clock.
104 * time_second ticks after N.999999999 not after N.4999999999
108 bintime2timespec(const struct bintime *bt, struct timespec *ts)
111 ts->tv_sec = bt->sec;
112 ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
116 timespec2bintime(const struct timespec *ts, struct bintime *bt)
119 bt->sec = ts->tv_sec;
120 /* 18446744073 = int(2^64 / 1000000000) */
121 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
125 bintime2timeval(const struct bintime *bt, struct timeval *tv)
128 tv->tv_sec = bt->sec;
129 tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
133 timeval2bintime(const struct timeval *tv, struct bintime *bt)
136 bt->sec = tv->tv_sec;
137 /* 18446744073709 = int(2^64 / 1000000) */
138 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
140 #endif /* __BSD_VISIBLE */
144 /* Operations on timespecs */
145 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
146 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
147 #define timespeccmp(tvp, uvp, cmp) \
148 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
149 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
150 ((tvp)->tv_sec cmp (uvp)->tv_sec))
151 #define timespecadd(vvp, uvp) \
153 (vvp)->tv_sec += (uvp)->tv_sec; \
154 (vvp)->tv_nsec += (uvp)->tv_nsec; \
155 if ((vvp)->tv_nsec >= 1000000000) { \
157 (vvp)->tv_nsec -= 1000000000; \
160 #define timespecsub(vvp, uvp) \
162 (vvp)->tv_sec -= (uvp)->tv_sec; \
163 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
164 if ((vvp)->tv_nsec < 0) { \
166 (vvp)->tv_nsec += 1000000000; \
170 /* Operations on timevals. */
172 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
173 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
174 #define timevalcmp(tvp, uvp, cmp) \
175 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
176 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
177 ((tvp)->tv_sec cmp (uvp)->tv_sec))
179 /* timevaladd and timevalsub are not inlined */
183 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
185 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
186 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
187 #define timercmp(tvp, uvp, cmp) \
188 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
189 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
190 ((tvp)->tv_sec cmp (uvp)->tv_sec))
191 #define timeradd(tvp, uvp, vvp) \
193 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
194 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
195 if ((vvp)->tv_usec >= 1000000) { \
197 (vvp)->tv_usec -= 1000000; \
200 #define timersub(tvp, uvp, vvp) \
202 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
203 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
204 if ((vvp)->tv_usec < 0) { \
206 (vvp)->tv_usec += 1000000; \
212 * Names of the interval timers, and structure
213 * defining a timer setting.
215 #define ITIMER_REAL 0
216 #define ITIMER_VIRTUAL 1
217 #define ITIMER_PROF 2
220 struct timeval it_interval; /* timer interval */
221 struct timeval it_value; /* current value */
225 * Getkerninfo clock information structure
228 int hz; /* clock frequency */
229 int tick; /* micro-seconds per hz tick */
231 int stathz; /* statistics clock frequency */
232 int profhz; /* profiling clock frequency */
235 /* These macros are also in time.h. */
236 #ifndef CLOCK_REALTIME
237 #define CLOCK_REALTIME 0
238 #define CLOCK_VIRTUAL 1
240 #define CLOCK_MONOTONIC 4
241 #define CLOCK_UPTIME 5 /* FreeBSD-specific. */
242 #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
243 #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
244 #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
245 #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
246 #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
247 #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
248 #define CLOCK_SECOND 13 /* FreeBSD-specific. */
249 #define CLOCK_THREAD_CPUTIME_ID 14
252 #ifndef TIMER_ABSTIME
253 #define TIMER_RELTIME 0x0 /* relative timer */
254 #define TIMER_ABSTIME 0x1 /* absolute timer */
260 * Kernel to clock driver interface.
262 void inittodr(time_t base);
263 void resettodr(void);
265 extern time_t time_second;
266 extern time_t time_uptime;
267 extern struct timeval boottime;
270 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
272 * Functions without the "get" prefix returns the best timestamp
273 * we can produce in the given format.
275 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
276 * "nano" == struct timespec == seconds + nanoseconds.
277 * "micro" == struct timeval == seconds + microseconds.
279 * Functions containing "up" returns time relative to boot and
280 * should be used for calculating time intervals.
282 * Functions without "up" returns GMT time.
284 * Functions with the "get" prefix returns a less precise result
285 * much faster than the functions without "get" prefix and should
286 * be used where a precision of 10 msec is acceptable or where
287 * performance is priority. (NB: "precision", _not_ "resolution" !)
291 void binuptime(struct bintime *bt);
292 void nanouptime(struct timespec *tsp);
293 void microuptime(struct timeval *tvp);
295 void bintime(struct bintime *bt);
296 void nanotime(struct timespec *tsp);
297 void microtime(struct timeval *tvp);
299 void getbinuptime(struct bintime *bt);
300 void getnanouptime(struct timespec *tsp);
301 void getmicrouptime(struct timeval *tvp);
303 void getbintime(struct bintime *bt);
304 void getnanotime(struct timespec *tsp);
305 void getmicrotime(struct timeval *tvp);
307 /* Other functions */
308 int itimerdecr(struct itimerval *itp, int usec);
309 int itimerfix(struct timeval *tv);
310 int ppsratecheck(struct timeval *, int *, int);
311 int ratecheck(struct timeval *, const struct timeval *);
312 void timevaladd(struct timeval *t1, const struct timeval *t2);
313 void timevalsub(struct timeval *t1, const struct timeval *t2);
314 int tvtohz(struct timeval *tv);
318 #include <sys/cdefs.h>
321 int adjtime(const struct timeval *, struct timeval *);
322 int futimes(int, const struct timeval *);
323 int getitimer(int, struct itimerval *);
324 int gettimeofday(struct timeval *, struct timezone *);
325 int lutimes(const char *, const struct timeval *);
326 int setitimer(int, const struct itimerval *, struct itimerval *);
327 int settimeofday(const struct timeval *, const struct timezone *);
328 int utimes(const char *, const struct timeval *);
329 int futimesat(int, const char *, const struct timeval [2]);
332 #endif /* !_KERNEL */
334 #endif /* !_SYS_TIME_H_ */