VirtualBox

source: vbox/trunk/include/iprt/time.h@ 97894

Last change on this file since 97894 was 96622, checked in by vboxsync, 2 years ago

IPRT: Added RTTimeFormatDuration[Ex] for duration/interval formatting.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette