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 <sys/cdefs.h>
43 #include <sys/_types.h>
44 #include <sys/_timeval.h>
45 #include <sys/_timespec.h>
46 #include <sys/_select.h>
47
48 _BEGIN_STD_C
49
50 #ifndef _SUSECONDS_T_DECLARED
51 typedef __suseconds_t suseconds_t;
52 #define _SUSECONDS_T_DECLARED
53 #endif
54
55 /*
56 * Names of the interval timers, and structure
57 * defining a timer setting.
58 */
59 #define ITIMER_REAL 0
60 #define ITIMER_VIRTUAL 1
61 #define ITIMER_PROF 2
62
63 struct itimerval {
64 struct timeval it_interval; /* timer interval */
65 struct timeval it_value; /* current value */
66 };
67
68 int getitimer (int __which, struct itimerval *__value);
69 int gettimeofday (struct timeval *__restrict __p,
70 void *__restrict __tz);
71 int setitimer (int __which, const struct itimerval *__restrict __value,
72 struct itimerval *__restrict __ovalue);
73 int utimes (const char *, const struct timeval [2]);
74
75 #if __BSD_VISIBLE
76
77 struct timezone {
78 int tz_minuteswest; /* minutes west of Greenwich */
79 int tz_dsttime; /* type of dst correction */
80 };
81
82 int adjtime (const struct timeval *, struct timeval *);
83 int futimes (int, const struct timeval [2]);
84 int lutimes (const char *, const struct timeval [2]);
85 int settimeofday (const struct timeval *, const struct timezone *);
86
87 #define DST_NONE 0 /* not on dst */
88 #define DST_USA 1 /* USA style dst */
89 #define DST_AUST 2 /* Australian style dst */
90 #define DST_WET 3 /* Western European dst */
91 #define DST_MET 4 /* Middle European dst */
92 #define DST_EET 5 /* Eastern European dst */
93 #define DST_CAN 6 /* Canada */
94
95 #ifndef _SBINTIME_T_DECLARED
96 typedef __int64_t sbintime_t;
97 #define _SBINTIME_T_DECLARED
98 #endif
99
100 struct bintime {
101 time_t sec;
102 __uint64_t frac;
103 };
104
105 static __inline void
bintime_addx(struct bintime * _bt,__uint64_t _x)106 bintime_addx(struct bintime *_bt, __uint64_t _x)
107 {
108 __uint64_t _u;
109
110 _u = _bt->frac;
111 _bt->frac += _x;
112 if (_u > _bt->frac)
113 _bt->sec++;
114 }
115
116 static __inline void
bintime_add(struct bintime * _bt,const struct bintime * _bt2)117 bintime_add(struct bintime *_bt, const struct bintime *_bt2)
118 {
119 __uint64_t _u;
120
121 _u = _bt->frac;
122 _bt->frac += _bt2->frac;
123 if (_u > _bt->frac)
124 _bt->sec++;
125 _bt->sec += _bt2->sec;
126 }
127
128 static __inline void
bintime_sub(struct bintime * _bt,const struct bintime * _bt2)129 bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
130 {
131 __uint64_t _u;
132
133 _u = _bt->frac;
134 _bt->frac -= _bt2->frac;
135 if (_u < _bt->frac)
136 _bt->sec--;
137 _bt->sec -= _bt2->sec;
138 }
139
140 static __inline void
bintime_mul(struct bintime * _bt,unsigned int _x)141 bintime_mul(struct bintime *_bt, unsigned int _x)
142 {
143 __uint64_t _p1, _p2;
144
145 _p1 = (_bt->frac & 0xffffffffull) * _x;
146 _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
147 _bt->sec *= _x;
148 _bt->sec += (_p2 >> 32);
149 _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
150 }
151
152 static __inline void
bintime_shift(struct bintime * _bt,int _exp)153 bintime_shift(struct bintime *_bt, int _exp)
154 {
155
156 if (_exp > 0) {
157 _bt->sec <<= _exp;
158 _bt->sec |= _bt->frac >> (64 - _exp);
159 _bt->frac <<= _exp;
160 } else if (_exp < 0) {
161 _bt->frac >>= -_exp;
162 _bt->frac |= (__uint64_t)_bt->sec << (64 + _exp);
163 _bt->sec >>= -_exp;
164 }
165 }
166
167 #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
168 #define bintime_isset(a) ((a)->sec || (a)->frac)
169 #define bintime_cmp(a, b, cmp) \
170 (((a)->sec == (b)->sec) ? \
171 ((a)->frac cmp (b)->frac) : \
172 ((a)->sec cmp (b)->sec))
173
174 #define SBT_1S ((sbintime_t)1 << 32)
175 #define SBT_1M (SBT_1S * 60)
176 #define SBT_1MS (SBT_1S / 1000)
177 #define SBT_1US (SBT_1S / 1000000)
178 #define SBT_1NS (SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
179 #define SBT_MAX 0x7fffffffffffffffLL
180
181 static __inline int
sbintime_getsec(sbintime_t _sbt)182 sbintime_getsec(sbintime_t _sbt)
183 {
184
185 return (_sbt >> 32);
186 }
187
188 static __inline sbintime_t
bttosbt(const struct bintime _bt)189 bttosbt(const struct bintime _bt)
190 {
191
192 return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
193 }
194
195 static __inline struct bintime
sbttobt(sbintime_t _sbt)196 sbttobt(sbintime_t _sbt)
197 {
198 struct bintime _bt;
199
200 _bt.sec = _sbt >> 32;
201 _bt.frac = _sbt << 32;
202 return (_bt);
203 }
204
205 /*
206 * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS results in
207 * large roundoff errors which sbttons() and nstosbt() avoid. Millisecond and
208 * microsecond functions are also provided for completeness.
209 *
210 * These functions return the smallest sbt larger or equal to the
211 * number of seconds requested so that sbttoX(Xtosbt(y)) == y. Unlike
212 * top of second computations below, which require that we tick at the
213 * top of second, these need to be rounded up so we do whatever for at
214 * least as long as requested.
215 *
216 * The naive computation we'd do is this
217 * ((unit * 2^64 / SIFACTOR) + 2^32-1) >> 32
218 * However, that overflows. Instead, we compute
219 * ((unit * 2^63 / SIFACTOR) + 2^31-1) >> 32
220 * and use pre-computed constants that are the ceil of the 2^63 / SIFACTOR
221 * term to ensure we are using exactly the right constant. We use the lesser
222 * evil of ull rather than a __uint64_t cast to ensure we have well defined
223 * right shift semantics. With these changes, we get all the ns, us and ms
224 * conversions back and forth right.
225 */
226 static __inline __int64_t
sbttons(sbintime_t _sbt)227 sbttons(sbintime_t _sbt)
228 {
229 __uint64_t ns;
230
231 ns = _sbt;
232 if (ns >= SBT_1S)
233 ns = (ns >> 32) * 1000000000;
234 else
235 ns = 0;
236
237 return (ns + (1000000000 * (_sbt & 0xffffffffu) >> 32));
238 }
239
240 static __inline sbintime_t
nstosbt(__int64_t _ns)241 nstosbt(__int64_t _ns)
242 {
243 sbintime_t sb = 0;
244
245 if (_ns >= SBT_1S) {
246 sb = (_ns / 1000000000) * SBT_1S;
247 _ns = _ns % 1000000000;
248 }
249 /* 9223372037 = ceil(2^63 / 1000000000) */
250 sb += ((_ns * 9223372037ull) + 0x7fffffff) >> 31;
251 return (sb);
252 }
253
254 static __inline __int64_t
sbttous(sbintime_t _sbt)255 sbttous(sbintime_t _sbt)
256 {
257
258 return ((1000000 * _sbt) >> 32);
259 }
260
261 static __inline sbintime_t
ustosbt(__int64_t _us)262 ustosbt(__int64_t _us)
263 {
264 sbintime_t sb = 0;
265
266 if (_us >= SBT_1S) {
267 sb = (_us / 1000000) * SBT_1S;
268 _us = _us % 1000000;
269 }
270 /* 9223372036855 = ceil(2^63 / 1000000) */
271 sb += ((_us * 9223372036855ull) + 0x7fffffff) >> 31;
272 return (sb);
273 }
274
275 static __inline __int64_t
sbttoms(sbintime_t _sbt)276 sbttoms(sbintime_t _sbt)
277 {
278
279 return ((1000 * _sbt) >> 32);
280 }
281
282 static __inline sbintime_t
mstosbt(__int64_t _ms)283 mstosbt(__int64_t _ms)
284 {
285 sbintime_t sb = 0;
286
287 if (_ms >= SBT_1S) {
288 sb = (_ms / 1000) * SBT_1S;
289 _ms = _ms % 1000;
290 }
291 /* 9223372036854776 = ceil(2^63 / 1000) */
292 sb += ((_ms * 9223372036854776ull) + 0x7fffffff) >> 31;
293 return (sb);
294 }
295
296 /*-
297 * Background information:
298 *
299 * When converting between timestamps on parallel timescales of differing
300 * resolutions it is historical and scientific practice to round down rather
301 * than doing 4/5 rounding.
302 *
303 * The date changes at midnight, not at noon.
304 *
305 * Even at 15:59:59.999999999 it's not four'o'clock.
306 *
307 * time_second ticks after N.999999999 not after N.4999999999
308 */
309
310 static __inline void
bintime2timespec(const struct bintime * _bt,struct timespec * _ts)311 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
312 {
313
314 _ts->tv_sec = _bt->sec;
315 _ts->tv_nsec = ((__uint64_t)1000000000 *
316 (__uint32_t)(_bt->frac >> 32)) >> 32;
317 }
318
319 static __inline void
timespec2bintime(const struct timespec * _ts,struct bintime * _bt)320 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
321 {
322
323 _bt->sec = _ts->tv_sec;
324 /* 18446744073 = int(2^64 / 1000000000) */
325 _bt->frac = _ts->tv_nsec * (__uint64_t)18446744073LL;
326 }
327
328 static __inline void
bintime2timeval(const struct bintime * _bt,struct timeval * _tv)329 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
330 {
331
332 _tv->tv_sec = _bt->sec;
333 _tv->tv_usec = ((__uint64_t)1000000 * (__uint32_t)(_bt->frac >> 32)) >> 32;
334 }
335
336 static __inline void
timeval2bintime(const struct timeval * _tv,struct bintime * _bt)337 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
338 {
339
340 _bt->sec = _tv->tv_sec;
341 /* 18446744073709 = int(2^64 / 1000000) */
342 _bt->frac = _tv->tv_usec * (__uint64_t)18446744073709LL;
343 }
344
345 static __inline struct timespec
sbttots(sbintime_t _sbt)346 sbttots(sbintime_t _sbt)
347 {
348 struct timespec _ts;
349
350 _ts.tv_sec = _sbt >> 32;
351 _ts.tv_nsec = sbttons((__uint32_t)_sbt);
352 return (_ts);
353 }
354
355 static __inline sbintime_t
tstosbt(struct timespec _ts)356 tstosbt(struct timespec _ts)
357 {
358
359 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
360 }
361
362 static __inline struct timeval
sbttotv(sbintime_t _sbt)363 sbttotv(sbintime_t _sbt)
364 {
365 struct timeval _tv;
366
367 _tv.tv_sec = _sbt >> 32;
368 _tv.tv_usec = sbttous((__uint32_t)_sbt);
369 return (_tv);
370 }
371
372 static __inline sbintime_t
tvtosbt(struct timeval _tv)373 tvtosbt(struct timeval _tv)
374 {
375
376 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
377 }
378
379 /* Operations on timespecs */
380 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
381 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
382 #define timespeccmp(tvp, uvp, cmp) \
383 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
384 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
385 ((tvp)->tv_sec cmp (uvp)->tv_sec))
386
387 #define timespecadd(tsp, usp, vsp) \
388 do { \
389 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
390 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
391 if ((vsp)->tv_nsec >= 1000000000L) { \
392 (vsp)->tv_sec++; \
393 (vsp)->tv_nsec -= 1000000000L; \
394 } \
395 } while (0)
396 #define timespecsub(tsp, usp, vsp) \
397 do { \
398 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
399 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
400 if ((vsp)->tv_nsec < 0) { \
401 (vsp)->tv_sec--; \
402 (vsp)->tv_nsec += 1000000000L; \
403 } \
404 } while (0)
405
406 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
407 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
408 #define timercmp(tvp, uvp, cmp) \
409 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
410 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
411 ((tvp)->tv_sec cmp (uvp)->tv_sec))
412 #define timeradd(tvp, uvp, vvp) \
413 do { \
414 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
415 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
416 if ((vvp)->tv_usec >= 1000000) { \
417 (vvp)->tv_sec++; \
418 (vvp)->tv_usec -= 1000000; \
419 } \
420 } while (0)
421 #define timersub(tvp, uvp, vvp) \
422 do { \
423 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
424 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
425 if ((vvp)->tv_usec < 0) { \
426 (vvp)->tv_sec--; \
427 (vvp)->tv_usec += 1000000; \
428 } \
429 } while (0)
430
431 #endif /* __BSD_VISIBLE */
432
433 #if __GNU_VISIBLE
434 int futimesat (int, const char *, const struct timeval [2]);
435 #endif
436
437 #include <machine/_time.h>
438
439 _END_STD_C
440
441 #endif /* !_SYS_TIME_H_ */
442