1 /* time.h -- An implementation of the standard Unix <sys/time.h> file.
2 Written by Geoffrey Noer <noer@cygnus.com>
3 Public domain; no rights reserved. */
4
5 /*-
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * Copyright (c) 1982, 1986, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)time.h 8.5 (Berkeley) 5/4/95
36 * $FreeBSD: head/sys/sys/time.h 346176 2019-04-13 04:46:35Z imp $
37 */
38
39 #ifndef _SYS_TIME_H_
40 #define _SYS_TIME_H_
41
42 #include <_ansi.h>
43 #include <sys/cdefs.h>
44 #include <sys/_timeval.h>
45 #include <sys/types.h>
46 #include <sys/timespec.h>
47
48 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
49 #include <sys/select.h>
50 #endif
51
52 struct timezone {
53 int tz_minuteswest; /* minutes west of Greenwich */
54 int tz_dsttime; /* type of dst correction */
55 };
56 #define DST_NONE 0 /* not on dst */
57 #define DST_USA 1 /* USA style dst */
58 #define DST_AUST 2 /* Australian style dst */
59 #define DST_WET 3 /* Western European dst */
60 #define DST_MET 4 /* Middle European dst */
61 #define DST_EET 5 /* Eastern European dst */
62 #define DST_CAN 6 /* Canada */
63
64 #if __BSD_VISIBLE
65 struct bintime {
66 time_t sec;
67 uint64_t frac;
68 };
69
70 static __inline void
bintime_addx(struct bintime * _bt,uint64_t _x)71 bintime_addx(struct bintime *_bt, uint64_t _x)
72 {
73 uint64_t _u;
74
75 _u = _bt->frac;
76 _bt->frac += _x;
77 if (_u > _bt->frac)
78 _bt->sec++;
79 }
80
81 static __inline void
bintime_add(struct bintime * _bt,const struct bintime * _bt2)82 bintime_add(struct bintime *_bt, const struct bintime *_bt2)
83 {
84 uint64_t _u;
85
86 _u = _bt->frac;
87 _bt->frac += _bt2->frac;
88 if (_u > _bt->frac)
89 _bt->sec++;
90 _bt->sec += _bt2->sec;
91 }
92
93 static __inline void
bintime_sub(struct bintime * _bt,const struct bintime * _bt2)94 bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
95 {
96 uint64_t _u;
97
98 _u = _bt->frac;
99 _bt->frac -= _bt2->frac;
100 if (_u < _bt->frac)
101 _bt->sec--;
102 _bt->sec -= _bt2->sec;
103 }
104
105 static __inline void
bintime_mul(struct bintime * _bt,u_int _x)106 bintime_mul(struct bintime *_bt, u_int _x)
107 {
108 uint64_t _p1, _p2;
109
110 _p1 = (_bt->frac & 0xffffffffull) * _x;
111 _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
112 _bt->sec *= _x;
113 _bt->sec += (_p2 >> 32);
114 _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
115 }
116
117 static __inline void
bintime_shift(struct bintime * _bt,int _exp)118 bintime_shift(struct bintime *_bt, int _exp)
119 {
120
121 if (_exp > 0) {
122 _bt->sec <<= _exp;
123 _bt->sec |= _bt->frac >> (64 - _exp);
124 _bt->frac <<= _exp;
125 } else if (_exp < 0) {
126 _bt->frac >>= -_exp;
127 _bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
128 _bt->sec >>= -_exp;
129 }
130 }
131
132 #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
133 #define bintime_isset(a) ((a)->sec || (a)->frac)
134 #define bintime_cmp(a, b, cmp) \
135 (((a)->sec == (b)->sec) ? \
136 ((a)->frac cmp (b)->frac) : \
137 ((a)->sec cmp (b)->sec))
138
139 #define SBT_1S ((sbintime_t)1 << 32)
140 #define SBT_1M (SBT_1S * 60)
141 #define SBT_1MS (SBT_1S / 1000)
142 #define SBT_1US (SBT_1S / 1000000)
143 #define SBT_1NS (SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
144 #define SBT_MAX 0x7fffffffffffffffLL
145
146 static __inline int
sbintime_getsec(sbintime_t _sbt)147 sbintime_getsec(sbintime_t _sbt)
148 {
149
150 return (_sbt >> 32);
151 }
152
153 static __inline sbintime_t
bttosbt(const struct bintime _bt)154 bttosbt(const struct bintime _bt)
155 {
156
157 return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
158 }
159
160 static __inline struct bintime
sbttobt(sbintime_t _sbt)161 sbttobt(sbintime_t _sbt)
162 {
163 struct bintime _bt;
164
165 _bt.sec = _sbt >> 32;
166 _bt.frac = _sbt << 32;
167 return (_bt);
168 }
169
170 /*
171 * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS results in
172 * large roundoff errors which sbttons() and nstosbt() avoid. Millisecond and
173 * microsecond functions are also provided for completeness.
174 *
175 * These functions return the smallest sbt larger or equal to the
176 * number of seconds requested so that sbttoX(Xtosbt(y)) == y. Unlike
177 * top of second computations below, which require that we tick at the
178 * top of second, these need to be rounded up so we do whatever for at
179 * least as long as requested.
180 *
181 * The naive computation we'd do is this
182 * ((unit * 2^64 / SIFACTOR) + 2^32-1) >> 32
183 * However, that overflows. Instead, we compute
184 * ((unit * 2^63 / SIFACTOR) + 2^31-1) >> 32
185 * and use pre-computed constants that are the ceil of the 2^63 / SIFACTOR
186 * term to ensure we are using exactly the right constant. We use the lesser
187 * evil of ull rather than a uint64_t cast to ensure we have well defined
188 * right shift semantics. With these changes, we get all the ns, us and ms
189 * conversions back and forth right.
190 */
191 static __inline int64_t
sbttons(sbintime_t _sbt)192 sbttons(sbintime_t _sbt)
193 {
194 uint64_t ns;
195
196 ns = _sbt;
197 if (ns >= SBT_1S)
198 ns = (ns >> 32) * 1000000000;
199 else
200 ns = 0;
201
202 return (ns + (1000000000 * (_sbt & 0xffffffffu) >> 32));
203 }
204
205 static __inline sbintime_t
nstosbt(int64_t _ns)206 nstosbt(int64_t _ns)
207 {
208 sbintime_t sb = 0;
209
210 if (_ns >= SBT_1S) {
211 sb = (_ns / 1000000000) * SBT_1S;
212 _ns = _ns % 1000000000;
213 }
214 /* 9223372037 = ceil(2^63 / 1000000000) */
215 sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
216 return (sb);
217 }
218
219 static __inline int64_t
sbttous(sbintime_t _sbt)220 sbttous(sbintime_t _sbt)
221 {
222
223 return ((1000000 * _sbt) >> 32);
224 }
225
226 static __inline sbintime_t
ustosbt(int64_t _us)227 ustosbt(int64_t _us)
228 {
229 sbintime_t sb = 0;
230
231 if (_us >= SBT_1S) {
232 sb = (_us / 1000000) * SBT_1S;
233 _us = _us % 1000000;
234 }
235 /* 9223372036855 = ceil(2^63 / 1000000) */
236 sb += ((_us * 9223372036855ull) + 0x7fffffff) >> 31;
237 return (sb);
238 }
239
240 static __inline int64_t
sbttoms(sbintime_t _sbt)241 sbttoms(sbintime_t _sbt)
242 {
243
244 return ((1000 * _sbt) >> 32);
245 }
246
247 static __inline sbintime_t
mstosbt(int64_t _ms)248 mstosbt(int64_t _ms)
249 {
250 sbintime_t sb = 0;
251
252 if (_ms >= SBT_1S) {
253 sb = (_ms / 1000) * SBT_1S;
254 _ms = _ms % 1000;
255 }
256 /* 9223372036854776 = ceil(2^63 / 1000) */
257 sb += ((_ms * 9223372036854776ull) + 0x7fffffff) >> 31;
258 return (sb);
259 }
260
261 /*-
262 * Background information:
263 *
264 * When converting between timestamps on parallel timescales of differing
265 * resolutions it is historical and scientific practice to round down rather
266 * than doing 4/5 rounding.
267 *
268 * The date changes at midnight, not at noon.
269 *
270 * Even at 15:59:59.999999999 it's not four'o'clock.
271 *
272 * time_second ticks after N.999999999 not after N.4999999999
273 */
274
275 static __inline void
bintime2timespec(const struct bintime * _bt,struct timespec * _ts)276 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
277 {
278
279 _ts->tv_sec = _bt->sec;
280 _ts->tv_nsec = ((uint64_t)1000000000 *
281 (uint32_t)(_bt->frac >> 32)) >> 32;
282 }
283
284 static __inline void
timespec2bintime(const struct timespec * _ts,struct bintime * _bt)285 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
286 {
287
288 _bt->sec = _ts->tv_sec;
289 /* 18446744073 = int(2^64 / 1000000000) */
290 _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
291 }
292
293 static __inline void
bintime2timeval(const struct bintime * _bt,struct timeval * _tv)294 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
295 {
296
297 _tv->tv_sec = _bt->sec;
298 _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
299 }
300
301 static __inline void
timeval2bintime(const struct timeval * _tv,struct bintime * _bt)302 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
303 {
304
305 _bt->sec = _tv->tv_sec;
306 /* 18446744073709 = int(2^64 / 1000000) */
307 _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
308 }
309
310 static __inline struct timespec
sbttots(sbintime_t _sbt)311 sbttots(sbintime_t _sbt)
312 {
313 struct timespec _ts;
314
315 _ts.tv_sec = _sbt >> 32;
316 _ts.tv_nsec = sbttons((uint32_t)_sbt);
317 return (_ts);
318 }
319
320 static __inline sbintime_t
tstosbt(struct timespec _ts)321 tstosbt(struct timespec _ts)
322 {
323
324 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
325 }
326
327 static __inline struct timeval
sbttotv(sbintime_t _sbt)328 sbttotv(sbintime_t _sbt)
329 {
330 struct timeval _tv;
331
332 _tv.tv_sec = _sbt >> 32;
333 _tv.tv_usec = sbttous((uint32_t)_sbt);
334 return (_tv);
335 }
336
337 static __inline sbintime_t
tvtosbt(struct timeval _tv)338 tvtosbt(struct timeval _tv)
339 {
340
341 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
342 }
343
344 /* Operations on timespecs */
345 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
346 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
347 #define timespeccmp(tvp, uvp, cmp) \
348 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
349 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
350 ((tvp)->tv_sec cmp (uvp)->tv_sec))
351
352 #define timespecadd(tsp, usp, vsp) \
353 do { \
354 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
355 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
356 if ((vsp)->tv_nsec >= 1000000000L) { \
357 (vsp)->tv_sec++; \
358 (vsp)->tv_nsec -= 1000000000L; \
359 } \
360 } while (0)
361 #define timespecsub(tsp, usp, vsp) \
362 do { \
363 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
364 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
365 if ((vsp)->tv_nsec < 0) { \
366 (vsp)->tv_sec--; \
367 (vsp)->tv_nsec += 1000000000L; \
368 } \
369 } while (0)
370
371 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
372
373 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
374 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
375 #define timercmp(tvp, uvp, cmp) \
376 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
377 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
378 ((tvp)->tv_sec cmp (uvp)->tv_sec))
379 #define timeradd(tvp, uvp, vvp) \
380 do { \
381 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
382 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
383 if ((vvp)->tv_usec >= 1000000) { \
384 (vvp)->tv_sec++; \
385 (vvp)->tv_usec -= 1000000; \
386 } \
387 } while (0)
388 #define timersub(tvp, uvp, vvp) \
389 do { \
390 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
391 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
392 if ((vvp)->tv_usec < 0) { \
393 (vvp)->tv_sec--; \
394 (vvp)->tv_usec += 1000000; \
395 } \
396 } while (0)
397 #endif
398 #endif /* __BSD_VISIBLE */
399
400 /*
401 * Names of the interval timers, and structure
402 * defining a timer setting.
403 */
404 #define ITIMER_REAL 0
405 #define ITIMER_VIRTUAL 1
406 #define ITIMER_PROF 2
407
408 struct itimerval {
409 struct timeval it_interval; /* timer interval */
410 struct timeval it_value; /* current value */
411 };
412
413 #ifndef _KERNEL
414 #include <time.h>
415
416 __BEGIN_DECLS
417 int utimes (const char *, const struct timeval [2]);
418
419 #if __BSD_VISIBLE
420 int adjtime (const struct timeval *, struct timeval *);
421 int futimes (int, const struct timeval [2]);
422 int lutimes (const char *, const struct timeval [2]);
423 int settimeofday (const struct timeval *, const struct timezone *);
424 #endif
425
426 #if __MISC_VISIBLE || __XSI_VISIBLE
427 int getitimer (int __which, struct itimerval *__value);
428 int setitimer (int __which, const struct itimerval *__restrict __value,
429 struct itimerval *__restrict __ovalue);
430 #endif
431
432 int gettimeofday (struct timeval *__restrict __p,
433 void *__restrict __tz);
434
435 #if __GNU_VISIBLE
436 int futimesat (int, const char *, const struct timeval [2]);
437 #endif
438
439 #ifdef _LIBC
440 int _gettimeofday (struct timeval *__p, void *__tz);
441 #endif
442
443 __END_DECLS
444
445 #endif /* !_KERNEL */
446 #include <machine/_time.h>
447
448 #endif /* !_SYS_TIME_H_ */
449