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