4 #include <sys/featuretest.h>
8 * Structure returned by gettimeofday(2) system call,
9 * and used in other calls.
12 time_t tv_sec
; /* seconds */
13 suseconds_t tv_usec
; /* and microseconds */
17 * Structure defined by POSIX.1b to be like a timeval.
20 time_t tv_sec
; /* seconds */
21 long tv_nsec
; /* and nanoseconds */
24 #if defined(_NETBSD_SOURCE)
25 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
26 (ts)->tv_sec = (tv)->tv_sec; \
27 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
28 } while (/*CONSTCOND*/0)
29 #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
30 (tv)->tv_sec = (ts)->tv_sec; \
31 (tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000; \
32 } while (/*CONSTCOND*/0)
35 * Note: timezone is obsolete. All timezone handling is now in
36 * userland. Its just here for back compatibility.
39 int tz_minuteswest
; /* minutes west of Greenwich */
40 int tz_dsttime
; /* type of dst correction */
43 /* Operations on timevals. */
44 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0L
45 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
46 #define timercmp(tvp, uvp, cmp) \
47 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
48 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
49 ((tvp)->tv_sec cmp (uvp)->tv_sec))
50 #define timeradd(tvp, uvp, vvp) \
52 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
53 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
54 if ((vvp)->tv_usec >= 1000000) { \
56 (vvp)->tv_usec -= 1000000; \
58 } while (/* CONSTCOND */ 0)
59 #define timersub(tvp, uvp, vvp) \
61 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
62 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
63 if ((vvp)->tv_usec < 0) { \
65 (vvp)->tv_usec += 1000000; \
67 } while (/* CONSTCOND */ 0)
71 * hide bintime for _STANDALONE because this header is used for hpcboot.exe,
72 * which is built with compilers which don't recognize LL suffix.
73 * http://mail-index.NetBSD.org/tech-userlevel/2008/02/27/msg000181.html
75 #if !defined(_STANDALONE)
82 bintime_addx(struct bintime
*bt
, uint64_t x
)
93 bintime_add(struct bintime
*bt
, const struct bintime
*bt2
)
98 bt
->frac
+= bt2
->frac
;
105 bintime_sub(struct bintime
*bt
, const struct bintime
*bt2
)
110 bt
->frac
-= bt2
->frac
;
117 * Background information:
119 * When converting between timestamps on parallel timescales of differing
120 * resolutions it is historical and scientific practice to round down rather
121 * than doing 4/5 rounding.
123 * The date changes at midnight, not at noon.
125 * Even at 15:59:59.999999999 it's not four'o'clock.
127 * time_second ticks after N.999999999 not after N.4999999999
131 bintime2timespec(const struct bintime
*bt
, struct timespec
*ts
)
134 ts
->tv_sec
= bt
->sec
;
136 (long)(((uint64_t)1000000000 * (uint32_t)(bt
->frac
>> 32)) >> 32);
140 timespec2bintime(const struct timespec
*ts
, struct bintime
*bt
)
143 bt
->sec
= ts
->tv_sec
;
144 /* 18446744073 = int(2^64 / 1000000000) */
145 bt
->frac
= ts
->tv_nsec
* (uint64_t)18446744073LL;
149 bintime2timeval(const struct bintime
*bt
, struct timeval
*tv
)
152 tv
->tv_sec
= bt
->sec
;
154 (suseconds_t
)(((uint64_t)1000000 * (uint32_t)(bt
->frac
>> 32)) >> 32);
158 timeval2bintime(const struct timeval
*tv
, struct bintime
*bt
)
161 bt
->sec
= tv
->tv_sec
;
162 /* 18446744073709 = int(2^64 / 1000000) */
163 bt
->frac
= tv
->tv_usec
* (uint64_t)18446744073709LL;
165 #endif /* !defined(_STANDALONE) */
167 /* Operations on timespecs. */
168 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
169 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
170 #define timespeccmp(tsp, usp, cmp) \
171 (((tsp)->tv_sec == (usp)->tv_sec) ? \
172 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
173 ((tsp)->tv_sec cmp (usp)->tv_sec))
174 #define timespecadd(tsp, usp, vsp) \
176 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
177 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
178 if ((vsp)->tv_nsec >= 1000000000L) { \
180 (vsp)->tv_nsec -= 1000000000L; \
182 } while (/* CONSTCOND */ 0)
183 #define timespecsub(tsp, usp, vsp) \
185 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
186 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
187 if ((vsp)->tv_nsec < 0) { \
189 (vsp)->tv_nsec += 1000000000L; \
191 } while (/* CONSTCOND */ 0)
192 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
193 #endif /* _NETBSD_SOURCE */
196 * Names of the interval timers, and structure
197 * defining a timer setting.
199 #define ITIMER_REAL 0
200 #define ITIMER_VIRTUAL 1
201 #define ITIMER_PROF 2
204 struct timeval it_interval
; /* timer interval */
205 struct timeval it_value
; /* current value */
209 * Structure defined by POSIX.1b to be like a itimerval, but with
210 * timespecs. Used in the timer_*() system calls.
213 struct timespec it_interval
;
214 struct timespec it_value
;
218 #define CLOCK_REALTIME 0
219 #define CLOCK_VIRTUAL 1
221 #define CLOCK_MONOTONIC 3
224 #if defined(_NETBSD_SOURCE)
225 #define TIMER_RELTIME 0x0 /* relative timer */
227 #define TIMER_ABSTIME 0x1 /* absolute timer */
230 #include <sys/timevar.h>
233 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
234 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
235 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
236 #include <sys/select.h>
239 #include <sys/cdefs.h>
243 #ifndef __LIBC12_SOURCE__
244 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
245 defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
246 int getitimer(int, struct itimerval
*) __RENAME(__getitimer50
);
247 int gettimeofday(struct timeval
* __restrict
, void *__restrict
);
248 int setitimer(int, const struct itimerval
* __restrict
,
249 struct itimerval
* __restrict
) __RENAME(__setitimer50
);
250 #endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
252 #if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
254 int adjtime(const struct timeval
*, struct timeval
*) __RENAME(__adjtime50
);
255 int futimes(int, const struct timeval
[2]) __RENAME(__futimes50
);
256 int lutimes(const char *, const struct timeval
[2]) __RENAME(__lutimes50
);
257 #endif /* !__minix */
258 int settimeofday(const struct timeval
* __restrict
,
259 const void *__restrict
) __RENAME(__settimeofday50
);
260 #endif /* _NETBSD_SOURCE */
261 #endif /* __LIBC12_SOURCE__ */
264 #endif /* !_STANDALONE */
265 #endif /* !_KERNEL */
266 #endif /* !_SYS_TIME_H_ */