Line data Source code
1 : /* $OpenBSD: time.h,v 1.38 2018/05/28 18:05:42 guenther 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/select.h>
39 :
40 : #ifndef _TIMEVAL_DECLARED
41 : #define _TIMEVAL_DECLARED
42 : /*
43 : * Structure returned by gettimeofday(2) system call,
44 : * and used in other calls.
45 : */
46 : struct timeval {
47 : time_t tv_sec; /* seconds */
48 : suseconds_t tv_usec; /* and microseconds */
49 : };
50 : #endif
51 :
52 : #ifndef _TIMESPEC_DECLARED
53 : #define _TIMESPEC_DECLARED
54 : /*
55 : * Structure defined by POSIX.1b to be like a timeval.
56 : */
57 : struct timespec {
58 : time_t tv_sec; /* seconds */
59 : long tv_nsec; /* and nanoseconds */
60 : };
61 : #endif
62 :
63 : #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
64 : (ts)->tv_sec = (tv)->tv_sec; \
65 : (ts)->tv_nsec = (tv)->tv_usec * 1000; \
66 : } while (0)
67 : #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
68 : (tv)->tv_sec = (ts)->tv_sec; \
69 : (tv)->tv_usec = (ts)->tv_nsec / 1000; \
70 : } while (0)
71 :
72 : struct timezone {
73 : int tz_minuteswest; /* minutes west of Greenwich */
74 : int tz_dsttime; /* type of dst correction */
75 : };
76 : #define DST_NONE 0 /* not on dst */
77 : #define DST_USA 1 /* USA style dst */
78 : #define DST_AUST 2 /* Australian style dst */
79 : #define DST_WET 3 /* Western European dst */
80 : #define DST_MET 4 /* Middle European dst */
81 : #define DST_EET 5 /* Eastern European dst */
82 : #define DST_CAN 6 /* Canada */
83 :
84 : /* Operations on timevals. */
85 : #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
86 : #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
87 : #define timercmp(tvp, uvp, cmp) \
88 : (((tvp)->tv_sec == (uvp)->tv_sec) ? \
89 : ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
90 : ((tvp)->tv_sec cmp (uvp)->tv_sec))
91 : #define timeradd(tvp, uvp, vvp) \
92 : do { \
93 : (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
94 : (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
95 : if ((vvp)->tv_usec >= 1000000) { \
96 : (vvp)->tv_sec++; \
97 : (vvp)->tv_usec -= 1000000; \
98 : } \
99 : } while (0)
100 : #define timersub(tvp, uvp, vvp) \
101 : do { \
102 : (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
103 : (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
104 : if ((vvp)->tv_usec < 0) { \
105 : (vvp)->tv_sec--; \
106 : (vvp)->tv_usec += 1000000; \
107 : } \
108 : } while (0)
109 :
110 : /* Operations on timespecs. */
111 : #define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
112 : #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
113 : #define timespeccmp(tsp, usp, cmp) \
114 : (((tsp)->tv_sec == (usp)->tv_sec) ? \
115 : ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
116 : ((tsp)->tv_sec cmp (usp)->tv_sec))
117 : #define timespecadd(tsp, usp, vsp) \
118 : do { \
119 : (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
120 : (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
121 : if ((vsp)->tv_nsec >= 1000000000L) { \
122 : (vsp)->tv_sec++; \
123 : (vsp)->tv_nsec -= 1000000000L; \
124 : } \
125 : } while (0)
126 : #define timespecsub(tsp, usp, vsp) \
127 : do { \
128 : (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
129 : (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
130 : if ((vsp)->tv_nsec < 0) { \
131 : (vsp)->tv_sec--; \
132 : (vsp)->tv_nsec += 1000000000L; \
133 : } \
134 : } while (0)
135 :
136 : /*
137 : * Names of the interval timers, and structure
138 : * defining a timer setting.
139 : */
140 : #define ITIMER_REAL 0
141 : #define ITIMER_VIRTUAL 1
142 : #define ITIMER_PROF 2
143 :
144 : struct itimerval {
145 : struct timeval it_interval; /* timer interval */
146 : struct timeval it_value; /* current value */
147 : };
148 :
149 : #if __BSD_VISIBLE
150 : /*
151 : * clock information structure for sysctl({CTL_KERN, KERN_CLOCKRATE})
152 : */
153 : struct clockinfo {
154 : int hz; /* clock frequency */
155 : int tick; /* micro-seconds per hz tick */
156 : int tickadj; /* clock skew rate for adjtime() */
157 : int stathz; /* statistics clock frequency */
158 : int profhz; /* profiling clock frequency */
159 : };
160 : #endif /* __BSD_VISIBLE */
161 :
162 : #if defined(_KERNEL) || defined(_STANDALONE)
163 : #include <sys/_time.h>
164 :
165 : /* Time expressed as seconds and fractions of a second + operations on it. */
166 : struct bintime {
167 : time_t sec;
168 : uint64_t frac;
169 : };
170 :
171 : static __inline void
172 0 : bintime_addx(struct bintime *bt, uint64_t x)
173 : {
174 : uint64_t u;
175 :
176 0 : u = bt->frac;
177 0 : bt->frac += x;
178 0 : if (u > bt->frac)
179 0 : bt->sec++;
180 0 : }
181 :
182 : static __inline void
183 0 : bintime_add(struct bintime *bt, const struct bintime *bt2)
184 : {
185 : uint64_t u;
186 :
187 0 : u = bt->frac;
188 0 : bt->frac += bt2->frac;
189 0 : if (u > bt->frac)
190 0 : bt->sec++;
191 0 : bt->sec += bt2->sec;
192 0 : }
193 :
194 : static __inline void
195 0 : bintime_sub(struct bintime *bt, const struct bintime *bt2)
196 : {
197 : uint64_t u;
198 :
199 0 : u = bt->frac;
200 0 : bt->frac -= bt2->frac;
201 0 : if (u < bt->frac)
202 0 : bt->sec--;
203 0 : bt->sec -= bt2->sec;
204 0 : }
205 :
206 : /*-
207 : * Background information:
208 : *
209 : * When converting between timestamps on parallel timescales of differing
210 : * resolutions it is historical and scientific practice to round down rather
211 : * than doing 4/5 rounding.
212 : *
213 : * The date changes at midnight, not at noon.
214 : *
215 : * Even at 15:59:59.999999999 it's not four'o'clock.
216 : *
217 : * time_second ticks after N.999999999 not after N.4999999999
218 : */
219 :
220 : static __inline void
221 0 : bintime2timespec(const struct bintime *bt, struct timespec *ts)
222 : {
223 :
224 0 : ts->tv_sec = bt->sec;
225 0 : ts->tv_nsec = (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
226 0 : }
227 :
228 : static __inline void
229 0 : timespec2bintime(const struct timespec *ts, struct bintime *bt)
230 : {
231 :
232 0 : bt->sec = ts->tv_sec;
233 : /* 18446744073 = int(2^64 / 1000000000) */
234 0 : bt->frac = (uint64_t)ts->tv_nsec * (uint64_t)18446744073ULL;
235 0 : }
236 :
237 : static __inline void
238 0 : bintime2timeval(const struct bintime *bt, struct timeval *tv)
239 : {
240 :
241 0 : tv->tv_sec = bt->sec;
242 0 : tv->tv_usec = (long)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
243 0 : }
244 :
245 : static __inline void
246 : timeval2bintime(const struct timeval *tv, struct bintime *bt)
247 : {
248 :
249 : bt->sec = (time_t)tv->tv_sec;
250 : /* 18446744073709 = int(2^64 / 1000000) */
251 : bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL;
252 : }
253 :
254 : extern volatile time_t time_second; /* Seconds since epoch, wall time. */
255 : extern volatile time_t time_uptime; /* Seconds since reboot. */
256 :
257 : /*
258 : * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
259 : *
260 : * Functions without the "get" prefix returns the best timestamp
261 : * we can produce in the given format.
262 : *
263 : * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
264 : * "nano" == struct timespec == seconds + nanoseconds.
265 : * "micro" == struct timeval == seconds + microseconds.
266 : *
267 : * Functions containing "up" returns time relative to boot and
268 : * should be used for calculating time intervals.
269 : *
270 : * Functions without "up" returns GMT time.
271 : *
272 : * Functions with the "get" prefix returns a less precise result
273 : * much faster than the functions without "get" prefix and should
274 : * be used where a precision of 10 msec is acceptable or where
275 : * performance is priority. (NB: "precision", _not_ "resolution" !)
276 : */
277 :
278 : void bintime(struct bintime *);
279 : void nanotime(struct timespec *);
280 : void microtime(struct timeval *);
281 :
282 : void getnanotime(struct timespec *);
283 : void getmicrotime(struct timeval *);
284 :
285 : void binuptime(struct bintime *);
286 : void nanouptime(struct timespec *);
287 : void microuptime(struct timeval *);
288 :
289 : void getnanouptime(struct timespec *);
290 : void getmicrouptime(struct timeval *);
291 :
292 : struct proc;
293 : int clock_gettime(struct proc *, clockid_t, struct timespec *);
294 :
295 : int timespecfix(struct timespec *);
296 : int itimerfix(struct timeval *);
297 : int itimerdecr(struct itimerval *itp, int usec);
298 : void itimerround(struct timeval *);
299 : int settime(const struct timespec *);
300 : int ratecheck(struct timeval *, const struct timeval *);
301 : int ppsratecheck(struct timeval *, int *, int);
302 :
303 : /*
304 : * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
305 : */
306 : struct clock_ymdhms {
307 : u_short dt_year;
308 : u_char dt_mon;
309 : u_char dt_day;
310 : u_char dt_wday; /* Day of week */
311 : u_char dt_hour;
312 : u_char dt_min;
313 : u_char dt_sec;
314 : };
315 :
316 : time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
317 : void clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
318 : /*
319 : * BCD to decimal and decimal to BCD.
320 : */
321 : #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
322 : #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
323 :
324 : /* Some handy constants. */
325 : #define SECDAY 86400L
326 : #define SECYR (SECDAY * 365)
327 :
328 : /* Traditional POSIX base year */
329 : #define POSIX_BASE_YEAR 1970
330 :
331 : #else /* !_KERNEL */
332 : #include <time.h>
333 :
334 : #if __BSD_VISIBLE || __XPG_VISIBLE
335 : __BEGIN_DECLS
336 : #if __BSD_VISIBLE
337 : int adjtime(const struct timeval *, struct timeval *);
338 : int adjfreq(const int64_t *, int64_t *);
339 : #endif
340 : #if __XPG_VISIBLE
341 : int futimes(int, const struct timeval *);
342 : int getitimer(int, struct itimerval *);
343 : int gettimeofday(struct timeval *, struct timezone *);
344 : int setitimer(int, const struct itimerval *, struct itimerval *);
345 : int settimeofday(const struct timeval *, const struct timezone *);
346 : int utimes(const char *, const struct timeval *);
347 : #endif /* __XPG_VISIBLE */
348 : __END_DECLS
349 : #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
350 :
351 : #endif /* !_KERNEL */
352 :
353 : #endif /* !_SYS_TIME_H_ */
|