1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
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 | * 3. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * @(#)time.h 8.5 (Berkeley) 5/4/95
|
---|
34 | * $FreeBSD: src/sys/sys/time.h,v 1.61 2003/02/23 10:18:31 phk Exp $
|
---|
35 | */
|
---|
36 |
|
---|
37 | /** @file
|
---|
38 | * FreeBSD 5.1
|
---|
39 | *
|
---|
40 | * @changed EMX isms
|
---|
41 | * @changed IBM TCP paranoia.
|
---|
42 | * @changed Added hrtime_t and gethrtime from SUN/HP/RTLinux.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #ifndef _SYS_TIME_H_
|
---|
46 | #define _SYS_TIME_H_
|
---|
47 |
|
---|
48 | #include <sys/_timeval.h>
|
---|
49 | #include <sys/types.h>
|
---|
50 | #include <sys/timespec.h>
|
---|
51 |
|
---|
52 | #if !defined (_TIMEZONE) /* bird: EMX */
|
---|
53 | #define _TIMEZONE /* bird: EMX */
|
---|
54 | struct timezone {
|
---|
55 | int tz_minuteswest; /* minutes west of Greenwich */
|
---|
56 | int tz_dsttime; /* type of dst correction */
|
---|
57 | };
|
---|
58 | #endif /* bird: EMX */
|
---|
59 | #define DST_NONE 0 /* not on dst */
|
---|
60 | #define DST_USA 1 /* USA style dst */
|
---|
61 | #define DST_AUST 2 /* Australian style dst */
|
---|
62 | #define DST_WET 3 /* Western European dst */
|
---|
63 | #define DST_MET 4 /* Middle European dst */
|
---|
64 | #define DST_EET 5 /* Eastern European dst */
|
---|
65 | #define DST_CAN 6 /* Canada */
|
---|
66 |
|
---|
67 | #if __BSD_VISIBLE
|
---|
68 | struct bintime {
|
---|
69 | time_t sec;
|
---|
70 | uint64_t frac;
|
---|
71 | };
|
---|
72 |
|
---|
73 | static __inline void
|
---|
74 | bintime_addx(struct bintime *bt, uint64_t x)
|
---|
75 | {
|
---|
76 | uint64_t u;
|
---|
77 |
|
---|
78 | u = bt->frac;
|
---|
79 | bt->frac += x;
|
---|
80 | if (u > bt->frac)
|
---|
81 | bt->sec++;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static __inline void
|
---|
85 | bintime_add(struct bintime *bt, struct bintime *bt2)
|
---|
86 | {
|
---|
87 | uint64_t u;
|
---|
88 |
|
---|
89 | u = bt->frac;
|
---|
90 | bt->frac += bt2->frac;
|
---|
91 | if (u > bt->frac)
|
---|
92 | bt->sec++;
|
---|
93 | bt->sec += bt2->sec;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static __inline void
|
---|
97 | bintime_sub(struct bintime *bt, struct bintime *bt2)
|
---|
98 | {
|
---|
99 | uint64_t u;
|
---|
100 |
|
---|
101 | u = bt->frac;
|
---|
102 | bt->frac -= bt2->frac;
|
---|
103 | if (u < bt->frac)
|
---|
104 | bt->sec--;
|
---|
105 | bt->sec -= bt2->sec;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*-
|
---|
109 | * Background information:
|
---|
110 | *
|
---|
111 | * When converting between timestamps on parallel timescales of differing
|
---|
112 | * resolutions it is historical and scientific practice to round down rather
|
---|
113 | * than doing 4/5 rounding.
|
---|
114 | *
|
---|
115 | * The date changes at midnight, not at noon.
|
---|
116 | *
|
---|
117 | * Even at 15:59:59.999999999 it's not four'o'clock.
|
---|
118 | *
|
---|
119 | * time_second ticks after N.999999999 not after N.4999999999
|
---|
120 | */
|
---|
121 |
|
---|
122 | static __inline void
|
---|
123 | bintime2timespec(struct bintime *bt, struct timespec *ts)
|
---|
124 | {
|
---|
125 |
|
---|
126 | ts->tv_sec = bt->sec;
|
---|
127 | ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static __inline void
|
---|
131 | timespec2bintime(struct timespec *ts, struct bintime *bt)
|
---|
132 | {
|
---|
133 |
|
---|
134 | bt->sec = ts->tv_sec;
|
---|
135 | /* 18446744073 = int(2^64 / 1000000000) */
|
---|
136 | bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static __inline void
|
---|
140 | bintime2timeval(struct bintime *bt, struct timeval *tv)
|
---|
141 | {
|
---|
142 |
|
---|
143 | tv->tv_sec = bt->sec;
|
---|
144 | tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static __inline void
|
---|
148 | timeval2bintime(struct timeval *tv, struct bintime *bt)
|
---|
149 | {
|
---|
150 |
|
---|
151 | bt->sec = tv->tv_sec;
|
---|
152 | /* 18446744073709 = int(2^64 / 1000000) */
|
---|
153 | bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
|
---|
154 | }
|
---|
155 | #endif /* __BSD_VISIBLE */
|
---|
156 |
|
---|
157 | #ifdef _KERNEL
|
---|
158 |
|
---|
159 | /* Operations on timespecs */
|
---|
160 | #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
|
---|
161 | #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
|
---|
162 | #define timespeccmp(tvp, uvp, cmp) \
|
---|
163 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \
|
---|
164 | ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
|
---|
165 | ((tvp)->tv_sec cmp (uvp)->tv_sec))
|
---|
166 | #define timespecadd(vvp, uvp) \
|
---|
167 | do { \
|
---|
168 | (vvp)->tv_sec += (uvp)->tv_sec; \
|
---|
169 | (vvp)->tv_nsec += (uvp)->tv_nsec; \
|
---|
170 | if ((vvp)->tv_nsec >= 1000000000) { \
|
---|
171 | (vvp)->tv_sec++; \
|
---|
172 | (vvp)->tv_nsec -= 1000000000; \
|
---|
173 | } \
|
---|
174 | } while (0)
|
---|
175 | #define timespecsub(vvp, uvp) \
|
---|
176 | do { \
|
---|
177 | (vvp)->tv_sec -= (uvp)->tv_sec; \
|
---|
178 | (vvp)->tv_nsec -= (uvp)->tv_nsec; \
|
---|
179 | if ((vvp)->tv_nsec < 0) { \
|
---|
180 | (vvp)->tv_sec--; \
|
---|
181 | (vvp)->tv_nsec += 1000000000; \
|
---|
182 | } \
|
---|
183 | } while (0)
|
---|
184 |
|
---|
185 | /* Operations on timevals. */
|
---|
186 |
|
---|
187 | #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
|
---|
188 | #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
|
---|
189 | #define timevalcmp(tvp, uvp, cmp) \
|
---|
190 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \
|
---|
191 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
|
---|
192 | ((tvp)->tv_sec cmp (uvp)->tv_sec))
|
---|
193 |
|
---|
194 | /* timevaladd and timevalsub are not inlined */
|
---|
195 |
|
---|
196 | #endif /* _KERNEL */
|
---|
197 |
|
---|
198 | #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
|
---|
199 |
|
---|
200 | #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
|
---|
201 | #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
|
---|
202 | #ifndef timercmp /* bird: TCP paranoia */
|
---|
203 | #define timercmp(tvp, uvp, cmp) \
|
---|
204 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \
|
---|
205 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
|
---|
206 | ((tvp)->tv_sec cmp (uvp)->tv_sec))
|
---|
207 | #endif /* bird: TCP paranoia */
|
---|
208 | #ifndef timeradd /* bird: TCP paranoia */
|
---|
209 | #define timeradd(tvp, uvp, vvp) \
|
---|
210 | do { \
|
---|
211 | (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
|
---|
212 | (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
|
---|
213 | if ((vvp)->tv_usec >= 1000000) { \
|
---|
214 | (vvp)->tv_sec++; \
|
---|
215 | (vvp)->tv_usec -= 1000000; \
|
---|
216 | } \
|
---|
217 | } while (0)
|
---|
218 | #endif /* bird: TCP paranoia */
|
---|
219 | #ifndef timersub /* bird: TCP paranoia */
|
---|
220 | #define timersub(tvp, uvp, vvp) \
|
---|
221 | do { \
|
---|
222 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
|
---|
223 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
|
---|
224 | if ((vvp)->tv_usec < 0) { \
|
---|
225 | (vvp)->tv_sec--; \
|
---|
226 | (vvp)->tv_usec += 1000000; \
|
---|
227 | } \
|
---|
228 | } while (0)
|
---|
229 | #endif /* bird: TCP paranoia */
|
---|
230 | #endif
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Names of the interval timers, and structure
|
---|
234 | * defining a timer setting.
|
---|
235 | */
|
---|
236 | #define ITIMER_REAL 0
|
---|
237 | #define ITIMER_VIRTUAL 1
|
---|
238 | #define ITIMER_PROF 2
|
---|
239 |
|
---|
240 | struct itimerval {
|
---|
241 | struct timeval it_interval; /* timer interval */
|
---|
242 | struct timeval it_value; /* current value */
|
---|
243 | };
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Getkerninfo clock information structure
|
---|
247 | */
|
---|
248 | struct clockinfo {
|
---|
249 | int hz; /* clock frequency */
|
---|
250 | int tick; /* micro-seconds per hz tick */
|
---|
251 | int spare;
|
---|
252 | int stathz; /* statistics clock frequency */
|
---|
253 | int profhz; /* profiling clock frequency */
|
---|
254 | };
|
---|
255 |
|
---|
256 | /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
|
---|
257 |
|
---|
258 | #ifndef CLOCK_REALTIME
|
---|
259 | #define CLOCK_REALTIME 0
|
---|
260 | #endif
|
---|
261 | #define CLOCK_VIRTUAL 1
|
---|
262 | #define CLOCK_PROF 2
|
---|
263 | #define CLOCK_MONOTONIC 4
|
---|
264 |
|
---|
265 | #define TIMER_RELTIME 0x0 /* relative timer */
|
---|
266 | #ifndef TIMER_ABSTIME
|
---|
267 | #define TIMER_ABSTIME 0x1 /* absolute timer */
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | #ifdef _KERNEL
|
---|
271 | extern time_t time_second;
|
---|
272 | extern time_t time_uptime;
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
|
---|
276 | *
|
---|
277 | * Functions without the "get" prefix returns the best timestamp
|
---|
278 | * we can produce in the given format.
|
---|
279 | *
|
---|
280 | * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
|
---|
281 | * "nano" == struct timespec == seconds + nanoseconds.
|
---|
282 | * "micro" == struct timeval == seconds + microseconds.
|
---|
283 | *
|
---|
284 | * Functions containing "up" returns time relative to boot and
|
---|
285 | * should be used for calculating time intervals.
|
---|
286 | *
|
---|
287 | * Functions without "up" returns GMT time.
|
---|
288 | *
|
---|
289 | * Functions with the "get" prefix returns a less precise result
|
---|
290 | * much faster than the functions without "get" prefix and should
|
---|
291 | * be used where a precision of 10 msec is acceptable or where
|
---|
292 | * performance is priority. (NB: "precision", _not_ "resolution" !)
|
---|
293 | *
|
---|
294 | */
|
---|
295 |
|
---|
296 | void binuptime(struct bintime *bt);
|
---|
297 | void nanouptime(struct timespec *tsp);
|
---|
298 | void microuptime(struct timeval *tvp);
|
---|
299 |
|
---|
300 | void bintime(struct bintime *bt);
|
---|
301 | void nanotime(struct timespec *tsp);
|
---|
302 | void microtime(struct timeval *tvp);
|
---|
303 |
|
---|
304 | void getbinuptime(struct bintime *bt);
|
---|
305 | void getnanouptime(struct timespec *tsp);
|
---|
306 | void getmicrouptime(struct timeval *tvp);
|
---|
307 |
|
---|
308 | void getbintime(struct bintime *bt);
|
---|
309 | void getnanotime(struct timespec *tsp);
|
---|
310 | void getmicrotime(struct timeval *tvp);
|
---|
311 |
|
---|
312 | /* Other functions */
|
---|
313 | int itimerdecr(struct itimerval *itp, int usec);
|
---|
314 | int itimerfix(struct timeval *tv);
|
---|
315 | int ppsratecheck(struct timeval *, int *, int);
|
---|
316 | int ratecheck(struct timeval *, const struct timeval *);
|
---|
317 | void timevaladd(struct timeval *t1, struct timeval *t2);
|
---|
318 | void timevalsub(struct timeval *t1, struct timeval *t2);
|
---|
319 | int tvtohz(struct timeval *tv);
|
---|
320 | #else /* !_KERNEL */
|
---|
321 | #include <time.h>
|
---|
322 |
|
---|
323 | #include <sys/cdefs.h>
|
---|
324 |
|
---|
325 | __BEGIN_DECLS
|
---|
326 | /** @todo int adjtime(const struct timeval *, struct timeval *); */
|
---|
327 | int futimes(int, const struct timeval *);
|
---|
328 | int getitimer(int, struct itimerval *);
|
---|
329 | int gettimeofday(struct timeval *, struct timezone *);
|
---|
330 | int lutimes(const char *, const struct timeval *);
|
---|
331 | int setitimer(int, const struct itimerval *, struct itimerval *);
|
---|
332 | int settimeofday(const struct timeval *, const struct timezone *);
|
---|
333 | int utimes(const char *, const struct timeval *);
|
---|
334 |
|
---|
335 | /* bird - start: A nice SUN/HP/RTLinux extension. */
|
---|
336 | typedef signed long long hrtime_t;
|
---|
337 | hrtime_t gethrtime(void);
|
---|
338 | #define HRTIME_INFINITY (0x7fffffffffffffffLL)
|
---|
339 | /* bird - end. */
|
---|
340 |
|
---|
341 | __END_DECLS
|
---|
342 |
|
---|
343 | #endif /* !_KERNEL */
|
---|
344 |
|
---|
345 | #endif /* !_SYS_TIME_H_ */
|
---|