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. */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 1982, 1986, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)time.h 8.5 (Berkeley) 5/4/95
36 * $FreeBSD: head/sys/sys/time.h 346176 2019-04-13 04:46:35Z imp $
43 #include <sys/cdefs.h>
44 #include <sys/_timeval.h>
45 #include <sys/types.h>
46 #include <sys/timespec.h>
48 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
49 #include <sys/select.h>
53 int tz_minuteswest
; /* minutes west of Greenwich */
54 int tz_dsttime
; /* type of dst correction */
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 */
71 bintime_addx(struct bintime
*_bt
, uint64_t _x
)
82 bintime_add(struct bintime
*_bt
, const struct bintime
*_bt2
)
87 _bt
->frac
+= _bt2
->frac
;
90 _bt
->sec
+= _bt2
->sec
;
94 bintime_sub(struct bintime
*_bt
, const struct bintime
*_bt2
)
99 _bt
->frac
-= _bt2
->frac
;
102 _bt
->sec
-= _bt2
->sec
;
106 bintime_mul(struct bintime
*_bt
, u_int _x
)
110 _p1
= (_bt
->frac
& 0xffffffffull
) * _x
;
111 _p2
= (_bt
->frac
>> 32) * _x
+ (_p1
>> 32);
113 _bt
->sec
+= (_p2
>> 32);
114 _bt
->frac
= (_p2
<< 32) | (_p1
& 0xffffffffull
);
118 bintime_shift(struct bintime
*_bt
, int _exp
)
123 _bt
->sec
|= _bt
->frac
>> (64 - _exp
);
125 } else if (_exp
< 0) {
127 _bt
->frac
|= (uint64_t)_bt
->sec
<< (64 + _exp
);
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))
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
147 sbintime_getsec(sbintime_t _sbt
)
153 static __inline sbintime_t
154 bttosbt(const struct bintime _bt
)
157 return (((sbintime_t
)_bt
.sec
<< 32) + (_bt
.frac
>> 32));
160 static __inline
struct bintime
161 sbttobt(sbintime_t _sbt
)
165 _bt
.sec
= _sbt
>> 32;
166 _bt
.frac
= _sbt
<< 32;
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.
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.
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.
191 static __inline
int64_t
192 sbttons(sbintime_t _sbt
)
198 ns
= (ns
>> 32) * 1000000000;
202 return (ns
+ (1000000000 * (_sbt
& 0xffffffffu
) >> 32));
205 static __inline sbintime_t
211 sb
= (_ns
/ 1000000000) * SBT_1S
;
212 _ns
= _ns
% 1000000000;
214 /* 9223372037 = ceil(2^63 / 1000000000) */
215 sb
+= ((_ns
* 9223372037ull) + 0x7fffffff) >> 31;
219 static __inline
int64_t
220 sbttous(sbintime_t _sbt
)
223 return ((1000000 * _sbt
) >> 32);
226 static __inline sbintime_t
232 sb
= (_us
/ 1000000) * SBT_1S
;
235 /* 9223372036855 = ceil(2^63 / 1000000) */
236 sb
+= ((_us
* 9223372036855ull) + 0x7fffffff) >> 31;
240 static __inline
int64_t
241 sbttoms(sbintime_t _sbt
)
244 return ((1000 * _sbt
) >> 32);
247 static __inline sbintime_t
253 sb
= (_ms
/ 1000) * SBT_1S
;
256 /* 9223372036854776 = ceil(2^63 / 1000) */
257 sb
+= ((_ms
* 9223372036854776ull) + 0x7fffffff) >> 31;
262 * Background information:
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.
268 * The date changes at midnight, not at noon.
270 * Even at 15:59:59.999999999 it's not four'o'clock.
272 * time_second ticks after N.999999999 not after N.4999999999
276 bintime2timespec(const struct bintime
*_bt
, struct timespec
*_ts
)
279 _ts
->tv_sec
= _bt
->sec
;
280 _ts
->tv_nsec
= ((uint64_t)1000000000 *
281 (uint32_t)(_bt
->frac
>> 32)) >> 32;
285 timespec2bintime(const struct timespec
*_ts
, struct bintime
*_bt
)
288 _bt
->sec
= _ts
->tv_sec
;
289 /* 18446744073 = int(2^64 / 1000000000) */
290 _bt
->frac
= _ts
->tv_nsec
* (uint64_t)18446744073LL;
294 bintime2timeval(const struct bintime
*_bt
, struct timeval
*_tv
)
297 _tv
->tv_sec
= _bt
->sec
;
298 _tv
->tv_usec
= ((uint64_t)1000000 * (uint32_t)(_bt
->frac
>> 32)) >> 32;
302 timeval2bintime(const struct timeval
*_tv
, struct bintime
*_bt
)
305 _bt
->sec
= _tv
->tv_sec
;
306 /* 18446744073709 = int(2^64 / 1000000) */
307 _bt
->frac
= _tv
->tv_usec
* (uint64_t)18446744073709LL;
310 static __inline
struct timespec
311 sbttots(sbintime_t _sbt
)
315 _ts
.tv_sec
= _sbt
>> 32;
316 _ts
.tv_nsec
= sbttons((uint32_t)_sbt
);
320 static __inline sbintime_t
321 tstosbt(struct timespec _ts
)
324 return (((sbintime_t
)_ts
.tv_sec
<< 32) + nstosbt(_ts
.tv_nsec
));
327 static __inline
struct timeval
328 sbttotv(sbintime_t _sbt
)
332 _tv
.tv_sec
= _sbt
>> 32;
333 _tv
.tv_usec
= sbttous((uint32_t)_sbt
);
337 static __inline sbintime_t
338 tvtosbt(struct timeval _tv
)
341 return (((sbintime_t
)_tv
.tv_sec
<< 32) + ustosbt(_tv
.tv_usec
));
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))
352 #define timespecadd(tsp, usp, vsp) \
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) { \
358 (vsp)->tv_nsec -= 1000000000L; \
361 #define timespecsub(tsp, usp, vsp) \
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) { \
367 (vsp)->tv_nsec += 1000000000L; \
371 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
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) \
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) { \
385 (vvp)->tv_usec -= 1000000; \
388 #define timersub(tvp, uvp, vvp) \
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) { \
394 (vvp)->tv_usec += 1000000; \
398 #endif /* __BSD_VISIBLE */
401 * Names of the interval timers, and structure
402 * defining a timer setting.
404 #define ITIMER_REAL 0
405 #define ITIMER_VIRTUAL 1
406 #define ITIMER_PROF 2
409 struct timeval it_interval
; /* timer interval */
410 struct timeval it_value
; /* current value */
417 int utimes (const char *, const struct timeval
[2]);
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
*);
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
);
432 int gettimeofday (struct timeval
*__restrict __p
,
433 void *__restrict __tz
);
436 int futimesat (int, const char *, const struct timeval
[2]);
440 int _gettimeofday (struct timeval
*__p
, void *__tz
);
445 #endif /* !_KERNEL */
446 #include <machine/_time.h>
448 #endif /* !_SYS_TIME_H_ */