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 | * Copyright (c) 1982, 1986, 1993 |
---|
7 | * The Regents of the University of California. All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions |
---|
11 | * are met: |
---|
12 | * 1. Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * 4. Neither the name of the University nor the names of its contributors |
---|
18 | * may be used to endorse or promote products derived from this software |
---|
19 | * without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
31 | * SUCH DAMAGE. |
---|
32 | * |
---|
33 | * @(#)time.h 8.5 (Berkeley) 5/4/95 |
---|
34 | * $FreeBSD$ |
---|
35 | */ |
---|
36 | |
---|
37 | #ifndef _SYS_TIME_H_ |
---|
38 | #define _SYS_TIME_H_ |
---|
39 | |
---|
40 | #include <_ansi.h> |
---|
41 | #include <sys/cdefs.h> |
---|
42 | #include <sys/_timeval.h> |
---|
43 | #include <sys/types.h> |
---|
44 | #include <sys/timespec.h> |
---|
45 | |
---|
46 | #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE |
---|
47 | #include <sys/select.h> |
---|
48 | #endif |
---|
49 | |
---|
50 | struct timezone { |
---|
51 | int tz_minuteswest; /* minutes west of Greenwich */ |
---|
52 | int tz_dsttime; /* type of dst correction */ |
---|
53 | }; |
---|
54 | #define DST_NONE 0 /* not on dst */ |
---|
55 | #define DST_USA 1 /* USA style dst */ |
---|
56 | #define DST_AUST 2 /* Australian style dst */ |
---|
57 | #define DST_WET 3 /* Western European dst */ |
---|
58 | #define DST_MET 4 /* Middle European dst */ |
---|
59 | #define DST_EET 5 /* Eastern European dst */ |
---|
60 | #define DST_CAN 6 /* Canada */ |
---|
61 | |
---|
62 | #if __BSD_VISIBLE |
---|
63 | struct bintime { |
---|
64 | time_t sec; |
---|
65 | uint64_t frac; |
---|
66 | }; |
---|
67 | |
---|
68 | static __inline void |
---|
69 | bintime_addx(struct bintime *_bt, uint64_t _x) |
---|
70 | { |
---|
71 | uint64_t _u; |
---|
72 | |
---|
73 | _u = _bt->frac; |
---|
74 | _bt->frac += _x; |
---|
75 | if (_u > _bt->frac) |
---|
76 | _bt->sec++; |
---|
77 | } |
---|
78 | |
---|
79 | static __inline void |
---|
80 | bintime_add(struct bintime *_bt, const struct bintime *_bt2) |
---|
81 | { |
---|
82 | uint64_t _u; |
---|
83 | |
---|
84 | _u = _bt->frac; |
---|
85 | _bt->frac += _bt2->frac; |
---|
86 | if (_u > _bt->frac) |
---|
87 | _bt->sec++; |
---|
88 | _bt->sec += _bt2->sec; |
---|
89 | } |
---|
90 | |
---|
91 | static __inline void |
---|
92 | bintime_sub(struct bintime *_bt, const struct bintime *_bt2) |
---|
93 | { |
---|
94 | uint64_t _u; |
---|
95 | |
---|
96 | _u = _bt->frac; |
---|
97 | _bt->frac -= _bt2->frac; |
---|
98 | if (_u < _bt->frac) |
---|
99 | _bt->sec--; |
---|
100 | _bt->sec -= _bt2->sec; |
---|
101 | } |
---|
102 | |
---|
103 | static __inline void |
---|
104 | bintime_mul(struct bintime *_bt, u_int _x) |
---|
105 | { |
---|
106 | uint64_t _p1, _p2; |
---|
107 | |
---|
108 | _p1 = (_bt->frac & 0xffffffffull) * _x; |
---|
109 | _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32); |
---|
110 | _bt->sec *= _x; |
---|
111 | _bt->sec += (_p2 >> 32); |
---|
112 | _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull); |
---|
113 | } |
---|
114 | |
---|
115 | static __inline void |
---|
116 | bintime_shift(struct bintime *_bt, int _exp) |
---|
117 | { |
---|
118 | |
---|
119 | if (_exp > 0) { |
---|
120 | _bt->sec <<= _exp; |
---|
121 | _bt->sec |= _bt->frac >> (64 - _exp); |
---|
122 | _bt->frac <<= _exp; |
---|
123 | } else if (_exp < 0) { |
---|
124 | _bt->frac >>= -_exp; |
---|
125 | _bt->frac |= (uint64_t)_bt->sec << (64 + _exp); |
---|
126 | _bt->sec >>= -_exp; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | #define bintime_clear(a) ((a)->sec = (a)->frac = 0) |
---|
131 | #define bintime_isset(a) ((a)->sec || (a)->frac) |
---|
132 | #define bintime_cmp(a, b, cmp) \ |
---|
133 | (((a)->sec == (b)->sec) ? \ |
---|
134 | ((a)->frac cmp (b)->frac) : \ |
---|
135 | ((a)->sec cmp (b)->sec)) |
---|
136 | |
---|
137 | #define SBT_1S ((sbintime_t)1 << 32) |
---|
138 | #define SBT_1M (SBT_1S * 60) |
---|
139 | #define SBT_1MS (SBT_1S / 1000) |
---|
140 | #define SBT_1US (SBT_1S / 1000000) |
---|
141 | #define SBT_1NS (SBT_1S / 1000000000) |
---|
142 | #define SBT_MAX 0x7fffffffffffffffLL |
---|
143 | |
---|
144 | static __inline int |
---|
145 | sbintime_getsec(sbintime_t _sbt) |
---|
146 | { |
---|
147 | |
---|
148 | return (_sbt >> 32); |
---|
149 | } |
---|
150 | |
---|
151 | static __inline sbintime_t |
---|
152 | bttosbt(const struct bintime _bt) |
---|
153 | { |
---|
154 | |
---|
155 | return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32)); |
---|
156 | } |
---|
157 | |
---|
158 | static __inline struct bintime |
---|
159 | sbttobt(sbintime_t _sbt) |
---|
160 | { |
---|
161 | struct bintime _bt; |
---|
162 | |
---|
163 | _bt.sec = _sbt >> 32; |
---|
164 | _bt.frac = _sbt << 32; |
---|
165 | return (_bt); |
---|
166 | } |
---|
167 | |
---|
168 | /*- |
---|
169 | * Background information: |
---|
170 | * |
---|
171 | * When converting between timestamps on parallel timescales of differing |
---|
172 | * resolutions it is historical and scientific practice to round down rather |
---|
173 | * than doing 4/5 rounding. |
---|
174 | * |
---|
175 | * The date changes at midnight, not at noon. |
---|
176 | * |
---|
177 | * Even at 15:59:59.999999999 it's not four'o'clock. |
---|
178 | * |
---|
179 | * time_second ticks after N.999999999 not after N.4999999999 |
---|
180 | */ |
---|
181 | |
---|
182 | static __inline void |
---|
183 | bintime2timespec(const struct bintime *_bt, struct timespec *_ts) |
---|
184 | { |
---|
185 | |
---|
186 | _ts->tv_sec = _bt->sec; |
---|
187 | _ts->tv_nsec = ((uint64_t)1000000000 * |
---|
188 | (uint32_t)(_bt->frac >> 32)) >> 32; |
---|
189 | } |
---|
190 | |
---|
191 | static __inline void |
---|
192 | timespec2bintime(const struct timespec *_ts, struct bintime *_bt) |
---|
193 | { |
---|
194 | |
---|
195 | _bt->sec = _ts->tv_sec; |
---|
196 | /* 18446744073 = int(2^64 / 1000000000) */ |
---|
197 | _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL; |
---|
198 | } |
---|
199 | |
---|
200 | static __inline void |
---|
201 | bintime2timeval(const struct bintime *_bt, struct timeval *_tv) |
---|
202 | { |
---|
203 | |
---|
204 | _tv->tv_sec = _bt->sec; |
---|
205 | _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32; |
---|
206 | } |
---|
207 | |
---|
208 | static __inline void |
---|
209 | timeval2bintime(const struct timeval *_tv, struct bintime *_bt) |
---|
210 | { |
---|
211 | |
---|
212 | _bt->sec = _tv->tv_sec; |
---|
213 | /* 18446744073709 = int(2^64 / 1000000) */ |
---|
214 | _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL; |
---|
215 | } |
---|
216 | |
---|
217 | static __inline struct timespec |
---|
218 | sbttots(sbintime_t _sbt) |
---|
219 | { |
---|
220 | struct timespec _ts; |
---|
221 | |
---|
222 | _ts.tv_sec = _sbt >> 32; |
---|
223 | _ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)_sbt) >> 32; |
---|
224 | return (_ts); |
---|
225 | } |
---|
226 | |
---|
227 | static __inline sbintime_t |
---|
228 | tstosbt(struct timespec _ts) |
---|
229 | { |
---|
230 | |
---|
231 | return (((sbintime_t)_ts.tv_sec << 32) + |
---|
232 | (_ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32)); |
---|
233 | } |
---|
234 | |
---|
235 | static __inline struct timeval |
---|
236 | sbttotv(sbintime_t _sbt) |
---|
237 | { |
---|
238 | struct timeval _tv; |
---|
239 | |
---|
240 | _tv.tv_sec = _sbt >> 32; |
---|
241 | _tv.tv_usec = ((uint64_t)1000000 * (uint32_t)_sbt) >> 32; |
---|
242 | return (_tv); |
---|
243 | } |
---|
244 | |
---|
245 | static __inline sbintime_t |
---|
246 | tvtosbt(struct timeval _tv) |
---|
247 | { |
---|
248 | |
---|
249 | return (((sbintime_t)_tv.tv_sec << 32) + |
---|
250 | (_tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32)); |
---|
251 | } |
---|
252 | #endif /* __BSD_VISIBLE */ |
---|
253 | |
---|
254 | /* |
---|
255 | * Names of the interval timers, and structure |
---|
256 | * defining a timer setting. |
---|
257 | */ |
---|
258 | #define ITIMER_REAL 0 |
---|
259 | #define ITIMER_VIRTUAL 1 |
---|
260 | #define ITIMER_PROF 2 |
---|
261 | |
---|
262 | struct itimerval { |
---|
263 | struct timeval it_interval; /* timer interval */ |
---|
264 | struct timeval it_value; /* current value */ |
---|
265 | }; |
---|
266 | |
---|
267 | #ifndef _KERNEL |
---|
268 | #include <time.h> |
---|
269 | |
---|
270 | __BEGIN_DECLS |
---|
271 | int utimes (const char *__path, const struct timeval *__tvp); |
---|
272 | |
---|
273 | #if __BSD_VISIBLE |
---|
274 | int adjtime (const struct timeval *, struct timeval *); |
---|
275 | int futimes (int, const struct timeval *); |
---|
276 | int lutimes (const char *, const struct timeval *); |
---|
277 | int settimeofday (const struct timeval *, const struct timezone *); |
---|
278 | #endif |
---|
279 | |
---|
280 | #if __MISC_VISIBLE || __XSI_VISIBLE |
---|
281 | int getitimer (int __which, struct itimerval *__value); |
---|
282 | int setitimer (int __which, const struct itimerval *__restrict __value, |
---|
283 | struct itimerval *__restrict __ovalue); |
---|
284 | #endif |
---|
285 | |
---|
286 | int gettimeofday (struct timeval *__restrict __p, |
---|
287 | void *__restrict __tz); |
---|
288 | |
---|
289 | #if __GNU_VISIBLE |
---|
290 | int futimesat (int, const char *, const struct timeval [2]); |
---|
291 | #endif |
---|
292 | |
---|
293 | #ifdef _COMPILING_NEWLIB |
---|
294 | int _gettimeofday (struct timeval *__p, void *__tz); |
---|
295 | #endif |
---|
296 | |
---|
297 | __END_DECLS |
---|
298 | |
---|
299 | #endif /* !_KERNEL */ |
---|
300 | #include <machine/_time.h> |
---|
301 | |
---|
302 | #endif /* !_SYS_TIME_H_ */ |
---|