1:
2:
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 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: 47: 48:
49: struct timeval {
50: long tv_sec;
51: long tv_usec;
52: };
53:
54: #ifndef _TIMESPEC_DECLARED
55: #define _TIMESPEC_DECLARED
56: 57: 58:
59: struct timespec {
60: time_t tv_sec;
61: long tv_nsec;
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;
76: int tz_dsttime;
77: };
78: #define DST_NONE 0
79: #define DST_USA 1
80: #define DST_AUST 2
81: #define DST_WET 3
82: #define DST_MET 4
83: #define DST_EET 5
84: #define DST_CAN 6
85:
86:
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:
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:
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: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 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:
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:
224: bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL;
225: }
226:
227: 228: 229: 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;
237: struct timeval it_value;
238: };
239:
240: 241: 242: 243:
244: struct itimerspec {
245: struct timespec it_interval;
246: struct timespec it_value;
247: };
248:
249: 250: 251:
252: struct clockinfo {
253: int hz;
254: int tick;
255: int tickadj;
256: int stathz;
257: int profhz;
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
266: #define TIMER_ABSTIME 0x1
267:
268: #if defined(_KERNEL) || defined(_STANDALONE)
269: extern volatile time_t time_second;
270: extern volatile time_t time_uptime;
271:
272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 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: struct proc;
308: int clock_gettime(struct proc *, clockid_t, struct timespec *);
309:
310: int timespecfix(struct timespec *);
311: int itimerfix(struct timeval *);
312: int itimerdecr(struct itimerval *itp, int usec);
313: int settime(struct timespec *);
314: int ratecheck(struct timeval *, const struct timeval *);
315: int ppsratecheck(struct timeval *, int *, int);
316:
317: 318: 319:
320: struct clock_ymdhms {
321: u_short dt_year;
322: u_char dt_mon;
323: u_char dt_day;
324: u_char dt_wday;
325: u_char dt_hour;
326: u_char dt_min;
327: u_char dt_sec;
328: };
329:
330: time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
331: void clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
332: 333: 334:
335: #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
336: #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
337:
338:
339: #define SECDAY 86400L
340: #define SECYR (SECDAY * 365)
341:
342:
343: #define POSIX_BASE_YEAR 1970
344:
345: #else
346: #include <time.h>
347:
348: #if __BSD_VISIBLE || __XPG_VISIBLE
349: __BEGIN_DECLS
350: #if __BSD_VISIBLE
351: int adjtime(const struct timeval *, struct timeval *);
352: int adjfreq(const int64_t *, int64_t *);
353: #endif
354: #if __XPG_VISIBLE
355: int clock_getres(clockid_t, struct timespec *);
356: int clock_gettime(clockid_t, struct timespec *);
357: int clock_settime(clockid_t, const struct timespec *);
358: int futimes(int, const struct timeval *);
359: int getitimer(int, struct itimerval *);
360: int gettimeofday(struct timeval *, struct timezone *);
361: int setitimer(int, const struct itimerval *, struct itimerval *);
362: int settimeofday(const struct timeval *, const struct timezone *);
363: int utimes(const char *, const struct timeval *);
364: #endif
365: __END_DECLS
366: #endif
367:
368: #endif
369:
370: #endif