1 | /** @file
|
---|
2 | * IPRT - Time.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2019 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 | #if defined(RTTIME_INCL_TIMEVAL) || defined(_STRUCT_TIMEVAL) || defined(_SYS__TIMEVAL_H_) || defined(_SYS_TIME_H) || defined(_TIMEVAL) || defined(_LINUX_TIME_H) \
|
---|
361 | || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_))
|
---|
362 | /**
|
---|
363 | * Gets the time as POSIX timeval.
|
---|
364 | *
|
---|
365 | * @returns pTime.
|
---|
366 | * @param pTime The time spec to interpret.
|
---|
367 | * @param pTimeval Where to store the time as POSIX timeval.
|
---|
368 | */
|
---|
369 | DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
|
---|
370 | {
|
---|
371 | int64_t i64 = RTTimeSpecGetMicro(pTime);
|
---|
372 | int32_t i32Micro = (int32_t)(i64 % RT_US_1SEC);
|
---|
373 | i64 /= RT_US_1SEC;
|
---|
374 | if (i32Micro < 0)
|
---|
375 | {
|
---|
376 | i32Micro += RT_US_1SEC;
|
---|
377 | i64--;
|
---|
378 | }
|
---|
379 | pTimeval->tv_sec = (time_t)i64;
|
---|
380 | pTimeval->tv_usec = i32Micro;
|
---|
381 | return pTimeval;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Sets the time as POSIX timeval.
|
---|
386 | *
|
---|
387 | * @returns pTime.
|
---|
388 | * @param pTime The time spec to modify.
|
---|
389 | * @param pTimeval Pointer to the POSIX timeval struct with the new time.
|
---|
390 | */
|
---|
391 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimeval(PRTTIMESPEC pTime, const struct timeval *pTimeval)
|
---|
392 | {
|
---|
393 | return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_usec);
|
---|
394 | }
|
---|
395 | #endif /* various ways of detecting struct timeval */
|
---|
396 |
|
---|
397 |
|
---|
398 | /* PORTME: Add struct timespec guard macro here. */
|
---|
399 | #if defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(_SYS__TIMESPEC_H_) || defined(TIMEVAL_TO_TIMESPEC) || defined(_TIMESPEC) \
|
---|
400 | || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_))
|
---|
401 | /**
|
---|
402 | * Gets the time as POSIX timespec.
|
---|
403 | *
|
---|
404 | * @returns pTime.
|
---|
405 | * @param pTime The time spec to interpret.
|
---|
406 | * @param pTimespec Where to store the time as POSIX timespec.
|
---|
407 | */
|
---|
408 | DECLINLINE(struct timespec *) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime, struct timespec *pTimespec)
|
---|
409 | {
|
---|
410 | int64_t i64 = RTTimeSpecGetNano(pTime);
|
---|
411 | int32_t i32Nano = (int32_t)(i64 % RT_NS_1SEC);
|
---|
412 | i64 /= RT_NS_1SEC;
|
---|
413 | if (i32Nano < 0)
|
---|
414 | {
|
---|
415 | i32Nano += RT_NS_1SEC;
|
---|
416 | i64--;
|
---|
417 | }
|
---|
418 | pTimespec->tv_sec = (time_t)i64;
|
---|
419 | pTimespec->tv_nsec = i32Nano;
|
---|
420 | return pTimespec;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Sets the time as POSIX timespec.
|
---|
425 | *
|
---|
426 | * @returns pTime.
|
---|
427 | * @param pTime The time spec to modify.
|
---|
428 | * @param pTimespec Pointer to the POSIX timespec struct with the new time.
|
---|
429 | */
|
---|
430 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec(PRTTIMESPEC pTime, const struct timespec *pTimespec)
|
---|
431 | {
|
---|
432 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimespec->tv_sec), pTimespec->tv_nsec);
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | # ifdef _LINUX_TIME64_H
|
---|
437 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec64(PRTTIMESPEC pTime, const struct timespec64 *pTimeval)
|
---|
438 | {
|
---|
439 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_nsec);
|
---|
440 | }
|
---|
441 | # endif
|
---|
442 | #endif /* various ways of detecting struct timespec */
|
---|
443 |
|
---|
444 |
|
---|
445 |
|
---|
446 | /** The offset of the unix epoch and the base for NT time (in 100ns units).
|
---|
447 | * Nt time starts at 1601-01-01 00:00:00. */
|
---|
448 | #define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
|
---|
449 |
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Gets the time as NT time.
|
---|
453 | *
|
---|
454 | * @returns Nt time.
|
---|
455 | * @param pTime The time spec to interpret.
|
---|
456 | */
|
---|
457 | DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime)
|
---|
458 | {
|
---|
459 | return pTime->i64NanosecondsRelativeToUnixEpoch / 100
|
---|
460 | + RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Sets the time given by Nt time.
|
---|
466 | *
|
---|
467 | * @returns pTime.
|
---|
468 | * @param pTime The time spec to modify.
|
---|
469 | * @param u64NtTime The new time in Nt time.
|
---|
470 | */
|
---|
471 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtTime(PRTTIMESPEC pTime, uint64_t u64NtTime)
|
---|
472 | {
|
---|
473 | pTime->i64NanosecondsRelativeToUnixEpoch =
|
---|
474 | ((int64_t)u64NtTime - RTTIME_NT_TIME_OFFSET_UNIX) * 100;
|
---|
475 | return pTime;
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | #ifdef _FILETIME_
|
---|
480 | /**
|
---|
481 | * Gets the time as NT file time.
|
---|
482 | *
|
---|
483 | * @returns pFileTime.
|
---|
484 | * @param pTime The time spec to interpret.
|
---|
485 | * @param pFileTime Pointer to NT filetime structure.
|
---|
486 | */
|
---|
487 | DECLINLINE(PFILETIME) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime, PFILETIME pFileTime)
|
---|
488 | {
|
---|
489 | *((uint64_t *)pFileTime) = RTTimeSpecGetNtTime(pTime);
|
---|
490 | return pFileTime;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Sets the time as NT file time.
|
---|
495 | *
|
---|
496 | * @returns pTime.
|
---|
497 | * @param pTime The time spec to modify.
|
---|
498 | * @param pFileTime Where to store the time as Nt file time.
|
---|
499 | */
|
---|
500 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime, const FILETIME *pFileTime)
|
---|
501 | {
|
---|
502 | return RTTimeSpecSetNtTime(pTime, *(const uint64_t *)pFileTime);
|
---|
503 | }
|
---|
504 | #endif
|
---|
505 |
|
---|
506 |
|
---|
507 | /** The offset to the start of DOS time.
|
---|
508 | * DOS time starts 1980-01-01 00:00:00. */
|
---|
509 | #define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
|
---|
510 |
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Gets the time as seconds relative to the start of dos time.
|
---|
514 | *
|
---|
515 | * @returns seconds relative to the start of dos time.
|
---|
516 | * @param pTime The time spec to interpret.
|
---|
517 | */
|
---|
518 | DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime)
|
---|
519 | {
|
---|
520 | return (pTime->i64NanosecondsRelativeToUnixEpoch - RTTIME_OFFSET_DOS_TIME)
|
---|
521 | / RT_NS_1SEC;
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Sets the time given by seconds relative to the start of dos time.
|
---|
527 | *
|
---|
528 | * @returns pTime.
|
---|
529 | * @param pTime The time spec to modify.
|
---|
530 | * @param i64Seconds The new time in seconds relative to the start of dos time.
|
---|
531 | */
|
---|
532 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
533 | {
|
---|
534 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * RT_NS_1SEC
|
---|
535 | + RTTIME_OFFSET_DOS_TIME;
|
---|
536 | return pTime;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Compare two time specs.
|
---|
542 | *
|
---|
543 | * @returns true they are equal.
|
---|
544 | * @returns false they are not equal.
|
---|
545 | * @param pTime1 The 1st time spec.
|
---|
546 | * @param pTime2 The 2nd time spec.
|
---|
547 | */
|
---|
548 | DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1, PCRTTIMESPEC pTime2)
|
---|
549 | {
|
---|
550 | return pTime1->i64NanosecondsRelativeToUnixEpoch == pTime2->i64NanosecondsRelativeToUnixEpoch;
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Compare two time specs.
|
---|
556 | *
|
---|
557 | * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
|
---|
558 | * @returns false they are not equal.
|
---|
559 | * @param pLeft The 1st time spec.
|
---|
560 | * @param pRight The 2nd time spec.
|
---|
561 | */
|
---|
562 | DECLINLINE(int) RTTimeSpecCompare(PCRTTIMESPEC pLeft, PCRTTIMESPEC pRight)
|
---|
563 | {
|
---|
564 | if (pLeft->i64NanosecondsRelativeToUnixEpoch == pRight->i64NanosecondsRelativeToUnixEpoch)
|
---|
565 | return 0;
|
---|
566 | return pLeft->i64NanosecondsRelativeToUnixEpoch < pRight->i64NanosecondsRelativeToUnixEpoch ? -1 : 1;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Converts a time spec to a ISO date string.
|
---|
572 | *
|
---|
573 | * @returns psz on success.
|
---|
574 | * @returns NULL on buffer underflow.
|
---|
575 | * @param pTime The time spec.
|
---|
576 | * @param psz Where to store the string.
|
---|
577 | * @param cb The size of the buffer.
|
---|
578 | */
|
---|
579 | RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Attempts to convert an ISO date string to a time structure.
|
---|
583 | *
|
---|
584 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
585 | * and trailing spaces.
|
---|
586 | *
|
---|
587 | * @retval pTime on success,
|
---|
588 | * @retval NULL on failure.
|
---|
589 | * @param pTime The time spec.
|
---|
590 | * @param pszString The ISO date string to convert.
|
---|
591 | */
|
---|
592 | RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString);
|
---|
593 |
|
---|
594 | /** @} */
|
---|
595 |
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * Exploded time.
|
---|
599 | */
|
---|
600 | typedef struct RTTIME
|
---|
601 | {
|
---|
602 | /** The year number. */
|
---|
603 | int32_t i32Year;
|
---|
604 | /** The month of the year (1-12). January is 1. */
|
---|
605 | uint8_t u8Month;
|
---|
606 | /** The day of the week (0-6). Monday is 0. */
|
---|
607 | uint8_t u8WeekDay;
|
---|
608 | /** The day of the year (1-366). January the 1st is 1. */
|
---|
609 | uint16_t u16YearDay;
|
---|
610 | /** The day of the month (1-31). */
|
---|
611 | uint8_t u8MonthDay;
|
---|
612 | /** Hour of the day (0-23). */
|
---|
613 | uint8_t u8Hour;
|
---|
614 | /** The minute of the hour (0-59). */
|
---|
615 | uint8_t u8Minute;
|
---|
616 | /** The second of the minute (0-60).
|
---|
617 | * (u32Nanosecond / 1000000) */
|
---|
618 | uint8_t u8Second;
|
---|
619 | /** The nanoseconds of the second (0-999999999). */
|
---|
620 | uint32_t u32Nanosecond;
|
---|
621 | /** Flags, of the RTTIME_FLAGS_* \#defines. */
|
---|
622 | uint32_t fFlags;
|
---|
623 | /** UCT time offset in minutes (-840-840). Positive for timezones east of
|
---|
624 | * UTC, negative for zones to the west. Same as what RTTimeLocalDeltaNano
|
---|
625 | * & RTTimeLocalDeltaNanoFor returns, just different unit. */
|
---|
626 | int32_t offUTC;
|
---|
627 | } RTTIME;
|
---|
628 | AssertCompileSize(RTTIME, 24);
|
---|
629 | /** Pointer to a exploded time structure. */
|
---|
630 | typedef RTTIME *PRTTIME;
|
---|
631 | /** Pointer to a const exploded time structure. */
|
---|
632 | typedef const RTTIME *PCRTTIME;
|
---|
633 |
|
---|
634 | /** @name RTTIME::fFlags values.
|
---|
635 | * @{ */
|
---|
636 | /** Set if the time is UTC. If clear the time local time. */
|
---|
637 | #define RTTIME_FLAGS_TYPE_MASK 3
|
---|
638 | /** the time is UTC time. */
|
---|
639 | #define RTTIME_FLAGS_TYPE_UTC 2
|
---|
640 | /** The time is local time. */
|
---|
641 | #define RTTIME_FLAGS_TYPE_LOCAL 3
|
---|
642 |
|
---|
643 | /** Set if the time is local and daylight saving time is in effect.
|
---|
644 | * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
|
---|
645 | #define RTTIME_FLAGS_DST RT_BIT(4)
|
---|
646 | /** Set if the time is local and there is no data available on daylight saving time. */
|
---|
647 | #define RTTIME_FLAGS_NO_DST_DATA RT_BIT(5)
|
---|
648 | /** Set if the year is a leap year.
|
---|
649 | * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
|
---|
650 | #define RTTIME_FLAGS_LEAP_YEAR RT_BIT(6)
|
---|
651 | /** Set if the year is a common year.
|
---|
652 | * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
|
---|
653 | #define RTTIME_FLAGS_COMMON_YEAR RT_BIT(7)
|
---|
654 | /** The mask of valid flags. */
|
---|
655 | #define RTTIME_FLAGS_MASK UINT32_C(0xff)
|
---|
656 | /** @} */
|
---|
657 |
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * Gets the current system time (UTC).
|
---|
661 | *
|
---|
662 | * @returns pTime.
|
---|
663 | * @param pTime Where to store the time.
|
---|
664 | */
|
---|
665 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime);
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Sets the system time.
|
---|
669 | *
|
---|
670 | * @returns IPRT status code
|
---|
671 | * @param pTime The new system time (UTC).
|
---|
672 | *
|
---|
673 | * @remarks This will usually fail because changing the wall time is usually
|
---|
674 | * requires extra privileges.
|
---|
675 | */
|
---|
676 | RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime);
|
---|
677 |
|
---|
678 | /**
|
---|
679 | * Explodes a time spec (UTC).
|
---|
680 | *
|
---|
681 | * @returns pTime.
|
---|
682 | * @param pTime Where to store the exploded time.
|
---|
683 | * @param pTimeSpec The time spec to exploded.
|
---|
684 | */
|
---|
685 | RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Implodes exploded time to a time spec (UTC).
|
---|
689 | *
|
---|
690 | * @returns pTime on success.
|
---|
691 | * @returns NULL if the pTime data is invalid.
|
---|
692 | * @param pTimeSpec Where to store the imploded UTC time.
|
---|
693 | * If pTime specifies a time which outside the range, maximum or
|
---|
694 | * minimum values will be returned.
|
---|
695 | * @param pTime Pointer to the exploded time to implode.
|
---|
696 | * The fields u8Month, u8WeekDay and u8MonthDay are not used,
|
---|
697 | * and all the other fields are expected to be within their
|
---|
698 | * bounds. Use RTTimeNormalize() to calculate u16YearDay and
|
---|
699 | * normalize the ranges of the fields.
|
---|
700 | */
|
---|
701 | RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime);
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Normalizes the fields of a time structure.
|
---|
705 | *
|
---|
706 | * It is possible to calculate year-day from month/day and vice
|
---|
707 | * versa. If you adjust any of of these, make sure to zero the
|
---|
708 | * other so you make it clear which of the fields to use. If
|
---|
709 | * it's ambiguous, the year-day field is used (and you get
|
---|
710 | * assertions in debug builds).
|
---|
711 | *
|
---|
712 | * All the time fields and the year-day or month/day fields will
|
---|
713 | * be adjusted for overflows. (Since all fields are unsigned, there
|
---|
714 | * is no underflows.) It is possible to exploit this for simple
|
---|
715 | * date math, though the recommended way of doing that to implode
|
---|
716 | * the time into a timespec and do the math on that.
|
---|
717 | *
|
---|
718 | * @returns pTime on success.
|
---|
719 | * @returns NULL if the data is invalid.
|
---|
720 | *
|
---|
721 | * @param pTime The time structure to normalize.
|
---|
722 | *
|
---|
723 | * @remarks This function doesn't work with local time, only with UTC time.
|
---|
724 | */
|
---|
725 | RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime);
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Gets the current local system time.
|
---|
729 | *
|
---|
730 | * @returns pTime.
|
---|
731 | * @param pTime Where to store the local time.
|
---|
732 | */
|
---|
733 | RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime);
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Gets the current delta between UTC and local time.
|
---|
737 | *
|
---|
738 | * @code
|
---|
739 | * RTTIMESPEC LocalTime;
|
---|
740 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
741 | * @endcode
|
---|
742 | *
|
---|
743 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
744 | */
|
---|
745 | RTDECL(int64_t) RTTimeLocalDeltaNano(void);
|
---|
746 |
|
---|
747 | /**
|
---|
748 | * Gets the delta between UTC and local time at the given time.
|
---|
749 | *
|
---|
750 | * @code
|
---|
751 | * RTTIMESPEC LocalTime;
|
---|
752 | * RTTimeNow(&LocalTime);
|
---|
753 | * RTTimeSpecAddNano(&LocalTime, RTTimeLocalDeltaNanoFor(&LocalTime));
|
---|
754 | * @endcode
|
---|
755 | *
|
---|
756 | * @param pTimeSpec The time spec giving the time to get the delta for.
|
---|
757 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
758 | */
|
---|
759 | RTDECL(int64_t) RTTimeLocalDeltaNanoFor(PCRTTIMESPEC pTimeSpec);
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Explodes a time spec to the localized timezone.
|
---|
763 | *
|
---|
764 | * @returns pTime.
|
---|
765 | * @param pTime Where to store the exploded time.
|
---|
766 | * @param pTimeSpec The time spec to exploded (UTC).
|
---|
767 | */
|
---|
768 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Normalizes the fields of a time structure containing local time.
|
---|
772 | *
|
---|
773 | * See RTTimeNormalize for details.
|
---|
774 | *
|
---|
775 | * @returns pTime on success.
|
---|
776 | * @returns NULL if the data is invalid.
|
---|
777 | * @param pTime The time structure to normalize.
|
---|
778 | */
|
---|
779 | RTDECL(PRTTIME) RTTimeLocalNormalize(PRTTIME pTime);
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Converts a time structure to UTC, relying on UTC offset information
|
---|
783 | * if it contains local time.
|
---|
784 | *
|
---|
785 | * @returns pTime on success.
|
---|
786 | * @returns NULL if the data is invalid.
|
---|
787 | * @param pTime The time structure to convert.
|
---|
788 | */
|
---|
789 | RTDECL(PRTTIME) RTTimeConvertToZulu(PRTTIME pTime);
|
---|
790 |
|
---|
791 | /**
|
---|
792 | * Converts a time spec to a ISO date string.
|
---|
793 | *
|
---|
794 | * @returns psz on success.
|
---|
795 | * @returns NULL on buffer underflow.
|
---|
796 | * @param pTime The time. Caller should've normalized this.
|
---|
797 | * @param psz Where to store the string.
|
---|
798 | * @param cb The size of the buffer.
|
---|
799 | */
|
---|
800 | RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb);
|
---|
801 |
|
---|
802 | /**
|
---|
803 | * Converts a time spec to a ISO date string, extended version.
|
---|
804 | *
|
---|
805 | * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
|
---|
806 | * (negative) or VERR_OUT_OF_RANGE (negative) on failure.
|
---|
807 | * @param pTime The time. Caller should've normalized this.
|
---|
808 | * @param psz Where to store the string.
|
---|
809 | * @param cb The size of the buffer.
|
---|
810 | * @param cFractionDigits Number of digits in the fraction. Max is 9.
|
---|
811 | */
|
---|
812 | RTDECL(ssize_t) RTTimeToStringEx(PCRTTIME pTime, char *psz, size_t cb, unsigned cFractionDigits);
|
---|
813 |
|
---|
814 | /** Suggested buffer length for RTTimeToString and RTTimeToStringEx output, including terminator. */
|
---|
815 | #define RTTIME_STR_LEN 40
|
---|
816 |
|
---|
817 | /**
|
---|
818 | * Attempts to convert an ISO date string to a time structure.
|
---|
819 | *
|
---|
820 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
821 | * and trailing spaces.
|
---|
822 | *
|
---|
823 | * @retval pTime on success,
|
---|
824 | * @retval NULL on failure.
|
---|
825 | * @param pTime Where to store the time on success.
|
---|
826 | * @param pszString The ISO date string to convert.
|
---|
827 | */
|
---|
828 | RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString);
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Formats the given time on a RTC-2822 compliant format.
|
---|
832 | *
|
---|
833 | * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
|
---|
834 | * (negative) on failure.
|
---|
835 | * @param pTime The time. Caller should've normalized this.
|
---|
836 | * @param psz Where to store the string.
|
---|
837 | * @param cb The size of the buffer.
|
---|
838 | * @param fFlags RTTIME_RFC2822_F_XXX
|
---|
839 | * @sa RTTIME_RFC2822_LEN
|
---|
840 | */
|
---|
841 | RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb, uint32_t fFlags);
|
---|
842 |
|
---|
843 | /** Suggested buffer length for RTTimeToRfc2822 output, including terminator. */
|
---|
844 | #define RTTIME_RFC2822_LEN 40
|
---|
845 | /** @name RTTIME_RFC2822_F_XXX
|
---|
846 | * @{ */
|
---|
847 | /** Use the deprecated GMT timezone instead of +/-0000.
|
---|
848 | * This is required by the HTTP RFC-7231 7.1.1.1. */
|
---|
849 | #define RTTIME_RFC2822_F_GMT RT_BIT_32(0)
|
---|
850 | /** @} */
|
---|
851 |
|
---|
852 | /**
|
---|
853 | * Attempts to convert an RFC-2822 date string to a time structure.
|
---|
854 | *
|
---|
855 | * We're a little forgiving with zero padding, unspecified parts, and leading
|
---|
856 | * and trailing spaces.
|
---|
857 | *
|
---|
858 | * @retval pTime on success,
|
---|
859 | * @retval NULL on failure.
|
---|
860 | * @param pTime Where to store the time on success.
|
---|
861 | * @param pszString The ISO date string to convert.
|
---|
862 | */
|
---|
863 | RTDECL(PRTTIME) RTTimeFromRfc2822(PRTTIME pTime, const char *pszString);
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Checks if a year is a leap year or not.
|
---|
867 | *
|
---|
868 | * @returns true if it's a leap year.
|
---|
869 | * @returns false if it's a common year.
|
---|
870 | * @param i32Year The year in question.
|
---|
871 | */
|
---|
872 | RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year);
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Compares two normalized time structures.
|
---|
876 | *
|
---|
877 | * @retval 0 if equal.
|
---|
878 | * @retval -1 if @a pLeft is earlier than @a pRight.
|
---|
879 | * @retval 1 if @a pRight is earlier than @a pLeft.
|
---|
880 | *
|
---|
881 | * @param pLeft The left side time. NULL is accepted.
|
---|
882 | * @param pRight The right side time. NULL is accepted.
|
---|
883 | *
|
---|
884 | * @note A NULL time is considered smaller than anything else. If both are
|
---|
885 | * NULL, they are considered equal.
|
---|
886 | */
|
---|
887 | RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight);
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Gets the current nanosecond timestamp.
|
---|
891 | *
|
---|
892 | * @returns nanosecond timestamp.
|
---|
893 | */
|
---|
894 | RTDECL(uint64_t) RTTimeNanoTS(void);
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Gets the current millisecond timestamp.
|
---|
898 | *
|
---|
899 | * @returns millisecond timestamp.
|
---|
900 | */
|
---|
901 | RTDECL(uint64_t) RTTimeMilliTS(void);
|
---|
902 |
|
---|
903 | /**
|
---|
904 | * Debugging the time api.
|
---|
905 | *
|
---|
906 | * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
|
---|
907 | */
|
---|
908 | RTDECL(uint32_t) RTTimeDbgSteps(void);
|
---|
909 |
|
---|
910 | /**
|
---|
911 | * Debugging the time api.
|
---|
912 | *
|
---|
913 | * @returns the number of times the TSC interval expired RTTimeNanoTS().
|
---|
914 | */
|
---|
915 | RTDECL(uint32_t) RTTimeDbgExpired(void);
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Debugging the time api.
|
---|
919 | *
|
---|
920 | * @returns the number of bad previous values encountered by RTTimeNanoTS().
|
---|
921 | */
|
---|
922 | RTDECL(uint32_t) RTTimeDbgBad(void);
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Debugging the time api.
|
---|
926 | *
|
---|
927 | * @returns the number of update races in RTTimeNanoTS().
|
---|
928 | */
|
---|
929 | RTDECL(uint32_t) RTTimeDbgRaces(void);
|
---|
930 |
|
---|
931 | /** @name RTTimeNanoTS GIP worker functions, for TM.
|
---|
932 | * @{ */
|
---|
933 | /** Pointer to a RTTIMENANOTSDATA structure. */
|
---|
934 | typedef struct RTTIMENANOTSDATA *PRTTIMENANOTSDATA;
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * Nanosecond timestamp data.
|
---|
938 | *
|
---|
939 | * This is used to keep track of statistics and callback so IPRT
|
---|
940 | * and TM (VirtualBox) can share code.
|
---|
941 | *
|
---|
942 | * @remark Keep this in sync with the assembly version in timesupA.asm.
|
---|
943 | */
|
---|
944 | typedef struct RTTIMENANOTSDATA
|
---|
945 | {
|
---|
946 | /** Where the previous timestamp is stored.
|
---|
947 | * This is maintained to ensure that time doesn't go backwards or anything. */
|
---|
948 | uint64_t volatile *pu64Prev;
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Helper function that's used by the assembly routines when something goes bust.
|
---|
952 | *
|
---|
953 | * @param pData Pointer to this structure.
|
---|
954 | * @param u64NanoTS The calculated nano ts.
|
---|
955 | * @param u64DeltaPrev The delta relative to the previously returned timestamp.
|
---|
956 | * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
|
---|
957 | */
|
---|
958 | DECLCALLBACKMEMBER(void, pfnBad)(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
|
---|
959 |
|
---|
960 | /**
|
---|
961 | * Callback for when rediscovery is required.
|
---|
962 | *
|
---|
963 | * @returns Nanosecond timestamp.
|
---|
964 | * @param pData Pointer to this structure.
|
---|
965 | */
|
---|
966 | DECLCALLBACKMEMBER(uint64_t, pfnRediscover)(PRTTIMENANOTSDATA pData);
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Callback for when some CPU index related stuff goes wrong.
|
---|
970 | *
|
---|
971 | * @returns Nanosecond timestamp.
|
---|
972 | * @param pData Pointer to this structure.
|
---|
973 | * @param idApic The APIC ID if available, otherwise (UINT16_MAX-1).
|
---|
974 | * @param iCpuSet The CPU set index if available, otherwise
|
---|
975 | * (UINT16_MAX-1).
|
---|
976 | * @param iGipCpu The GIP CPU array index if available, otherwise
|
---|
977 | * (UINT16_MAX-1).
|
---|
978 | */
|
---|
979 | DECLCALLBACKMEMBER(uint64_t, pfnBadCpuIndex)(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu);
|
---|
980 |
|
---|
981 | /** Number of 1ns steps because of overshooting the period. */
|
---|
982 | uint32_t c1nsSteps;
|
---|
983 | /** The number of times the interval expired (overflow). */
|
---|
984 | uint32_t cExpired;
|
---|
985 | /** Number of "bad" previous values. */
|
---|
986 | uint32_t cBadPrev;
|
---|
987 | /** The number of update races. */
|
---|
988 | uint32_t cUpdateRaces;
|
---|
989 | } RTTIMENANOTSDATA;
|
---|
990 |
|
---|
991 | #ifndef IN_RING3
|
---|
992 | /**
|
---|
993 | * The Ring-3 layout of the RTTIMENANOTSDATA structure.
|
---|
994 | */
|
---|
995 | typedef struct RTTIMENANOTSDATAR3
|
---|
996 | {
|
---|
997 | R3PTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
998 | DECLR3CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
999 | DECLR3CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1000 | DECLR3CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1001 | uint32_t c1nsSteps;
|
---|
1002 | uint32_t cExpired;
|
---|
1003 | uint32_t cBadPrev;
|
---|
1004 | uint32_t cUpdateRaces;
|
---|
1005 | } RTTIMENANOTSDATAR3;
|
---|
1006 | #else
|
---|
1007 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR3;
|
---|
1008 | #endif
|
---|
1009 |
|
---|
1010 | #ifndef IN_RING0
|
---|
1011 | /**
|
---|
1012 | * The Ring-3 layout of the RTTIMENANOTSDATA structure.
|
---|
1013 | */
|
---|
1014 | typedef struct RTTIMENANOTSDATAR0
|
---|
1015 | {
|
---|
1016 | R0PTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
1017 | DECLR0CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
1018 | DECLR0CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1019 | DECLR0CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1020 | uint32_t c1nsSteps;
|
---|
1021 | uint32_t cExpired;
|
---|
1022 | uint32_t cBadPrev;
|
---|
1023 | uint32_t cUpdateRaces;
|
---|
1024 | } RTTIMENANOTSDATAR0;
|
---|
1025 | #else
|
---|
1026 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR0;
|
---|
1027 | #endif
|
---|
1028 |
|
---|
1029 | #ifndef IN_RC
|
---|
1030 | /**
|
---|
1031 | * The RC layout of the RTTIMENANOTSDATA structure.
|
---|
1032 | */
|
---|
1033 | typedef struct RTTIMENANOTSDATARC
|
---|
1034 | {
|
---|
1035 | RCPTRTYPE(uint64_t volatile *) pu64Prev;
|
---|
1036 | DECLRCCALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
|
---|
1037 | DECLRCCALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
|
---|
1038 | DECLRCCALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
|
---|
1039 | uint32_t c1nsSteps;
|
---|
1040 | uint32_t cExpired;
|
---|
1041 | uint32_t cBadPrev;
|
---|
1042 | uint32_t cUpdateRaces;
|
---|
1043 | } RTTIMENANOTSDATARC;
|
---|
1044 | #else
|
---|
1045 | typedef RTTIMENANOTSDATA RTTIMENANOTSDATARC;
|
---|
1046 | #endif
|
---|
1047 |
|
---|
1048 | /** Internal RTTimeNanoTS worker (assembly). */
|
---|
1049 | typedef DECLCALLBACK(uint64_t) FNTIMENANOTSINTERNAL(PRTTIMENANOTSDATA pData);
|
---|
1050 | /** Pointer to an internal RTTimeNanoTS worker (assembly). */
|
---|
1051 | typedef FNTIMENANOTSINTERNAL *PFNTIMENANOTSINTERNAL;
|
---|
1052 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarNoDelta(PRTTIMENANOTSDATA pData);
|
---|
1053 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarNoDelta(PRTTIMENANOTSDATA pData);
|
---|
1054 | #ifdef IN_RING3
|
---|
1055 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1056 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1057 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1058 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1059 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData);
|
---|
1060 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1061 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1062 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1063 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1064 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1065 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1066 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1067 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1068 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1069 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1070 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData);
|
---|
1071 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1072 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
|
---|
1073 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt0B(PRTTIMENANOTSDATA pData);
|
---|
1074 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt8000001E(PRTTIMENANOTSDATA pData);
|
---|
1075 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
|
---|
1076 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
|
---|
1077 | #else
|
---|
1078 | RTDECL(uint64_t) RTTimeNanoTSLegacyAsync(PRTTIMENANOTSDATA pData);
|
---|
1079 | RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDelta(PRTTIMENANOTSDATA pData);
|
---|
1080 | RTDECL(uint64_t) RTTimeNanoTSLFenceAsync(PRTTIMENANOTSDATA pData);
|
---|
1081 | RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDelta(PRTTIMENANOTSDATA pData);
|
---|
1082 | #endif
|
---|
1083 |
|
---|
1084 | /** @} */
|
---|
1085 |
|
---|
1086 |
|
---|
1087 | /**
|
---|
1088 | * Gets the current nanosecond timestamp.
|
---|
1089 | *
|
---|
1090 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
1091 | * resolution or performance optimizations.
|
---|
1092 | *
|
---|
1093 | * @returns nanosecond timestamp.
|
---|
1094 | */
|
---|
1095 | RTDECL(uint64_t) RTTimeSystemNanoTS(void);
|
---|
1096 |
|
---|
1097 | /**
|
---|
1098 | * Gets the current millisecond timestamp.
|
---|
1099 | *
|
---|
1100 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
1101 | * resolution or performance optimizations.
|
---|
1102 | *
|
---|
1103 | * @returns millisecond timestamp.
|
---|
1104 | */
|
---|
1105 | RTDECL(uint64_t) RTTimeSystemMilliTS(void);
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Get the nanosecond timestamp relative to program startup.
|
---|
1109 | *
|
---|
1110 | * @returns Timestamp relative to program startup.
|
---|
1111 | */
|
---|
1112 | RTDECL(uint64_t) RTTimeProgramNanoTS(void);
|
---|
1113 |
|
---|
1114 | /**
|
---|
1115 | * Get the microsecond timestamp relative to program startup.
|
---|
1116 | *
|
---|
1117 | * @returns Timestamp relative to program startup.
|
---|
1118 | */
|
---|
1119 | RTDECL(uint64_t) RTTimeProgramMicroTS(void);
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * Get the millisecond timestamp relative to program startup.
|
---|
1123 | *
|
---|
1124 | * @returns Timestamp relative to program startup.
|
---|
1125 | */
|
---|
1126 | RTDECL(uint64_t) RTTimeProgramMilliTS(void);
|
---|
1127 |
|
---|
1128 | /**
|
---|
1129 | * Get the second timestamp relative to program startup.
|
---|
1130 | *
|
---|
1131 | * @returns Timestamp relative to program startup.
|
---|
1132 | */
|
---|
1133 | RTDECL(uint32_t) RTTimeProgramSecTS(void);
|
---|
1134 |
|
---|
1135 | /**
|
---|
1136 | * Get the RTTimeNanoTS() of when the program started.
|
---|
1137 | *
|
---|
1138 | * @returns Program startup timestamp.
|
---|
1139 | */
|
---|
1140 | RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
|
---|
1141 |
|
---|
1142 |
|
---|
1143 | /**
|
---|
1144 | * Time zone information.
|
---|
1145 | */
|
---|
1146 | typedef struct RTTIMEZONEINFO
|
---|
1147 | {
|
---|
1148 | /** Unix time zone name (continent/country[/city]|). */
|
---|
1149 | const char *pszUnixName;
|
---|
1150 | /** Windows time zone name. */
|
---|
1151 | const char *pszWindowsName;
|
---|
1152 | /** The length of the unix time zone name. */
|
---|
1153 | uint8_t cchUnixName;
|
---|
1154 | /** The length of the windows time zone name. */
|
---|
1155 | uint8_t cchWindowsName;
|
---|
1156 | /** Two letter country/territory code if applicable, otherwise 'ZZ'. */
|
---|
1157 | char szCountry[3];
|
---|
1158 | /** Two letter windows country/territory code if applicable.
|
---|
1159 | * Empty string if no windows mapping. */
|
---|
1160 | char szWindowsCountry[3];
|
---|
1161 | #if 0 /* Add when needed and it's been extracted. */
|
---|
1162 | /** The standard delta in minutes (add to UTC). */
|
---|
1163 | int16_t cMinStdDelta;
|
---|
1164 | /** The daylight saving time delta in minutes (add to UTC). */
|
---|
1165 | int16_t cMinDstDelta;
|
---|
1166 | #endif
|
---|
1167 | /** closest matching windows time zone index. */
|
---|
1168 | uint32_t idxWindows;
|
---|
1169 | /** Flags, RTTIMEZONEINFO_F_XXX. */
|
---|
1170 | uint32_t fFlags;
|
---|
1171 | } RTTIMEZONEINFO;
|
---|
1172 | /** Pointer to time zone info. */
|
---|
1173 | typedef RTTIMEZONEINFO const *PCRTTIMEZONEINFO;
|
---|
1174 |
|
---|
1175 | /** @name RTTIMEZONEINFO_F_XXX - time zone info flags.
|
---|
1176 | * @{ */
|
---|
1177 | /** Indicates golden mapping entry for a windows time zone name. */
|
---|
1178 | #define RTTIMEZONEINFO_F_GOLDEN RT_BIT_32(0)
|
---|
1179 | /** @} */
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * Looks up static time zone information by unix name.
|
---|
1183 | *
|
---|
1184 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1185 | * @param pszName The unix zone name (TZ).
|
---|
1186 | */
|
---|
1187 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByUnixName(const char *pszName);
|
---|
1188 |
|
---|
1189 | /**
|
---|
1190 | * Looks up static time zone information by window name.
|
---|
1191 | *
|
---|
1192 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1193 | * @param pszName The windows zone name (reg key).
|
---|
1194 | */
|
---|
1195 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByWindowsName(const char *pszName);
|
---|
1196 |
|
---|
1197 | /**
|
---|
1198 | * Looks up static time zone information by windows index.
|
---|
1199 | *
|
---|
1200 | * @returns Pointer to info entry if found, NULL if not.
|
---|
1201 | * @param idxZone The windows timezone index.
|
---|
1202 | */
|
---|
1203 | RTDECL(PCRTTIMEZONEINFO) RTTimeZoneGetInfoByWindowsIndex(uint32_t idxZone);
|
---|
1204 |
|
---|
1205 | /**
|
---|
1206 | * Get the current time zone (TZ).
|
---|
1207 | *
|
---|
1208 | * @returns IPRT status code.
|
---|
1209 | * @param pszName Where to return the time zone name.
|
---|
1210 | * @param cbName The size of the name buffer.
|
---|
1211 | */
|
---|
1212 | RTDECL(int) RTTimeZoneGetCurrent(char *pszName, size_t cbName);
|
---|
1213 |
|
---|
1214 | /** @} */
|
---|
1215 |
|
---|
1216 | RT_C_DECLS_END
|
---|
1217 |
|
---|
1218 | #endif /* !IPRT_INCLUDED_time_h */
|
---|
1219 |
|
---|