1 | /** @file
|
---|
2 | * IPRT - Time.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_time_h
|
---|
27 | #define IPRT_INCLUDED_time_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/cdefs.h>
|
---|
33 | #include <iprt/types.h>
|
---|
34 | #include <iprt/assertcompile.h>
|
---|
35 |
|
---|
36 | RT_C_DECLS_BEGIN
|
---|
37 |
|
---|
38 | /** @defgroup grp_rt_time RTTime - Time
|
---|
39 | * @ingroup grp_rt
|
---|
40 | * @{
|
---|
41 | */
|
---|
42 |
|
---|
43 | /** Time Specification.
|
---|
44 | *
|
---|
45 | * Use the inline RTTimeSpecGet/Set to operate on structure this so we
|
---|
46 | * can easily change the representation if required later.
|
---|
47 | *
|
---|
48 | * The current representation is in nanoseconds relative to the unix epoch
|
---|
49 | * (1970-01-01 00:00:00 UTC). This gives us an approximate span from
|
---|
50 | * 1678 to 2262 without sacrificing the resolution offered by the various
|
---|
51 | * host OSes (BSD & LINUX 1ns, NT 100ns).
|
---|
52 | */
|
---|
53 | typedef struct RTTIMESPEC
|
---|
54 | {
|
---|
55 | /** Nanoseconds since epoch.
|
---|
56 | * The name is intentially too long to be comfortable to use because you should be
|
---|
57 | * using inline helpers! */
|
---|
58 | int64_t i64NanosecondsRelativeToUnixEpoch;
|
---|
59 | } RTTIMESPEC;
|
---|
60 |
|
---|
61 |
|
---|
62 | /** @name RTTIMESPEC methods
|
---|
63 | * @{ */
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Gets the time as nanoseconds relative to the unix epoch.
|
---|
67 | *
|
---|
68 | * @returns Nanoseconds relative to unix epoch.
|
---|
69 | * @param pTime The time spec to interpret.
|
---|
70 | */
|
---|
71 | DECLINLINE(int64_t) RTTimeSpecGetNano(PCRTTIMESPEC pTime)
|
---|
72 | {
|
---|
73 | return pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Sets the time give by nanoseconds relative to the unix epoch.
|
---|
79 | *
|
---|
80 | * @returns pTime.
|
---|
81 | * @param pTime The time spec to modify.
|
---|
82 | * @param i64Nano The new time in nanoseconds.
|
---|
83 | */
|
---|
84 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
85 | {
|
---|
86 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Nano;
|
---|
87 | return pTime;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Gets the time as microseconds relative to the unix epoch.
|
---|
93 | *
|
---|
94 | * @returns microseconds relative to unix epoch.
|
---|
95 | * @param pTime The time spec to interpret.
|
---|
96 | */
|
---|
97 | DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime)
|
---|
98 | {
|
---|
99 | return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1US;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Sets the time given by microseconds relative to the unix epoch.
|
---|
105 | *
|
---|
106 | * @returns pTime.
|
---|
107 | * @param pTime The time spec to modify.
|
---|
108 | * @param i64Micro The new time in microsecond.
|
---|
109 | */
|
---|
110 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
111 | {
|
---|
112 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Micro * RT_NS_1US;
|
---|
113 | return pTime;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Gets the time as milliseconds relative to the unix epoch.
|
---|
119 | *
|
---|
120 | * @returns milliseconds relative to unix epoch.
|
---|
121 | * @param pTime The time spec to interpret.
|
---|
122 | */
|
---|
123 | DECLINLINE(int64_t) RTTimeSpecGetMilli(PCRTTIMESPEC pTime)
|
---|
124 | {
|
---|
125 | return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1MS;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Sets the time given by milliseconds relative to the unix epoch.
|
---|
131 | *
|
---|
132 | * @returns pTime.
|
---|
133 | * @param pTime The time spec to modify.
|
---|
134 | * @param i64Milli The new time in milliseconds.
|
---|
135 | */
|
---|
136 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
137 | {
|
---|
138 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Milli * RT_NS_1MS;
|
---|
139 | return pTime;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Gets the time as seconds relative to the unix epoch.
|
---|
145 | *
|
---|
146 | * @returns seconds relative to unix epoch.
|
---|
147 | * @param pTime The time spec to interpret.
|
---|
148 | */
|
---|
149 | DECLINLINE(int64_t) RTTimeSpecGetSeconds(PCRTTIMESPEC pTime)
|
---|
150 | {
|
---|
151 | return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1SEC;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Sets the time given by seconds relative to the unix epoch.
|
---|
157 | *
|
---|
158 | * @returns pTime.
|
---|
159 | * @param pTime The time spec to modify.
|
---|
160 | * @param i64Seconds The new time in seconds.
|
---|
161 | */
|
---|
162 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
163 | {
|
---|
164 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * RT_NS_1SEC;
|
---|
165 | return pTime;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Makes the time spec absolute like abs() does (i.e. a positive value).
|
---|
171 | *
|
---|
172 | * @returns pTime.
|
---|
173 | * @param pTime The time spec to modify.
|
---|
174 | */
|
---|
175 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAbsolute(PRTTIMESPEC pTime)
|
---|
176 | {
|
---|
177 | if (pTime->i64NanosecondsRelativeToUnixEpoch < 0)
|
---|
178 | pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
179 | return pTime;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Negates the time.
|
---|
185 | *
|
---|
186 | * @returns pTime.
|
---|
187 | * @param pTime The time spec to modify.
|
---|
188 | */
|
---|
189 | DECLINLINE(PRTTIMESPEC) RTTimeSpecNegate(PRTTIMESPEC pTime)
|
---|
190 | {
|
---|
191 | pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
192 | return pTime;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Adds a time period to the time.
|
---|
198 | *
|
---|
199 | * @returns pTime.
|
---|
200 | * @param pTime The time spec to modify.
|
---|
201 | * @param pTimeAdd The time spec to add to pTime.
|
---|
202 | */
|
---|
203 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAdd(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeAdd)
|
---|
204 | {
|
---|
205 | pTime->i64NanosecondsRelativeToUnixEpoch += pTimeAdd->i64NanosecondsRelativeToUnixEpoch;
|
---|
206 | return pTime;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Adds a time period give as nanoseconds from the time.
|
---|
212 | *
|
---|
213 | * @returns pTime.
|
---|
214 | * @param pTime The time spec to modify.
|
---|
215 | * @param i64Nano The time period in nanoseconds.
|
---|
216 | */
|
---|
217 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
218 | {
|
---|
219 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Nano;
|
---|
220 | return pTime;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Adds a time period give as microseconds from the time.
|
---|
226 | *
|
---|
227 | * @returns pTime.
|
---|
228 | * @param pTime The time spec to modify.
|
---|
229 | * @param i64Micro The time period in microseconds.
|
---|
230 | */
|
---|
231 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
232 | {
|
---|
233 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Micro * RT_NS_1US;
|
---|
234 | return pTime;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Adds a time period give as milliseconds from the time.
|
---|
240 | *
|
---|
241 | * @returns pTime.
|
---|
242 | * @param pTime The time spec to modify.
|
---|
243 | * @param i64Milli The time period in milliseconds.
|
---|
244 | */
|
---|
245 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
246 | {
|
---|
247 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Milli * RT_NS_1MS;
|
---|
248 | return pTime;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Adds a time period give as seconds from the time.
|
---|
254 | *
|
---|
255 | * @returns pTime.
|
---|
256 | * @param pTime The time spec to modify.
|
---|
257 | * @param i64Seconds The time period in seconds.
|
---|
258 | */
|
---|
259 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
260 | {
|
---|
261 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Seconds * RT_NS_1SEC;
|
---|
262 | return pTime;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Subtracts a time period from the time.
|
---|
268 | *
|
---|
269 | * @returns pTime.
|
---|
270 | * @param pTime The time spec to modify.
|
---|
271 | * @param pTimeSub The time spec to subtract from pTime.
|
---|
272 | */
|
---|
273 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSub(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeSub)
|
---|
274 | {
|
---|
275 | pTime->i64NanosecondsRelativeToUnixEpoch -= pTimeSub->i64NanosecondsRelativeToUnixEpoch;
|
---|
276 | return pTime;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Subtracts a time period give as nanoseconds from the time.
|
---|
282 | *
|
---|
283 | * @returns pTime.
|
---|
284 | * @param pTime The time spec to modify.
|
---|
285 | * @param i64Nano The time period in nanoseconds.
|
---|
286 | */
|
---|
287 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
288 | {
|
---|
289 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Nano;
|
---|
290 | return pTime;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Subtracts a time period give as microseconds from the time.
|
---|
296 | *
|
---|
297 | * @returns pTime.
|
---|
298 | * @param pTime The time spec to modify.
|
---|
299 | * @param i64Micro The time period in microseconds.
|
---|
300 | */
|
---|
301 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
302 | {
|
---|
303 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Micro * RT_NS_1US;
|
---|
304 | return pTime;
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Subtracts a time period give as milliseconds from the time.
|
---|
310 | *
|
---|
311 | * @returns pTime.
|
---|
312 | * @param pTime The time spec to modify.
|
---|
313 | * @param i64Milli The time period in milliseconds.
|
---|
314 | */
|
---|
315 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
316 | {
|
---|
317 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Milli * RT_NS_1MS;
|
---|
318 | return pTime;
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Subtracts a time period give as seconds from the time.
|
---|
324 | *
|
---|
325 | * @returns pTime.
|
---|
326 | * @param pTime The time spec to modify.
|
---|
327 | * @param i64Seconds The time period in seconds.
|
---|
328 | */
|
---|
329 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
330 | {
|
---|
331 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Seconds * RT_NS_1SEC;
|
---|
332 | return pTime;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Gives the time in seconds and nanoseconds.
|
---|
338 | *
|
---|
339 | * @returns pTime.
|
---|
340 | * @param pTime The time spec to interpret.
|
---|
341 | * @param *pi32Seconds Where to store the time period in seconds.
|
---|
342 | * @param *pi32Nano Where to store the time period in nanoseconds.
|
---|
343 | */
|
---|
344 | DECLINLINE(void) RTTimeSpecGetSecondsAndNano(PRTTIMESPEC pTime, int32_t *pi32Seconds, int32_t *pi32Nano)
|
---|
345 | {
|
---|
346 | int64_t i64 = RTTimeSpecGetNano(pTime);
|
---|
347 | int32_t i32Nano = (int32_t)(i64 % RT_NS_1SEC);
|
---|
348 | i64 /= RT_NS_1SEC;
|
---|
349 | if (i32Nano < 0)
|
---|
350 | {
|
---|
351 | i32Nano += RT_NS_1SEC;
|
---|
352 | i64--;
|
---|
353 | }
|
---|
354 | *pi32Seconds = (int32_t)i64;
|
---|
355 | *pi32Nano = i32Nano;
|
---|
356 | }
|
---|
357 |
|
---|
358 |
|
---|
359 | /* PORTME: Add struct timeval guard macro here. */
|
---|
360 | /*
|
---|
361 | * Starting with Linux kernel version 5.6-rc3, the struct timeval is no longer
|
---|
362 | * available to kernel code and must not be used in kernel code.
|
---|
363 | * Only 64-bit time-interfaces are allowed into the kernel.
|
---|
364 | */
|
---|
365 | #if defined(RT_OS_LINUX) && defined(_LINUX_TIME64_H)
|
---|
366 | #define RTTIME_NO_TIMEVAL
|
---|
367 | #endif
|
---|
368 | #if !defined(RTTIME_NO_TIMEVAL) \
|
---|
369 | && (defined(RTTIME_INCL_TIMEVAL) || defined(_STRUCT_TIMEVAL) || defined(_SYS__TIMEVAL_H_) \
|
---|
370 | || defined(_SYS_TIME_H) || defined(_TIMEVAL) || defined(_LINUX_TIME_H) \
|
---|
371 | || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_)))
|
---|
372 | /**
|
---|
373 | * Gets the time as POSIX timeval.
|
---|
374 | *
|
---|
375 | * @returns pTime.
|
---|
376 | * @param pTime The time spec to interpret.
|
---|
377 | * @param pTimeval Where to store the time as POSIX timeval.
|
---|
378 | */
|
---|
379 | DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
|
---|
380 | {
|
---|
381 | int64_t i64 = RTTimeSpecGetMicro(pTime);
|
---|
382 | int32_t i32Micro = (int32_t)(i64 % RT_US_1SEC);
|
---|
383 | i64 /= RT_US_1SEC;
|
---|
384 | if (i32Micro < 0)
|
---|
385 | {
|
---|
386 | i32Micro += RT_US_1SEC;
|
---|
387 | i64--;
|
---|
388 | }
|
---|
389 | pTimeval->tv_sec = (time_t)i64;
|
---|
390 | pTimeval->tv_usec = i32Micro;
|
---|
391 | return pTimeval;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Sets the time as POSIX timeval.
|
---|
396 | *
|
---|
397 | * @returns pTime.
|
---|
398 | * @param pTime The time spec to modify.
|
---|
399 | * @param pTimeval Pointer to the POSIX timeval struct with the new time.
|
---|
400 | */
|
---|
401 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimeval(PRTTIMESPEC pTime, const struct timeval *pTimeval)
|
---|
402 | {
|
---|
403 | return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_usec);
|
---|
404 | }
|
---|
405 | #endif /* various ways of detecting struct timeval */
|
---|
406 |
|
---|
407 |
|
---|
408 | /* PORTME: Add struct timespec guard macro here. */
|
---|
409 | /*
|
---|
410 | * Starting with Linux kernel version 5.6-rc3, the _STRUCT_TIMESPEC is only defined
|
---|
411 | * under !__KERNEL__ guard and _LINUX_TIME64_H does not define a corresponding
|
---|
412 | * _STRUCT_TIMESPEC64. Only 64-bit time-interfaces are now allowed into the kernel.
|
---|
413 | * We have to keep it for __KERNEL__ though to support older guest kernels (2.6.X)
|
---|
414 | * without _LINUX_TIME64_H.
|
---|
415 | */
|
---|
416 | #if defined(RT_OS_LINUX) && defined(_LINUX_TIME64_H)
|
---|
417 | #define RTTIME_NO_TIMESPEC
|
---|
418 | #endif
|
---|
419 | #if !defined(RTTIME_NO_TIMESPEC) \
|
---|
420 | && (defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(_SYS__TIMESPEC_H_) \
|
---|
421 | || defined(TIMEVAL_TO_TIMESPEC) || defined(_TIMESPEC) \
|
---|
422 | || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_)))
|
---|
423 | /**
|
---|
424 | * Gets the time as POSIX timespec.
|
---|
425 | *
|
---|
426 | * @returns pTime.
|
---|
427 | * @param pTime The time spec to interpret.
|
---|
428 | * @param pTimespec Where to store the time as POSIX timespec.
|
---|
429 | */
|
---|
430 | DECLINLINE(struct timespec *) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime, struct timespec *pTimespec)
|
---|
431 | {
|
---|
432 | int64_t i64 = RTTimeSpecGetNano(pTime);
|
---|
433 | int32_t i32Nano = (int32_t)(i64 % RT_NS_1SEC);
|
---|
434 | i64 /= RT_NS_1SEC;
|
---|
435 | if (i32Nano < 0)
|
---|
436 | {
|
---|
437 | i32Nano += RT_NS_1SEC;
|
---|
438 | i64--;
|
---|
439 | }
|
---|
440 | pTimespec->tv_sec = (time_t)i64;
|
---|
441 | pTimespec->tv_nsec = i32Nano;
|
---|
442 | return pTimespec;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Sets the time as POSIX timespec.
|
---|
447 | *
|
---|
448 | * @returns pTime.
|
---|
449 | * @param pTime The time spec to modify.
|
---|
450 | * @param pTimespec Pointer to the POSIX timespec struct with the new time.
|
---|
451 | */
|
---|
452 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec(PRTTIMESPEC pTime, const struct timespec *pTimespec)
|
---|
453 | {
|
---|
454 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimespec->tv_sec), pTimespec->tv_nsec);
|
---|
455 | }
|
---|
456 | #endif /* various ways of detecting struct timespec */
|
---|
457 |
|
---|
458 | #if defined(RTTIME_NO_TIMESPEC)
|
---|
459 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec64(PRTTIMESPEC pTime, const struct timespec64 *pTimeval)
|
---|
460 | {
|
---|
461 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_nsec);
|
---|
462 | }
|
---|
463 | #endif /* RT_OS_LINUX && _LINUX_TIME64_H */
|
---|
464 |
|
---|
465 |
|
---|
466 |
|
---|
467 | /** The offset of the unix epoch and the base for NT time (in 100ns units).
|
---|
468 | * Nt time starts at 1601-01-01 00:00:00. */
|
---|
469 | #define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
|
---|
470 |
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Gets the time as NT time.
|
---|
474 | *
|
---|
475 | * @returns Nt time.
|
---|
476 | * @param pTime The time spec to interpret.
|
---|
477 | */
|
---|
478 | DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime)
|
---|
479 | {
|
---|
480 | return pTime->i64NanosecondsRelativeToUnixEpoch / 100
|
---|
481 | + RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * Sets the time given by Nt time.
|
---|
487 | *
|
---|
488 | * @returns pTime.
|
---|
489 | * @param pTime The time spec to modify.
|
---|
490 | * @param u64NtTime The new time in Nt time.
|
---|
491 | */
|
---|
492 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtTime(PRTTIMESPEC pTime, uint64_t u64NtTime)
|
---|
493 | {
|
---|
494 | pTime->i64NanosecondsRelativeToUnixEpoch =
|
---|
495 | ((int64_t)u64NtTime - RTTIME_NT_TIME_OFFSET_UNIX) * 100;
|
---|
496 | return pTime;
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | #ifdef _FILETIME_
|
---|
501 | /**
|
---|
502 | * Gets the time as NT file time.
|
---|
503 | *
|
---|
504 | * @returns pFileTime.
|
---|
505 | * @param pTime The time spec to interpret.
|
---|
506 | * @param pFileTime Pointer to NT filetime structure.
|
---|
507 | */
|
---|
508 | DECLINLINE(PFILETIME) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime, PFILETIME pFileTime)
|
---|
509 | {
|
---|
510 | *((uint64_t *)pFileTime) = RTTimeSpecGetNtTime(pTime);
|
---|
511 | return pFileTime;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Sets the time as NT file time.
|
---|
516 | *
|
---|
517 | * @returns pTime.
|
---|
518 | * @param pTime The time spec to modify.
|
---|
519 | * @param pFileTime Where to store the time as Nt file time.
|
---|
520 | */
|
---|
521 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime, const FILETIME *pFileTime)
|
---|
522 | {
|
---|
523 | return RTTimeSpecSetNtTime(pTime, *(const uint64_t *)pFileTime);
|
---|
524 | }
|
---|
525 | #endif
|
---|
526 |
|
---|
527 |
|
---|
528 | /** The offset to the start of DOS time.
|
---|
529 | * DOS time starts 1980-01-01 00:00:00. */
|
---|
530 | #define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
|
---|
531 |
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Gets the time as seconds relative to the start of dos time.
|
---|
535 | *
|
---|
536 | * @returns seconds relative to the start of dos time.
|
---|
537 | * @param pTime The time spec to interpret.
|
---|
538 | */
|
---|
539 | DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime)
|
---|
540 | {
|
---|
541 | return (pTime->i64NanosecondsRelativeToUnixEpoch - RTTIME_OFFSET_DOS_TIME)
|
---|
542 | / RT_NS_1SEC;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * Sets the time given by seconds relative to the start of dos time.
|
---|
548 | *
|
---|
549 | * @returns pTime.
|
---|
550 | * @param pTime The time spec to modify.
|
---|
551 | * @param i64Seconds The new time in seconds relative to the start of dos time.
|
---|
552 | */
|
---|
553 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
554 | {
|
---|
555 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * RT_NS_1SEC
|
---|
556 | + RTTIME_OFFSET_DOS_TIME;
|
---|
557 | return pTime;
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Compare two time specs.
|
---|
563 | *
|
---|
564 | * @returns true they are equal.
|
---|
565 | * @returns false they are not equal.
|
---|
566 | * @param pTime1 The 1st time spec.
|
---|
567 | * @param pTime2 The 2nd time spec.
|
---|
568 | */
|
---|
569 | DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1, PCRTTIMESPEC pTime2)
|
---|
570 | {
|
---|
571 | return pTime1->i64NanosecondsRelativeToUnixEpoch == pTime2->i64NanosecondsRelativeToUnixEpoch;
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Compare two time specs.
|
---|
577 | *
|
---|
578 | * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
|
---|
579 | * @returns false they are not equal.
|
---|
580 | * @param pLeft The 1st time spec.
|
---|
581 | * @param pRight The 2nd time spec.
|
---|
582 | */
|
---|
583 | DECLINLINE(int) RTTimeSpecCompare(PCRTTIMESPEC pLeft, PCRTTIMESPEC pRight)
|
---|
584 | {
|
---|
585 | if (pLeft->i64NanosecondsRelativeToUnixEpoch == pRight->i64NanosecondsRelativeToUnixEpoch)
|
---|
586 | return 0;
|
---|
587 | return pLeft->i64NanosecondsRelativeToUnixEpoch < pRight->i64NanosecondsRelativeToUnixEpoch ? -1 : 1;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Converts a time spec to a ISO date string.
|
---|
593 | *
|
---|
594 | * @returns psz on success.
|
---|
595 | * @returns NULL on buffer underflow.
|
---|
596 | * @param pTime The time spec.
|
---|
597 | * @param psz Where to store the string.
|
---|
598 | * @param cb The size of the buffer.
|
---|
599 | */
|
---|
600 | RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Attempts to convert an ISO date string to a time structure.
|
---|
604 | *
|
---|
605 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
606 | * and trailing spaces.
|
---|
607 | *
|
---|
608 | * @retval pTime on success,
|
---|
609 | * @retval NULL on failure.
|
---|
610 | * @param pTime The time spec.
|
---|
611 | * @param pszString The ISO date string to convert.
|
---|
612 | */
|
---|
613 | RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString);
|
---|
614 |
|
---|
615 | /** @} */
|
---|
616 |
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Exploded time.
|
---|
620 | */
|
---|
621 | typedef struct RTTIME
|
---|
622 | {
|
---|
623 | /** The year number. */
|
---|
624 | int32_t i32Year;
|
---|
625 | /** The month of the year (1-12). January is 1. */
|
---|
626 | uint8_t u8Month;
|
---|
627 | /** The day of the week (0-6). Monday is 0. */
|
---|
628 | uint8_t u8WeekDay;
|
---|
629 | /** The day of the year (1-366). January the 1st is 1. */
|
---|
630 | uint16_t u16YearDay;
|
---|
631 | /** The day of the month (1-31). */
|
---|
632 | uint8_t u8MonthDay;
|
---|
633 | /** Hour of the day (0-23). */
|
---|
634 | uint8_t u8Hour;
|
---|
635 | /** The minute of the hour (0-59). */
|
---|
636 | uint8_t u8Minute;
|
---|
637 | /** The second of the minute (0-60).
|
---|
638 | * (u32Nanosecond / 1000000) */
|
---|
639 | uint8_t u8Second;
|
---|
640 | /** The nanoseconds of the second (0-999999999). */
|
---|
641 | uint32_t u32Nanosecond;
|
---|
642 | /** Flags, of the RTTIME_FLAGS_* \#defines. */
|
---|
643 | uint32_t fFlags;
|
---|
644 | /** UCT time offset in minutes (-840-840). Positive for timezones east of
|
---|
645 | * UTC, negative for zones to the west. Same as what RTTimeLocalDeltaNano
|
---|
646 | * & RTTimeLocalDeltaNanoFor returns, just different unit. */
|
---|
647 | int32_t offUTC;
|
---|
648 | } RTTIME;
|
---|
649 | AssertCompileSize(RTTIME, 24);
|
---|
650 | /** Pointer to a exploded time structure. */
|
---|
651 | typedef RTTIME *PRTTIME;
|
---|
652 | /** Pointer to a const exploded time structure. */
|
---|
653 | typedef const RTTIME *PCRTTIME;
|
---|
654 |
|
---|
655 | /** @name RTTIME::fFlags values.
|
---|
656 | * @{ */
|
---|
657 | /** Set if the time is UTC. If clear the time local time. */
|
---|
658 | #define RTTIME_FLAGS_TYPE_MASK 3
|
---|
659 | /** the time is UTC time. */
|
---|
660 | #define RTTIME_FLAGS_TYPE_UTC 2
|
---|
661 | /** The time is local time. */
|
---|
662 | #define RTTIME_FLAGS_TYPE_LOCAL 3
|
---|
663 |
|
---|
664 | /** Set if the time is local and daylight saving time is in effect.
|
---|
665 | * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
|
---|
666 | #define RTTIME_FLAGS_DST RT_BIT(4)
|
---|
667 | /** Set if the time is local and there is no data available on daylight saving time. */
|
---|
668 | #define RTTIME_FLAGS_NO_DST_DATA RT_BIT(5)
|
---|
669 | /** Set if the year is a leap year.
|
---|
670 | * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
|
---|
671 | #define RTTIME_FLAGS_LEAP_YEAR RT_BIT(6)
|
---|
672 | /** Set if the year is a common year.
|
---|
673 | * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
|
---|
674 | #define RTTIME_FLAGS_COMMON_YEAR RT_BIT(7)
|
---|
675 | /** The mask of valid flags. */
|
---|
676 | #define RTTIME_FLAGS_MASK UINT32_C(0xff)
|
---|
677 | /** @} */
|
---|
678 |
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Gets the current system time (UTC).
|
---|
682 | *
|
---|
683 | * @returns pTime.
|
---|
684 | * @param pTime Where to store the time.
|
---|
685 | */
|
---|
686 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime);
|
---|
687 |
|
---|
688 | /**
|
---|
689 | * Sets the system time.
|
---|
690 | *
|
---|
691 | * @returns IPRT status code
|
---|
692 | * @param pTime The new system time (UTC).
|
---|
693 | *
|
---|
694 | * @remarks This will usually fail because changing the wall time is usually
|
---|
695 | * requires extra privileges.
|
---|
696 | */
|
---|
697 | RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime);
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * Explodes a time spec (UTC).
|
---|
701 | *
|
---|
702 | * @returns pTime.
|
---|
703 | * @param pTime Where to store the exploded time.
|
---|
704 | * @param pTimeSpec The time spec to exploded.
|
---|
705 | */
|
---|
706 | RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Implodes exploded time to a time spec (UTC).
|
---|
710 | *
|
---|
711 | * @returns pTime on success.
|
---|
712 | * @returns NULL if the pTime data is invalid.
|
---|
713 | * @param pTimeSpec Where to store the imploded UTC time.
|
---|
714 | * If pTime specifies a time which outside the range, maximum or
|
---|
715 | * minimum values will be returned.
|
---|
716 | * @param pTime Pointer to the exploded time to implode.
|
---|
717 | * The fields u8Month, u8WeekDay and u8MonthDay are not used,
|
---|
718 | * and all the other fields are expected to be within their
|
---|
719 | * bounds. Use RTTimeNormalize() to calculate u16YearDay and
|
---|
720 | * normalize the ranges of the fields.
|
---|
721 | */
|
---|
722 | RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime);
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Normalizes the fields of a time structure.
|
---|
726 | *
|
---|
727 | * It is possible to calculate year-day from month/day and vice
|
---|
728 | * versa. If you adjust any of of these, make sure to zero the
|
---|
729 | * other so you make it clear which of the fields to use. If
|
---|
730 | * it's ambiguous, the year-day field is used (and you get
|
---|
731 | * assertions in debug builds).
|
---|
732 | *
|
---|
733 | * All the time fields and the year-day or month/day fields will
|
---|
734 | * be adjusted for overflows. (Since all fields are unsigned, there
|
---|
735 | * is no underflows.) It is possible to exploit this for simple
|
---|
736 | * date math, though the recommended way of doing that to implode
|
---|
737 | * the time into a timespec and do the math on that.
|
---|
738 | *
|
---|
739 | * @returns pTime on success.
|
---|
740 | * @returns NULL if the data is invalid.
|
---|
741 | *
|
---|
742 | * @param pTime The time structure to normalize.
|
---|
743 | *
|
---|
744 | * @remarks This function doesn't work with local time, only with UTC time.
|
---|
745 | */
|
---|
746 | RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime);
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Gets the current local system time.
|
---|
750 | *
|
---|
751 | * @returns pTime.
|
---|
752 | * @param pTime Where to store the local time.
|
---|
753 | */
|
---|
754 | RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime);
|
---|
755 |
|
---|
756 | /**
|
---|
757 | * Gets the current delta between UTC and local time.
|
---|
758 | *
|
---|
759 | * @code
|
---|
760 | * RTTIMESPEC LocalTime;
|
---|
761 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
762 | * @endcode
|
---|
763 | *
|
---|
764 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
765 | */
|
---|
766 | RTDECL(int64_t) RTTimeLocalDeltaNano(void);
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Gets the delta between UTC and local time at the given time.
|
---|
770 | *
|
---|
771 | * @code
|
---|
772 | * RTTIMESPEC LocalTime;
|
---|
773 | * RTTimeNow(&LocalTime);
|
---|
774 | * RTTimeSpecAddNano(&LocalTime, RTTimeLocalDeltaNanoFor(&LocalTime));
|
---|
775 | * @endcode
|
---|
776 | *
|
---|
777 | * @param pTimeSpec The time spec giving the time to get the delta for.
|
---|
778 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
779 | */
|
---|
780 | RTDECL(int64_t) RTTimeLocalDeltaNanoFor(PCRTTIMESPEC pTimeSpec);
|
---|
781 |
|
---|
782 | /**
|
---|
783 | * Explodes a time spec to the localized timezone.
|
---|
784 | *
|
---|
785 | * @returns pTime.
|
---|
786 | * @param pTime Where to store the exploded time.
|
---|
787 | * @param pTimeSpec The time spec to exploded (UTC).
|
---|
788 | */
|
---|
789 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
790 |
|
---|
791 | /**
|
---|
792 | * Normalizes the fields of a time structure containing local time.
|
---|
793 | *
|
---|
794 | * See RTTimeNormalize for details.
|
---|
795 | *
|
---|
796 | * @returns pTime on success.
|
---|
797 | * @returns NULL if the data is invalid.
|
---|
798 | * @param pTime The time structure to normalize.
|
---|
799 | */
|
---|
800 | RTDECL(PRTTIME) RTTimeLocalNormalize(PRTTIME pTime);
|
---|
801 |
|
---|
802 | /**
|
---|
803 | * Converts a time structure to UTC, relying on UTC offset information
|
---|
804 | * if it contains local time.
|
---|
805 | *
|
---|
806 | * @returns pTime on success.
|
---|
807 | * @returns NULL if the data is invalid.
|
---|
808 | * @param pTime The time structure to convert.
|
---|
809 | */
|
---|
810 | RTDECL(PRTTIME) RTTimeConvertToZulu(PRTTIME pTime);
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * Converts a time spec to a ISO date string.
|
---|
814 | *
|
---|
815 | * @returns psz on success.
|
---|
816 | * @returns NULL on buffer underflow.
|
---|
817 | * @param pTime The time. Caller should've normalized this.
|
---|
818 | * @param psz Where to store the string.
|
---|
819 | * @param cb The size of the buffer.
|
---|
820 | */
|
---|
821 | RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb);
|
---|
822 |
|
---|
823 | /**
|
---|
824 | * Converts a time spec to a ISO date string, extended version.
|
---|
825 | *
|
---|
826 | * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
|
---|
827 | * (negative) or VERR_OUT_OF_RANGE (negative) on failure.
|
---|
828 | * @param pTime The time. Caller should've normalized this.
|
---|
829 | * @param psz Where to store the string.
|
---|
830 | * @param cb The size of the buffer.
|
---|
831 | * @param cFractionDigits Number of digits in the fraction. Max is 9.
|
---|
832 | */
|
---|
833 | RTDECL(ssize_t) RTTimeToStringEx(PCRTTIME pTime, char *psz, size_t cb, unsigned cFractionDigits);
|
---|
834 |
|
---|
835 | /** Suggested buffer length for RTTimeToString and RTTimeToStringEx output, including terminator. */
|
---|
836 | #define RTTIME_STR_LEN 40
|
---|
837 |
|
---|
838 | /**
|
---|
839 | * Attempts to convert an ISO date string to a time structure.
|
---|
840 | *
|
---|
841 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
842 | * and trailing spaces.
|
---|
843 | *
|
---|
844 | * @retval pTime on success,
|
---|
845 | * @retval NULL on failure.
|
---|
846 | * @param pTime Where to store the time on success.
|
---|
847 | * @param pszString The ISO date string to convert.
|
---|
848 | */
|
---|
849 | RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString);
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * Formats the given time on a RTC-2822 compliant format.
|
---|
853 | *
|
---|
854 | * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
|
---|
855 | * (negative) on failure.
|
---|
856 | * @param pTime The time. Caller should've normalized this.
|
---|
857 | * @param psz Where to store the string.
|
---|
858 | * @param cb The size of the buffer.
|
---|
859 | * @param fFlags RTTIME_RFC2822_F_XXX
|
---|
860 | * @sa RTTIME_RFC2822_LEN
|
---|
861 | */
|
---|
862 | RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb, uint32_t fFlags);
|
---|
863 |
|
---|
864 | /** Suggested buffer length for RTTimeToRfc2822 output, including terminator. */
|
---|
865 | #define RTTIME_RFC2822_LEN 40
|
---|
866 | /** @name RTTIME_RFC2822_F_XXX
|
---|
867 | * @{ */
|
---|
868 | /** Use the deprecated GMT timezone instead of +/-0000.
|
---|
869 | * This is required by the HTTP RFC-7231 7.1.1.1. */
|
---|
870 | #define RTTIME_RFC2822_F_GMT RT_BIT_32(0)
|
---|
871 | /** @} */
|
---|
872 |
|
---|
873 | /**
|
---|
874 | * Attempts to convert an RFC-2822 date string to a time structure.
|
---|
875 | *
|
---|
876 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
877 | * and trailing spaces.
|
---|
878 | *
|
---|
879 | * @retval pTime on success,
|
---|
880 | * @retval NULL on failure.
|
---|
881 | * @param pTime Where to store the time on success.
|
---|
882 | * @param pszString The ISO date string to convert.
|
---|
883 | */
|
---|
884 | RTDECL(PRTTIME) RTTimeFromRfc2822(PRTTIME pTime, const char *pszString);
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Checks if a year is a leap year or not.
|
---|
888 | *
|
---|
889 | * @returns true if it's a leap year.
|
---|
890 | * @returns false if it's a common year.
|
---|
891 | * @param i32Year The year in question.
|
---|
892 | */
|
---|
893 | RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year);
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Compares two normalized time structures.
|
---|
897 | *
|
---|
898 | * @retval 0 if equal.
|
---|
899 | * @retval -1 if @a pLeft is earlier than @a pRight.
|
---|
900 | * @retval 1 if @a pRight is earlier than @a pLeft.
|
---|
901 | *
|
---|
902 | * @param pLeft The left side time. NULL is accepted.
|
---|
903 | * @param pRight The right side time. NULL is accepted.
|
---|
904 | *
|
---|
905 | * @note A NULL time is considered smaller than anything else. If both are
|
---|
906 | * NULL, they are considered equal.
|
---|
907 | */
|
---|
908 | RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight);
|
---|
909 |
|
---|
910 | /**
|
---|
911 | * Gets the current nanosecond timestamp.
|
---|
912 | *
|
---|
913 | * @returns nanosecond timestamp.
|
---|
914 | */
|
---|
915 | RTDECL(uint64_t) RTTimeNanoTS(void);
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Gets the current millisecond timestamp.
|
---|
919 | *
|
---|
920 | * @returns millisecond timestamp.
|
---|
921 | */
|
---|
922 | RTDECL(uint64_t) RTTimeMilliTS(void);
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Debugging the time api.
|
---|
926 | *
|
---|
927 | * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
|
---|
928 | */
|
---|
929 | RTDECL(uint32_t) RTTimeDbgSteps(void);
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * Debugging the time api.
|
---|
933 | *
|
---|
934 | * @returns the number of times the TSC interval expired RTTimeNanoTS().
|
---|
935 | */
|
---|
936 | RTDECL(uint32_t) RTTimeDbgExpired(void);
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Debugging the time api.
|
---|
940 | *
|
---|
941 | * @returns the number of bad previous values encountered by RTTimeNanoTS().
|
---|
942 | */
|
---|
943 | RTDECL(uint32_t) RTTimeDbgBad(void);
|
---|
944 |
|
---|
945 | /**
|
---|
946 | * Debugging the time api.
|
---|
947 | *
|
---|
948 | * @returns the number of update races in RTTimeNanoTS().
|
---|
949 | */
|
---|
950 | RTDECL(uint32_t) RTTimeDbgRaces(void);
|
---|
951 |
|
---|
952 | /** @name RTTimeNanoTS GIP worker functions, for TM.
|
---|
953 | * @{ */
|
---|
954 | /** Pointer to a RTTIMENANOTSDATA structure. */
|
---|
955 | typedef struct RTTIMENANOTSDATA *PRTTIMENANOTSDATA;
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Nanosecond timestamp data.
|
---|
959 | *
|
---|
960 | * This is used to keep track of statistics and callback so IPRT
|
---|
961 | * and TM (VirtualBox) can share code.
|
---|
962 | *
|
---|
963 | * @remark Keep this in sync with the assembly version in timesupA.asm.
|
---|
964 | */
|
---|
965 | typedef struct RTTIMENANOTSDATA
|
---|
966 | {
|
---|
967 | /** Where the previous timestamp is stored.
|
---|
968 | * This is maintained to ensure that time doesn't go backwards or anything. */
|
---|
969 | uint64_t volatile *pu64Prev;
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Helper function that's used by the assembly routines when something goes bust.
|
---|
973 | *
|
---|
974 | * @param pData Pointer to this structure.
|
---|
975 | * @param u64NanoTS The calculated nano ts.
|
---|
976 | * @param u64DeltaPrev The delta relative to the previously returned timestamp.
|
---|
977 | * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
|
---|
978 | */
|
---|
979 | DECLCALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * Callback for when rediscovery is required.
|
---|
983 | *
|
---|
984 | * @returns Nanosecond timestamp.
|
---|
985 | * @param pData Pointer to this structure.
|
---|
986 | */
|
---|
987 | DECLCALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
988 |
|
---|
989 | /**
|
---|
990 | * Callback for when some CPU index related stuff goes wrong.
|
---|
991 | *
|
---|
992 | * @returns Nanosecond timestamp.
|
---|
993 | * @param pData Pointer to this structure.
|
---|
994 | * @param idApic The APIC ID if available, otherwise (UINT16_MAX-1).
|
---|
995 | * @param iCpuSet The CPU set index if available, otherwise
|
---|
996 | * (UINT16_MAX-1).
|
---|
997 | * @param iGipCpu The GIP CPU array index if available, otherwise
|
---|
998 | * (UINT16_MAX-1).
|
---|
999 | */
|
---|
1000 | DECLCALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1001 |
|
---|
1002 | /** Number of 1ns steps because of overshooting the period. */
|
---|
1003 | uint32_t c1nsSteps;
|
---|
1004 | /** The number of times the interval expired (overflow). */
|
---|
1005 | uint32_t cExpired;
|
---|
1006 | /** Number of "bad" previous values. */
|
---|
1007 | uint32_t cBadPrev;
|
---|
1008 | /** The number of update races. */
|
---|
1009 | uint32_t cUpdateRaces;
|
---|
1010 | } RTTIMENANOTSDATA;
|
---|
1011 |
|
---|
1012 | #ifndef IN_RING3
|
---|
1013 | /**
|
---|
1014 | * The Ring-3 layout of the RTTIMENANOTSDATA structure.
|
---|
1015 | */
|
---|
1016 | typedef struct RTTIMENANOTSDATAR3
|
---|
1017 | {
|
---|
1018 | R3PTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
1019 | DECLR3CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
1020 | DECLR3CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1021 | DECLR3CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1022 | uint32_t c1nsSteps;
|
---|
1023 | uint32_t cExpired;
|
---|
1024 | uint32_t cBadPrev;
|
---|
1025 | uint32_t cUpdateRaces;
|
---|
1026 | } RTTIMENANOTSDATAR3;
|
---|
1027 | #else
|
---|
1028 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR3;
|
---|
1029 | #endif
|
---|
1030 |
|
---|
1031 | #ifndef IN_RING0
|
---|
1032 | /**
|
---|
1033 | * The Ring-3 layout of the RTTIMENANOTSDATA structure.
|
---|
1034 | */
|
---|
1035 | typedef struct RTTIMENANOTSDATAR0
|
---|
1036 | {
|
---|
1037 | R0PTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
1038 | DECLR0CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
1039 | DECLR0CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1040 | DECLR0CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1041 | uint32_t c1nsSteps;
|
---|
1042 | uint32_t cExpired;
|
---|
1043 | uint32_t cBadPrev;
|
---|
1044 | uint32_t cUpdateRaces;
|
---|
1045 | } RTTIMENANOTSDATAR0;
|
---|
1046 | #else
|
---|
1047 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR0;
|
---|
1048 | #endif
|
---|
1049 |
|
---|
1050 | #ifndef IN_RC
|
---|
1051 | /**
|
---|
1052 | * The RC layout of the RTTIMENANOTSDATA structure.
|
---|
1053 | */
|
---|
1054 | typedef struct RTTIMENANOTSDATARC
|
---|
1055 | {
|
---|
1056 | RCPTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
1057 | DECLRCCALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
1058 | DECLRCCALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1059 | DECLRCCALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1060 | uint32_t c1nsSteps;
|
---|
1061 | uint32_t cExpired;
|
---|
1062 | uint32_t cBadPrev;
|
---|
1063 | uint32_t cUpdateRaces;
|
---|
1064 | } RTTIMENANOTSDATARC;
|
---|
1065 | #else
|
---|
1066 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATARC;
|
---|
1067 | #endif
|
---|
1068 |
|
---|
1069 | /** Internal RTTimeNanoTS worker (assembly). */
|
---|
1070 | typedef DECLCALLBACKTYPE(uint64_t, FNTIMENANOTSINTERNAL,(PRTTIMENANOTSDATA pData));
|
---|
1071 | /** Pointer to an internal RTTimeNanoTS worker (assembly). */
|
---|
1072 | typedef FNTIMENANOTSINTERNAL *PFNTIMENANOTSINTERNAL;
|
---|
1073 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarNoDelta(PRTTIMENANOTSDATA pData);
|
---|
1074 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarNoDelta(PRTTIMENANOTSDATA pData);
|
---|
1075 | #ifdef IN_RING3
|
---|
1076 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1077 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1078 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1079 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1080 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData);
|
---|
1081 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1082 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1083 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1084 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1085 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1086 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1087 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1088 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1089 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1090 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1091 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData);
|
---|
1092 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1093 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1094 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1095 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1096 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1097 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1098 | #else
|
---|
1099 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsync(PRTTIMENANOTSDATA pData);
|
---|
1100 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDelta(PRTTIMENANOTSDATA pData);
|
---|
1101 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsync(PRTTIMENANOTSDATA pData);
|
---|
1102 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDelta(PRTTIMENANOTSDATA pData);
|
---|
1103 | #endif
|
---|
1104 |
|
---|
1105 | /** @} */
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Gets the current nanosecond timestamp.
|
---|
1110 | *
|
---|
1111 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
1112 | * resolution or performance optimizations.
|
---|
1113 | *
|
---|
1114 | * @returns nanosecond timestamp.
|
---|
1115 | */
|
---|
1116 | RTDECL(uint64_t) RTTimeSystemNanoTS(void);
|
---|
1117 |
|
---|
1118 | /**
|
---|
1119 | * Gets the current millisecond timestamp.
|
---|
1120 | *
|
---|
1121 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
1122 | * resolution or performance optimizations.
|
---|
1123 | *
|
---|
1124 | * @returns millisecond timestamp.
|
---|
1125 | */
|
---|
1126 | RTDECL(uint64_t) RTTimeSystemMilliTS(void);
|
---|
1127 |
|
---|
1128 | /**
|
---|
1129 | * Get the nanosecond timestamp relative to program startup.
|
---|
1130 | *
|
---|
1131 | * @returns Timestamp relative to program startup.
|
---|
1132 | */
|
---|
1133 | RTDECL(uint64_t) RTTimeProgramNanoTS(void);
|
---|
1134 |
|
---|
1135 | /**
|
---|
1136 | * Get the microsecond timestamp relative to program startup.
|
---|
1137 | *
|
---|
1138 | * @returns Timestamp relative to program startup.
|
---|
1139 | */
|
---|
1140 | RTDECL(uint64_t) RTTimeProgramMicroTS(void);
|
---|
1141 |
|
---|
1142 | /**
|
---|
1143 | * Get the millisecond timestamp relative to program startup.
|
---|
1144 | *
|
---|
1145 | * @returns Timestamp relative to program startup.
|
---|
1146 | */
|
---|
1147 | RTDECL(uint64_t) RTTimeProgramMilliTS(void);
|
---|
1148 |
|
---|
1149 | /**
|
---|
1150 | * Get the second timestamp relative to program startup.
|
---|
1151 | *
|
---|
1152 | * @returns Timestamp relative to program startup.
|
---|
1153 | */
|
---|
1154 | RTDECL(uint32_t) RTTimeProgramSecTS(void);
|
---|
1155 |
|
---|
1156 | /**
|
---|
1157 | * Get the RTTimeNanoTS() of when the program started.
|
---|
1158 | *
|
---|
1159 | * @returns Program startup timestamp.
|
---|
1160 | */
|
---|
1161 | RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
|
---|
1162 |
|
---|
1163 |
|
---|
1164 | /**
|
---|
1165 | * Time zone information.
|
---|
1166 | */
|
---|
1167 | typedef struct RTTIMEZONEINFO
|
---|
1168 | {
|
---|
1169 | /** Unix time zone name (continent/country[/city]|). */
|
---|
1170 | const char *pszUnixName;
|
---|
1171 | /** Windows time zone name. */
|
---|
1172 | const char *pszWindowsName;
|
---|
1173 | /** The length of the unix time zone name. */
|
---|
1174 | uint8_t cchUnixName;
|
---|
1175 | /** The length of the windows time zone name. */
|
---|
1176 | uint8_t cchWindowsName;
|
---|
1177 | /** Two letter country/territory code if applicable, otherwise 'ZZ'. */
|
---|
1178 | char szCountry[3];
|
---|
1179 | /** Two letter windows country/territory code if applicable.
|
---|
1180 | * Empty string if no windows mapping. */
|
---|
1181 | char szWindowsCountry[3];
|
---|
1182 | #if 0 /* Add when needed and it's been extracted. */
|
---|
1183 | /** The standard delta in minutes (add to UTC). */
|
---|
1184 | int16_t cMinStdDelta;
|
---|
1185 | /** The daylight saving time delta in minutes (add to UTC). */
|
---|
1186 | int16_t cMinDstDelta;
|
---|
1187 | #endif
|
---|
1188 | /** closest matching windows time zone index. */
|
---|
1189 | uint32_t idxWindows;
|
---|
1190 | /** Flags, RTTIMEZONEINFO_F_XXX. */
|
---|
1191 | uint32_t fFlags;
|
---|
1192 | } RTTIMEZONEINFO;
|
---|
1193 | /** Pointer to time zone info. */
|
---|
1194 | typedef RTTIMEZONEINFO const *PCRTTIMEZONEINFO;
|
---|
1195 |
|
---|
1196 | /** @name RTTIMEZONEINFO_F_XXX - time zone info flags.
|
---|
1197 | * @{ */
|
---|
1198 | /** Indicates golden mapping entry for a windows time zone name. */
|
---|
1199 | #define RTTIMEZONEINFO_F_GOLDEN RT_BIT_32(0)
|
---|
1200 | /** @} */
|
---|
1201 |
|
---|
1202 | /**
|
---|
1203 | * Looks up static time zone information by unix name.
|
---|
1204 | *
|
---|
1205 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1206 | * @param pszName The unix zone name (TZ).
|
---|
1207 | */
|
---|
1208 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByUnixName(const char *pszName);
|
---|
1209 |
|
---|
1210 | /**
|
---|
1211 | * Looks up static time zone information by window name.
|
---|
1212 | *
|
---|
1213 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1214 | * @param pszName The windows zone name (reg key).
|
---|
1215 | */
|
---|
1216 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByWindowsName(const char *pszName);
|
---|
1217 |
|
---|
1218 | /**
|
---|
1219 | * Looks up static time zone information by windows index.
|
---|
1220 | *
|
---|
1221 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1222 | * @param idxZone The windows timezone index.
|
---|
1223 | */
|
---|
1224 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByWindowsIndex(uint32_t idxZone);
|
---|
1225 |
|
---|
1226 | /**
|
---|
1227 | * Get the current time zone (TZ).
|
---|
1228 | *
|
---|
1229 | * @returns IPRT status code.
|
---|
1230 | * @param pszName Where to return the time zone name.
|
---|
1231 | * @param cbName The size of the name buffer.
|
---|
1232 | */
|
---|
1233 | RTDECL(int) RTTimeZoneGetCurrent(char *pszName, size_t cbName);
|
---|
1234 |
|
---|
1235 | /** @} */
|
---|
1236 |
|
---|
1237 | RT_C_DECLS_END
|
---|
1238 |
|
---|
1239 | #endif /* !IPRT_INCLUDED_time_h */
|
---|
1240 |
|
---|