6 * Copyright (C) 2006-2017 Oracle Corporation
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.
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.
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.
26 #ifndef ___iprt_time_h
27 #define ___iprt_time_h
29 #include <iprt/cdefs.h>
30 #include <iprt/types.h>
34 /** @defgroup grp_rt_time RTTime - Time
39 /** Time Specification.
41 * Use the inline RTTimeSpecGet/Set to operate on structure this so we
42 * can easily change the representation if required later.
44 * The current representation is in nanoseconds relative to the unix epoch
45 * (1970-01-01 00:00:00 UTC). This gives us an approximate span from
46 * 1678 to 2262 without sacrificing the resolution offered by the various
47 * host OSes (BSD & LINUX 1ns, NT 100ns).
49 typedef struct RTTIMESPEC
51 /** Nanoseconds since epoch.
52 * The name is intentially too long to be comfortable to use because you should be
53 * using inline helpers! */
54 int64_t i64NanosecondsRelativeToUnixEpoch
;
58 /** @name RTTIMESPEC methods
62 * Gets the time as nanoseconds relative to the unix epoch.
64 * @returns Nanoseconds relative to unix epoch.
65 * @param pTime The time spec to interpret.
67 DECLINLINE(int64_t) RTTimeSpecGetNano(PCRTTIMESPEC pTime
)
69 return pTime
->i64NanosecondsRelativeToUnixEpoch
;
74 * Sets the time give by nanoseconds relative to the unix epoch.
77 * @param pTime The time spec to modify.
78 * @param i64Nano The new time in nanoseconds.
80 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetNano(PRTTIMESPEC pTime
, int64_t i64Nano
)
82 pTime
->i64NanosecondsRelativeToUnixEpoch
= i64Nano
;
88 * Gets the time as microseconds relative to the unix epoch.
90 * @returns microseconds relative to unix epoch.
91 * @param pTime The time spec to interpret.
93 DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime
)
95 return pTime
->i64NanosecondsRelativeToUnixEpoch
/ RT_NS_1US
;
100 * Sets the time given by microseconds relative to the unix epoch.
103 * @param pTime The time spec to modify.
104 * @param i64Micro The new time in microsecond.
106 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetMicro(PRTTIMESPEC pTime
, int64_t i64Micro
)
108 pTime
->i64NanosecondsRelativeToUnixEpoch
= i64Micro
* RT_NS_1US
;
114 * Gets the time as milliseconds relative to the unix epoch.
116 * @returns milliseconds relative to unix epoch.
117 * @param pTime The time spec to interpret.
119 DECLINLINE(int64_t) RTTimeSpecGetMilli(PCRTTIMESPEC pTime
)
121 return pTime
->i64NanosecondsRelativeToUnixEpoch
/ RT_NS_1MS
;
126 * Sets the time given by milliseconds relative to the unix epoch.
129 * @param pTime The time spec to modify.
130 * @param i64Milli The new time in milliseconds.
132 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetMilli(PRTTIMESPEC pTime
, int64_t i64Milli
)
134 pTime
->i64NanosecondsRelativeToUnixEpoch
= i64Milli
* RT_NS_1MS
;
140 * Gets the time as seconds relative to the unix epoch.
142 * @returns seconds relative to unix epoch.
143 * @param pTime The time spec to interpret.
145 DECLINLINE(int64_t) RTTimeSpecGetSeconds(PCRTTIMESPEC pTime
)
147 return pTime
->i64NanosecondsRelativeToUnixEpoch
/ RT_NS_1SEC
;
152 * Sets the time given by seconds relative to the unix epoch.
155 * @param pTime The time spec to modify.
156 * @param i64Seconds The new time in seconds.
158 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetSeconds(PRTTIMESPEC pTime
, int64_t i64Seconds
)
160 pTime
->i64NanosecondsRelativeToUnixEpoch
= i64Seconds
* RT_NS_1SEC
;
166 * Makes the time spec absolute like abs() does (i.e. a positive value).
169 * @param pTime The time spec to modify.
171 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAbsolute(PRTTIMESPEC pTime
)
173 if (pTime
->i64NanosecondsRelativeToUnixEpoch
< 0)
174 pTime
->i64NanosecondsRelativeToUnixEpoch
= -pTime
->i64NanosecondsRelativeToUnixEpoch
;
183 * @param pTime The time spec to modify.
185 DECLINLINE(PRTTIMESPEC
) RTTimeSpecNegate(PRTTIMESPEC pTime
)
187 pTime
->i64NanosecondsRelativeToUnixEpoch
= -pTime
->i64NanosecondsRelativeToUnixEpoch
;
193 * Adds a time period to the time.
196 * @param pTime The time spec to modify.
197 * @param pTimeAdd The time spec to add to pTime.
199 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAdd(PRTTIMESPEC pTime
, PCRTTIMESPEC pTimeAdd
)
201 pTime
->i64NanosecondsRelativeToUnixEpoch
+= pTimeAdd
->i64NanosecondsRelativeToUnixEpoch
;
207 * Adds a time period give as nanoseconds from the time.
210 * @param pTime The time spec to modify.
211 * @param i64Nano The time period in nanoseconds.
213 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAddNano(PRTTIMESPEC pTime
, int64_t i64Nano
)
215 pTime
->i64NanosecondsRelativeToUnixEpoch
+= i64Nano
;
221 * Adds a time period give as microseconds from the time.
224 * @param pTime The time spec to modify.
225 * @param i64Micro The time period in microseconds.
227 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAddMicro(PRTTIMESPEC pTime
, int64_t i64Micro
)
229 pTime
->i64NanosecondsRelativeToUnixEpoch
+= i64Micro
* RT_NS_1US
;
235 * Adds a time period give as milliseconds from the time.
238 * @param pTime The time spec to modify.
239 * @param i64Milli The time period in milliseconds.
241 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAddMilli(PRTTIMESPEC pTime
, int64_t i64Milli
)
243 pTime
->i64NanosecondsRelativeToUnixEpoch
+= i64Milli
* RT_NS_1MS
;
249 * Adds a time period give as seconds from the time.
252 * @param pTime The time spec to modify.
253 * @param i64Seconds The time period in seconds.
255 DECLINLINE(PRTTIMESPEC
) RTTimeSpecAddSeconds(PRTTIMESPEC pTime
, int64_t i64Seconds
)
257 pTime
->i64NanosecondsRelativeToUnixEpoch
+= i64Seconds
* RT_NS_1SEC
;
263 * Subtracts a time period from the time.
266 * @param pTime The time spec to modify.
267 * @param pTimeSub The time spec to subtract from pTime.
269 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSub(PRTTIMESPEC pTime
, PCRTTIMESPEC pTimeSub
)
271 pTime
->i64NanosecondsRelativeToUnixEpoch
-= pTimeSub
->i64NanosecondsRelativeToUnixEpoch
;
277 * Subtracts a time period give as nanoseconds from the time.
280 * @param pTime The time spec to modify.
281 * @param i64Nano The time period in nanoseconds.
283 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSubNano(PRTTIMESPEC pTime
, int64_t i64Nano
)
285 pTime
->i64NanosecondsRelativeToUnixEpoch
-= i64Nano
;
291 * Subtracts a time period give as microseconds from the time.
294 * @param pTime The time spec to modify.
295 * @param i64Micro The time period in microseconds.
297 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSubMicro(PRTTIMESPEC pTime
, int64_t i64Micro
)
299 pTime
->i64NanosecondsRelativeToUnixEpoch
-= i64Micro
* RT_NS_1US
;
305 * Subtracts a time period give as milliseconds from the time.
308 * @param pTime The time spec to modify.
309 * @param i64Milli The time period in milliseconds.
311 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSubMilli(PRTTIMESPEC pTime
, int64_t i64Milli
)
313 pTime
->i64NanosecondsRelativeToUnixEpoch
-= i64Milli
* RT_NS_1MS
;
319 * Subtracts a time period give as seconds from the time.
322 * @param pTime The time spec to modify.
323 * @param i64Seconds The time period in seconds.
325 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSubSeconds(PRTTIMESPEC pTime
, int64_t i64Seconds
)
327 pTime
->i64NanosecondsRelativeToUnixEpoch
-= i64Seconds
* RT_NS_1SEC
;
333 * Gives the time in seconds and nanoseconds.
336 * @param pTime The time spec to interpret.
337 * @param *pi32Seconds Where to store the time period in seconds.
338 * @param *pi32Nano Where to store the time period in nanoseconds.
340 DECLINLINE(void) RTTimeSpecGetSecondsAndNano(PRTTIMESPEC pTime
, int32_t *pi32Seconds
, int32_t *pi32Nano
)
342 int64_t i64
= RTTimeSpecGetNano(pTime
);
343 int32_t i32Nano
= (int32_t)(i64
% RT_NS_1SEC
);
347 i32Nano
+= RT_NS_1SEC
;
350 *pi32Seconds
= (int32_t)i64
;
355 /* PORTME: Add struct timeval guard macro here. */
356 #if defined(RTTIME_INCL_TIMEVAL) || defined(_STRUCT_TIMEVAL) || defined(_SYS__TIMEVAL_H_) || defined(_SYS_TIME_H) || defined(_TIMEVAL) || defined(_LINUX_TIME_H) \
357 || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_))
359 * Gets the time as POSIX timeval.
362 * @param pTime The time spec to interpret.
363 * @param pTimeval Where to store the time as POSIX timeval.
365 DECLINLINE(struct timeval
*) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime
, struct timeval
*pTimeval
)
367 int64_t i64
= RTTimeSpecGetMicro(pTime
);
368 int32_t i32Micro
= (int32_t)(i64
% RT_US_1SEC
);
372 i32Micro
+= RT_US_1SEC
;
375 pTimeval
->tv_sec
= (time_t)i64
;
376 pTimeval
->tv_usec
= i32Micro
;
381 * Sets the time as POSIX timeval.
384 * @param pTime The time spec to modify.
385 * @param pTimeval Pointer to the POSIX timeval struct with the new time.
387 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetTimeval(PRTTIMESPEC pTime
, const struct timeval
*pTimeval
)
389 return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime
, pTimeval
->tv_sec
), pTimeval
->tv_usec
);
391 #endif /* various ways of detecting struct timeval */
394 /* PORTME: Add struct timespec guard macro here. */
395 #if defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(_SYS__TIMESPEC_H_) || defined(TIMEVAL_TO_TIMESPEC) || defined(_TIMESPEC) \
396 || (defined(RT_OS_NETBSD) && defined(_SYS_TIME_H_))
398 * Gets the time as POSIX timespec.
401 * @param pTime The time spec to interpret.
402 * @param pTimespec Where to store the time as POSIX timespec.
404 DECLINLINE(struct timespec
*) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime
, struct timespec
*pTimespec
)
406 int64_t i64
= RTTimeSpecGetNano(pTime
);
407 int32_t i32Nano
= (int32_t)(i64
% RT_NS_1SEC
);
411 i32Nano
+= RT_NS_1SEC
;
414 pTimespec
->tv_sec
= (time_t)i64
;
415 pTimespec
->tv_nsec
= i32Nano
;
420 * Sets the time as POSIX timespec.
423 * @param pTime The time spec to modify.
424 * @param pTimespec Pointer to the POSIX timespec struct with the new time.
426 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetTimespec(PRTTIMESPEC pTime
, const struct timespec
*pTimespec
)
428 return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime
, pTimespec
->tv_sec
), pTimespec
->tv_nsec
);
430 #endif /* various ways of detecting struct timespec */
434 /** The offset of the unix epoch and the base for NT time (in 100ns units).
435 * Nt time starts at 1601-01-01 00:00:00. */
436 #define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
440 * Gets the time as NT time.
443 * @param pTime The time spec to interpret.
445 DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime
)
447 return pTime
->i64NanosecondsRelativeToUnixEpoch
/ 100
448 + RTTIME_NT_TIME_OFFSET_UNIX
;
453 * Sets the time given by Nt time.
456 * @param pTime The time spec to modify.
457 * @param u64NtTime The new time in Nt time.
459 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetNtTime(PRTTIMESPEC pTime
, uint64_t u64NtTime
)
461 pTime
->i64NanosecondsRelativeToUnixEpoch
=
462 ((int64_t)u64NtTime
- RTTIME_NT_TIME_OFFSET_UNIX
) * 100;
469 * Gets the time as NT file time.
471 * @returns pFileTime.
472 * @param pTime The time spec to interpret.
473 * @param pFileTime Pointer to NT filetime structure.
475 DECLINLINE(PFILETIME
) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime
, PFILETIME pFileTime
)
477 *((uint64_t *)pFileTime
) = RTTimeSpecGetNtTime(pTime
);
482 * Sets the time as NT file time.
485 * @param pTime The time spec to modify.
486 * @param pFileTime Where to store the time as Nt file time.
488 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime
, const FILETIME
*pFileTime
)
490 return RTTimeSpecSetNtTime(pTime
, *(const uint64_t *)pFileTime
);
495 /** The offset to the start of DOS time.
496 * DOS time starts 1980-01-01 00:00:00. */
497 #define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
501 * Gets the time as seconds relative to the start of dos time.
503 * @returns seconds relative to the start of dos time.
504 * @param pTime The time spec to interpret.
506 DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime
)
508 return (pTime
->i64NanosecondsRelativeToUnixEpoch
- RTTIME_OFFSET_DOS_TIME
)
514 * Sets the time given by seconds relative to the start of dos time.
517 * @param pTime The time spec to modify.
518 * @param i64Seconds The new time in seconds relative to the start of dos time.
520 DECLINLINE(PRTTIMESPEC
) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime
, int64_t i64Seconds
)
522 pTime
->i64NanosecondsRelativeToUnixEpoch
= i64Seconds
* RT_NS_1SEC
523 + RTTIME_OFFSET_DOS_TIME
;
529 * Compare two time specs.
531 * @returns true they are equal.
532 * @returns false they are not equal.
533 * @param pTime1 The 1st time spec.
534 * @param pTime2 The 2nd time spec.
536 DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1
, PCRTTIMESPEC pTime2
)
538 return pTime1
->i64NanosecondsRelativeToUnixEpoch
== pTime2
->i64NanosecondsRelativeToUnixEpoch
;
543 * Compare two time specs.
545 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
546 * @returns false they are not equal.
547 * @param pLeft The 1st time spec.
548 * @param pRight The 2nd time spec.
550 DECLINLINE(int) RTTimeSpecCompare(PCRTTIMESPEC pLeft
, PCRTTIMESPEC pRight
)
552 if (pLeft
->i64NanosecondsRelativeToUnixEpoch
== pRight
->i64NanosecondsRelativeToUnixEpoch
)
554 return pLeft
->i64NanosecondsRelativeToUnixEpoch
< pRight
->i64NanosecondsRelativeToUnixEpoch
? -1 : 1;
559 * Converts a time spec to a ISO date string.
561 * @returns psz on success.
562 * @returns NULL on buffer underflow.
563 * @param pTime The time spec.
564 * @param psz Where to store the string.
565 * @param cb The size of the buffer.
567 RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime
, char *psz
, size_t cb
);
570 * Attempts to convert an ISO date string to a time structure.
572 * We're a little forgiving with zero padding, unspecified parts, and leading
573 * and trailing spaces.
575 * @retval pTime on success,
576 * @retval NULL on failure.
577 * @param pTime The time spec.
578 * @param pszString The ISO date string to convert.
580 RTDECL(PRTTIMESPEC
) RTTimeSpecFromString(PRTTIMESPEC pTime
, const char *pszString
);
589 typedef struct RTTIME
591 /** The year number. */
593 /** The month of the year (1-12). January is 1. */
595 /** The day of the week (0-6). Monday is 0. */
597 /** The day of the year (1-366). January the 1st is 1. */
599 /** The day of the month (1-31). */
601 /** Hour of the day (0-23). */
603 /** The minute of the hour (0-59). */
605 /** The second of the minute (0-60).
606 * (u32Nanosecond / 1000000) */
608 /** The nanoseconds of the second (0-999999999). */
609 uint32_t u32Nanosecond
;
610 /** Flags, of the RTTIME_FLAGS_* \#defines. */
612 /** UCT time offset in minutes (-840-840).
613 * @remarks The implementation of RTTimeLocal* isn't quite there yet, so this might not be 100% correct. */
617 /** Pointer to a exploded time structure. */
618 typedef RTTIME
*PRTTIME
;
619 /** Pointer to a const exploded time structure. */
620 typedef const RTTIME
*PCRTTIME
;
622 /** @name RTTIME::fFlags values.
624 /** Set if the time is UTC. If clear the time local time. */
625 #define RTTIME_FLAGS_TYPE_MASK 3
626 /** the time is UTC time. */
627 #define RTTIME_FLAGS_TYPE_UTC 2
628 /** The time is local time. */
629 #define RTTIME_FLAGS_TYPE_LOCAL 3
631 /** Set if the time is local and daylight saving time is in effect.
632 * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
633 #define RTTIME_FLAGS_DST RT_BIT(4)
634 /** Set if the time is local and there is no data available on daylight saving time. */
635 #define RTTIME_FLAGS_NO_DST_DATA RT_BIT(5)
636 /** Set if the year is a leap year.
637 * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
638 #define RTTIME_FLAGS_LEAP_YEAR RT_BIT(6)
639 /** Set if the year is a common year.
640 * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
641 #define RTTIME_FLAGS_COMMON_YEAR RT_BIT(7)
642 /** The mask of valid flags. */
643 #define RTTIME_FLAGS_MASK UINT32_C(0xff)
648 * Gets the current system time (UTC).
651 * @param pTime Where to store the time.
653 RTDECL(PRTTIMESPEC
) RTTimeNow(PRTTIMESPEC pTime
);
656 * Sets the system time.
658 * @returns IPRT status code
659 * @param pTime The new system time (UTC).
661 * @remarks This will usually fail because changing the wall time is usually
662 * requires extra privileges.
664 RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime
);
667 * Explodes a time spec (UTC).
670 * @param pTime Where to store the exploded time.
671 * @param pTimeSpec The time spec to exploded.
673 RTDECL(PRTTIME
) RTTimeExplode(PRTTIME pTime
, PCRTTIMESPEC pTimeSpec
);
676 * Implodes exploded time to a time spec (UTC).
678 * @returns pTime on success.
679 * @returns NULL if the pTime data is invalid.
680 * @param pTimeSpec Where to store the imploded UTC time.
681 * If pTime specifies a time which outside the range, maximum or
682 * minimum values will be returned.
683 * @param pTime Pointer to the exploded time to implode.
684 * The fields u8Month, u8WeekDay and u8MonthDay are not used,
685 * and all the other fields are expected to be within their
686 * bounds. Use RTTimeNormalize() to calculate u16YearDay and
687 * normalize the ranges of the fields.
689 RTDECL(PRTTIMESPEC
) RTTimeImplode(PRTTIMESPEC pTimeSpec
, PCRTTIME pTime
);
692 * Normalizes the fields of a time structure.
694 * It is possible to calculate year-day from month/day and vice
695 * versa. If you adjust any of of these, make sure to zero the
696 * other so you make it clear which of the fields to use. If
697 * it's ambiguous, the year-day field is used (and you get
698 * assertions in debug builds).
700 * All the time fields and the year-day or month/day fields will
701 * be adjusted for overflows. (Since all fields are unsigned, there
702 * is no underflows.) It is possible to exploit this for simple
703 * date math, though the recommended way of doing that to implode
704 * the time into a timespec and do the math on that.
706 * @returns pTime on success.
707 * @returns NULL if the data is invalid.
709 * @param pTime The time structure to normalize.
711 * @remarks This function doesn't work with local time, only with UTC time.
713 RTDECL(PRTTIME
) RTTimeNormalize(PRTTIME pTime
);
716 * Gets the current local system time.
719 * @param pTime Where to store the local time.
721 RTDECL(PRTTIMESPEC
) RTTimeLocalNow(PRTTIMESPEC pTime
);
724 * Gets the delta between UTC and local time.
727 * RTTIMESPEC LocalTime;
728 * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
731 * @returns Returns the nanosecond delta between UTC and local time.
733 RTDECL(int64_t) RTTimeLocalDeltaNano(void);
736 * Explodes a time spec to the localized timezone.
739 * @param pTime Where to store the exploded time.
740 * @param pTimeSpec The time spec to exploded (UTC).
742 RTDECL(PRTTIME
) RTTimeLocalExplode(PRTTIME pTime
, PCRTTIMESPEC pTimeSpec
);
745 * Normalizes the fields of a time structure containing local time.
747 * See RTTimeNormalize for details.
749 * @returns pTime on success.
750 * @returns NULL if the data is invalid.
751 * @param pTime The time structure to normalize.
753 RTDECL(PRTTIME
) RTTimeLocalNormalize(PRTTIME pTime
);
756 * Converts a time spec to a ISO date string.
758 * @returns psz on success.
759 * @returns NULL on buffer underflow.
760 * @param pTime The time. Caller should've normalized this.
761 * @param psz Where to store the string.
762 * @param cb The size of the buffer.
764 RTDECL(char *) RTTimeToString(PCRTTIME pTime
, char *psz
, size_t cb
);
767 * Attempts to convert an ISO date string to a time structure.
769 * We're a little forgiving with zero padding, unspecified parts, and leading
770 * and trailing spaces.
772 * @retval pTime on success,
773 * @retval NULL on failure.
774 * @param pTime Where to store the time on success.
775 * @param pszString The ISO date string to convert.
777 RTDECL(PRTTIME
) RTTimeFromString(PRTTIME pTime
, const char *pszString
);
780 * Checks if a year is a leap year or not.
782 * @returns true if it's a leap year.
783 * @returns false if it's a common year.
784 * @param i32Year The year in question.
786 RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year
);
789 * Compares two normalized time structures.
791 * @retval 0 if equal.
792 * @retval -1 if @a pLeft is earlier than @a pRight.
793 * @retval 1 if @a pRight is earlier than @a pLeft.
795 * @param pLeft The left side time. NULL is accepted.
796 * @param pRight The right side time. NULL is accepted.
798 * @note A NULL time is considered smaller than anything else. If both are
799 * NULL, they are considered equal.
801 RTDECL(int) RTTimeCompare(PCRTTIME pLeft
, PCRTTIME pRight
);
804 * Gets the current nanosecond timestamp.
806 * @returns nanosecond timestamp.
808 RTDECL(uint64_t) RTTimeNanoTS(void);
811 * Gets the current millisecond timestamp.
813 * @returns millisecond timestamp.
815 RTDECL(uint64_t) RTTimeMilliTS(void);
818 * Debugging the time api.
820 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
822 RTDECL(uint32_t) RTTimeDbgSteps(void);
825 * Debugging the time api.
827 * @returns the number of times the TSC interval expired RTTimeNanoTS().
829 RTDECL(uint32_t) RTTimeDbgExpired(void);
832 * Debugging the time api.
834 * @returns the number of bad previous values encountered by RTTimeNanoTS().
836 RTDECL(uint32_t) RTTimeDbgBad(void);
839 * Debugging the time api.
841 * @returns the number of update races in RTTimeNanoTS().
843 RTDECL(uint32_t) RTTimeDbgRaces(void);
845 /** @name RTTimeNanoTS GIP worker functions, for TM.
847 /** Pointer to a RTTIMENANOTSDATA structure. */
848 typedef struct RTTIMENANOTSDATA
*PRTTIMENANOTSDATA
;
851 * Nanosecond timestamp data.
853 * This is used to keep track of statistics and callback so IPRT
854 * and TM (VirtualBox) can share code.
856 * @remark Keep this in sync with the assembly version in timesupA.asm.
858 typedef struct RTTIMENANOTSDATA
860 /** Where the previous timestamp is stored.
861 * This is maintained to ensure that time doesn't go backwards or anything. */
862 uint64_t volatile *pu64Prev
;
865 * Helper function that's used by the assembly routines when something goes bust.
867 * @param pData Pointer to this structure.
868 * @param u64NanoTS The calculated nano ts.
869 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
870 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
872 DECLCALLBACKMEMBER(void, pfnBad
)(PRTTIMENANOTSDATA pData
, uint64_t u64NanoTS
, uint64_t u64DeltaPrev
, uint64_t u64PrevNanoTS
);
875 * Callback for when rediscovery is required.
877 * @returns Nanosecond timestamp.
878 * @param pData Pointer to this structure.
880 DECLCALLBACKMEMBER(uint64_t, pfnRediscover
)(PRTTIMENANOTSDATA pData
);
883 * Callback for when some CPU index related stuff goes wrong.
885 * @returns Nanosecond timestamp.
886 * @param pData Pointer to this structure.
887 * @param idApic The APIC ID if available, otherwise (UINT16_MAX-1).
888 * @param iCpuSet The CPU set index if available, otherwise
890 * @param iGipCpu The GIP CPU array index if available, otherwise
893 DECLCALLBACKMEMBER(uint64_t, pfnBadCpuIndex
)(PRTTIMENANOTSDATA pData
, uint16_t idApic
, uint16_t iCpuSet
, uint16_t iGipCpu
);
895 /** Number of 1ns steps because of overshooting the period. */
897 /** The number of times the interval expired (overflow). */
899 /** Number of "bad" previous values. */
901 /** The number of update races. */
902 uint32_t cUpdateRaces
;
907 * The Ring-3 layout of the RTTIMENANOTSDATA structure.
909 typedef struct RTTIMENANOTSDATAR3
911 R3PTRTYPE(uint64_t volatile *) pu64Prev
;
912 DECLR3CALLBACKMEMBER(void, pfnBad
,(PRTTIMENANOTSDATA pData
, uint64_t u64NanoTS
, uint64_t u64DeltaPrev
, uint64_t u64PrevNanoTS
));
913 DECLR3CALLBACKMEMBER(uint64_t, pfnRediscover
,(PRTTIMENANOTSDATA pData
));
914 DECLR3CALLBACKMEMBER(uint64_t, pfnBadCpuIndex
,(PRTTIMENANOTSDATA pData
, uint16_t idApic
, uint16_t iCpuSet
, uint16_t iGipCpu
));
918 uint32_t cUpdateRaces
;
919 } RTTIMENANOTSDATAR3
;
921 typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR3
;
926 * The Ring-3 layout of the RTTIMENANOTSDATA structure.
928 typedef struct RTTIMENANOTSDATAR0
930 R0PTRTYPE(uint64_t volatile *) pu64Prev
;
931 DECLR0CALLBACKMEMBER(void, pfnBad
,(PRTTIMENANOTSDATA pData
, uint64_t u64NanoTS
, uint64_t u64DeltaPrev
, uint64_t u64PrevNanoTS
));
932 DECLR0CALLBACKMEMBER(uint64_t, pfnRediscover
,(PRTTIMENANOTSDATA pData
));
933 DECLR0CALLBACKMEMBER(uint64_t, pfnBadCpuIndex
,(PRTTIMENANOTSDATA pData
, uint16_t idApic
, uint16_t iCpuSet
, uint16_t iGipCpu
));
937 uint32_t cUpdateRaces
;
938 } RTTIMENANOTSDATAR0
;
940 typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR0
;
945 * The RC layout of the RTTIMENANOTSDATA structure.
947 typedef struct RTTIMENANOTSDATARC
949 RCPTRTYPE(uint64_t volatile *) pu64Prev
;
950 DECLRCCALLBACKMEMBER(void, pfnBad
,(PRTTIMENANOTSDATA pData
, uint64_t u64NanoTS
, uint64_t u64DeltaPrev
, uint64_t u64PrevNanoTS
));
951 DECLRCCALLBACKMEMBER(uint64_t, pfnRediscover
,(PRTTIMENANOTSDATA pData
));
952 DECLRCCALLBACKMEMBER(uint64_t, pfnBadCpuIndex
,(PRTTIMENANOTSDATA pData
, uint16_t idApic
, uint16_t iCpuSet
, uint16_t iGipCpu
));
956 uint32_t cUpdateRaces
;
957 } RTTIMENANOTSDATARC
;
959 typedef RTTIMENANOTSDATA RTTIMENANOTSDATARC
;
962 /** Internal RTTimeNanoTS worker (assembly). */
963 typedef DECLCALLBACK(uint64_t) FNTIMENANOTSINTERNAL(PRTTIMENANOTSDATA pData
);
964 /** Pointer to an internal RTTimeNanoTS worker (assembly). */
965 typedef FNTIMENANOTSINTERNAL
*PFNTIMENANOTSINTERNAL
;
966 RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarNoDelta(PRTTIMENANOTSDATA pData
);
967 RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarNoDelta(PRTTIMENANOTSDATA pData
);
969 RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicId(PRTTIMENANOTSDATA pData
);
970 RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscp(PRTTIMENANOTSDATA pData
);
971 RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData
);
972 RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseIdtrLim(PRTTIMENANOTSDATA pData
);
973 RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData
);
974 RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData
);
975 RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData
);
976 RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicId(PRTTIMENANOTSDATA pData
);
977 RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscp(PRTTIMENANOTSDATA pData
);
978 RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl(PRTTIMENANOTSDATA pData
);
979 RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseIdtrLim(PRTTIMENANOTSDATA pData
);
980 RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData
);
981 RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData
);
982 RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData
);
984 RTDECL(uint64_t) RTTimeNanoTSLegacyAsync(PRTTIMENANOTSDATA pData
);
985 RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDelta(PRTTIMENANOTSDATA pData
);
986 RTDECL(uint64_t) RTTimeNanoTSLFenceAsync(PRTTIMENANOTSDATA pData
);
987 RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDelta(PRTTIMENANOTSDATA pData
);
994 * Gets the current nanosecond timestamp.
996 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
997 * resolution or performance optimizations.
999 * @returns nanosecond timestamp.
1001 RTDECL(uint64_t) RTTimeSystemNanoTS(void);
1004 * Gets the current millisecond timestamp.
1006 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
1007 * resolution or performance optimizations.
1009 * @returns millisecond timestamp.
1011 RTDECL(uint64_t) RTTimeSystemMilliTS(void);
1014 * Get the nanosecond timestamp relative to program startup.
1016 * @returns Timestamp relative to program startup.
1018 RTDECL(uint64_t) RTTimeProgramNanoTS(void);
1021 * Get the microsecond timestamp relative to program startup.
1023 * @returns Timestamp relative to program startup.
1025 RTDECL(uint64_t) RTTimeProgramMicroTS(void);
1028 * Get the millisecond timestamp relative to program startup.
1030 * @returns Timestamp relative to program startup.
1032 RTDECL(uint64_t) RTTimeProgramMilliTS(void);
1035 * Get the second timestamp relative to program startup.
1037 * @returns Timestamp relative to program startup.
1039 RTDECL(uint32_t) RTTimeProgramSecTS(void);
1042 * Get the RTTimeNanoTS() of when the program started.
1044 * @returns Program startup timestamp.
1046 RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
1050 * Time zone information.
1052 typedef struct RTTIMEZONEINFO
1054 /** Unix time zone name (continent/country[/city]|). */
1055 const char *pszUnixName
;
1056 /** Windows time zone name. */
1057 const char *pszWindowsName
;
1058 /** The length of the unix time zone name. */
1059 uint8_t cchUnixName
;
1060 /** The length of the windows time zone name. */
1061 uint8_t cchWindowsName
;
1062 /** Two letter country/territory code if applicable, otherwise 'ZZ'. */
1064 /** Two letter windows country/territory code if applicable.
1065 * Empty string if no windows mapping. */
1066 char szWindowsCountry
[3];
1067 #if 0 /* Add when needed and it's been extracted. */
1068 /** The standard delta in minutes (add to UTC). */
1069 int16_t cMinStdDelta
;
1070 /** The daylight saving time delta in minutes (add to UTC). */
1071 int16_t cMinDstDelta
;
1073 /** closest matching windows time zone index. */
1074 uint32_t idxWindows
;
1075 /** Flags, RTTIMEZONEINFO_F_XXX. */
1078 /** Pointer to time zone info. */
1079 typedef RTTIMEZONEINFO
const *PCRTTIMEZONEINFO
;
1081 /** @name RTTIMEZONEINFO_F_XXX - time zone info flags.
1083 /** Indicates golden mapping entry for a windows time zone name. */
1084 #define RTTIMEZONEINFO_F_GOLDEN RT_BIT_32(0)
1088 * Looks up static time zone information by unix name.
1090 * @returns Pointer to info entry if found, NULL if not.
1091 * @param pszName The unix zone name (TZ).
1093 RTDECL(PCRTTIMEZONEINFO
) RTTimeZoneGetInfoByUnixName(const char *pszName
);
1096 * Looks up static time zone information by window name.
1098 * @returns Pointer to info entry if found, NULL if not.
1099 * @param pszName The windows zone name (reg key).
1101 RTDECL(PCRTTIMEZONEINFO
) RTTimeZoneGetInfoByWindowsName(const char *pszName
);
1104 * Looks up static time zone information by windows index.
1106 * @returns Pointer to info entry if found, NULL if not.
1107 * @param idxZone The windows timezone index.
1109 RTDECL(PCRTTIMEZONEINFO
) RTTimeZoneGetInfoByWindowsIndex(uint32_t idxZone
);
1112 * Get the current time zone (TZ).
1114 * @returns IPRT status code.
1115 * @param pszName Where to return the time zone name.
1116 * @param cbName The size of the name buffer.
1118 RTDECL(int) RTTimeZoneGetCurrent(char *pszName
, size_t cbName
);