VirtualBox

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

Last change on this file since 62473 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.6 KB
Line 
1/** @file
2 * IPRT - Time.
3 */
4
5/*
6 * Copyright (C) 2006-2016 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_time_h
27#define ___iprt_time_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_time RTTime - Time
35 * @ingroup grp_rt
36 * @{
37 */
38
39/** Time Specification.
40 *
41 * Use the inline RTTimeSpecGet/Set to operate on structure this so we
42 * can easily change the representation if required later.
43 *
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).
48 */
49typedef struct RTTIMESPEC
50{
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;
55} RTTIMESPEC;
56
57
58/** @name RTTIMESPEC methods
59 * @{ */
60
61/**
62 * Gets the time as nanoseconds relative to the unix epoch.
63 *
64 * @returns Nanoseconds relative to unix epoch.
65 * @param pTime The time spec to interpret.
66 */
67DECLINLINE(int64_t) RTTimeSpecGetNano(PCRTTIMESPEC pTime)
68{
69 return pTime->i64NanosecondsRelativeToUnixEpoch;
70}
71
72
73/**
74 * Sets the time give by nanoseconds relative to the unix epoch.
75 *
76 * @returns pTime.
77 * @param pTime The time spec to modify.
78 * @param i64Nano The new time in nanoseconds.
79 */
80DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNano(PRTTIMESPEC pTime, int64_t i64Nano)
81{
82 pTime->i64NanosecondsRelativeToUnixEpoch = i64Nano;
83 return pTime;
84}
85
86
87/**
88 * Gets the time as microseconds relative to the unix epoch.
89 *
90 * @returns microseconds relative to unix epoch.
91 * @param pTime The time spec to interpret.
92 */
93DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime)
94{
95 return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1US;
96}
97
98
99/**
100 * Sets the time given by microseconds relative to the unix epoch.
101 *
102 * @returns pTime.
103 * @param pTime The time spec to modify.
104 * @param i64Micro The new time in microsecond.
105 */
106DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMicro(PRTTIMESPEC pTime, int64_t i64Micro)
107{
108 pTime->i64NanosecondsRelativeToUnixEpoch = i64Micro * RT_NS_1US;
109 return pTime;
110}
111
112
113/**
114 * Gets the time as milliseconds relative to the unix epoch.
115 *
116 * @returns milliseconds relative to unix epoch.
117 * @param pTime The time spec to interpret.
118 */
119DECLINLINE(int64_t) RTTimeSpecGetMilli(PCRTTIMESPEC pTime)
120{
121 return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1MS;
122}
123
124
125/**
126 * Sets the time given by milliseconds relative to the unix epoch.
127 *
128 * @returns pTime.
129 * @param pTime The time spec to modify.
130 * @param i64Milli The new time in milliseconds.
131 */
132DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMilli(PRTTIMESPEC pTime, int64_t i64Milli)
133{
134 pTime->i64NanosecondsRelativeToUnixEpoch = i64Milli * RT_NS_1MS;
135 return pTime;
136}
137
138
139/**
140 * Gets the time as seconds relative to the unix epoch.
141 *
142 * @returns seconds relative to unix epoch.
143 * @param pTime The time spec to interpret.
144 */
145DECLINLINE(int64_t) RTTimeSpecGetSeconds(PCRTTIMESPEC pTime)
146{
147 return pTime->i64NanosecondsRelativeToUnixEpoch / RT_NS_1SEC;
148}
149
150
151/**
152 * Sets the time given by seconds relative to the unix epoch.
153 *
154 * @returns pTime.
155 * @param pTime The time spec to modify.
156 * @param i64Seconds The new time in seconds.
157 */
158DECLINLINE(PRTTIMESPEC) RTTimeSpecSetSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
159{
160 pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * RT_NS_1SEC;
161 return pTime;
162}
163
164
165/**
166 * Makes the time spec absolute like abs() does (i.e. a positive value).
167 *
168 * @returns pTime.
169 * @param pTime The time spec to modify.
170 */
171DECLINLINE(PRTTIMESPEC) RTTimeSpecAbsolute(PRTTIMESPEC pTime)
172{
173 if (pTime->i64NanosecondsRelativeToUnixEpoch < 0)
174 pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
175 return pTime;
176}
177
178
179/**
180 * Negates the time.
181 *
182 * @returns pTime.
183 * @param pTime The time spec to modify.
184 */
185DECLINLINE(PRTTIMESPEC) RTTimeSpecNegate(PRTTIMESPEC pTime)
186{
187 pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
188 return pTime;
189}
190
191
192/**
193 * Adds a time period to the time.
194 *
195 * @returns pTime.
196 * @param pTime The time spec to modify.
197 * @param pTimeAdd The time spec to add to pTime.
198 */
199DECLINLINE(PRTTIMESPEC) RTTimeSpecAdd(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeAdd)
200{
201 pTime->i64NanosecondsRelativeToUnixEpoch += pTimeAdd->i64NanosecondsRelativeToUnixEpoch;
202 return pTime;
203}
204
205
206/**
207 * Adds a time period give as nanoseconds from the time.
208 *
209 * @returns pTime.
210 * @param pTime The time spec to modify.
211 * @param i64Nano The time period in nanoseconds.
212 */
213DECLINLINE(PRTTIMESPEC) RTTimeSpecAddNano(PRTTIMESPEC pTime, int64_t i64Nano)
214{
215 pTime->i64NanosecondsRelativeToUnixEpoch += i64Nano;
216 return pTime;
217}
218
219
220/**
221 * Adds a time period give as microseconds from the time.
222 *
223 * @returns pTime.
224 * @param pTime The time spec to modify.
225 * @param i64Micro The time period in microseconds.
226 */
227DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMicro(PRTTIMESPEC pTime, int64_t i64Micro)
228{
229 pTime->i64NanosecondsRelativeToUnixEpoch += i64Micro * RT_NS_1US;
230 return pTime;
231}
232
233
234/**
235 * Adds a time period give as milliseconds from the time.
236 *
237 * @returns pTime.
238 * @param pTime The time spec to modify.
239 * @param i64Milli The time period in milliseconds.
240 */
241DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMilli(PRTTIMESPEC pTime, int64_t i64Milli)
242{
243 pTime->i64NanosecondsRelativeToUnixEpoch += i64Milli * RT_NS_1MS;
244 return pTime;
245}
246
247
248/**
249 * Adds a time period give as seconds from the time.
250 *
251 * @returns pTime.
252 * @param pTime The time spec to modify.
253 * @param i64Seconds The time period in seconds.
254 */
255DECLINLINE(PRTTIMESPEC) RTTimeSpecAddSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
256{
257 pTime->i64NanosecondsRelativeToUnixEpoch += i64Seconds * RT_NS_1SEC;
258 return pTime;
259}
260
261
262/**
263 * Subtracts a time period from the time.
264 *
265 * @returns pTime.
266 * @param pTime The time spec to modify.
267 * @param pTimeSub The time spec to subtract from pTime.
268 */
269DECLINLINE(PRTTIMESPEC) RTTimeSpecSub(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeSub)
270{
271 pTime->i64NanosecondsRelativeToUnixEpoch -= pTimeSub->i64NanosecondsRelativeToUnixEpoch;
272 return pTime;
273}
274
275
276/**
277 * Subtracts a time period give as nanoseconds from the time.
278 *
279 * @returns pTime.
280 * @param pTime The time spec to modify.
281 * @param i64Nano The time period in nanoseconds.
282 */
283DECLINLINE(PRTTIMESPEC) RTTimeSpecSubNano(PRTTIMESPEC pTime, int64_t i64Nano)
284{
285 pTime->i64NanosecondsRelativeToUnixEpoch -= i64Nano;
286 return pTime;
287}
288
289
290/**
291 * Subtracts a time period give as microseconds from the time.
292 *
293 * @returns pTime.
294 * @param pTime The time spec to modify.
295 * @param i64Micro The time period in microseconds.
296 */
297DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMicro(PRTTIMESPEC pTime, int64_t i64Micro)
298{
299 pTime->i64NanosecondsRelativeToUnixEpoch -= i64Micro * RT_NS_1US;
300 return pTime;
301}
302
303
304/**
305 * Subtracts a time period give as milliseconds from the time.
306 *
307 * @returns pTime.
308 * @param pTime The time spec to modify.
309 * @param i64Milli The time period in milliseconds.
310 */
311DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMilli(PRTTIMESPEC pTime, int64_t i64Milli)
312{
313 pTime->i64NanosecondsRelativeToUnixEpoch -= i64Milli * RT_NS_1MS;
314 return pTime;
315}
316
317
318/**
319 * Subtracts a time period give as seconds from the time.
320 *
321 * @returns pTime.
322 * @param pTime The time spec to modify.
323 * @param i64Seconds The time period in seconds.
324 */
325DECLINLINE(PRTTIMESPEC) RTTimeSpecSubSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
326{
327 pTime->i64NanosecondsRelativeToUnixEpoch -= i64Seconds * RT_NS_1SEC;
328 return pTime;
329}
330
331
332/**
333 * Gives the time in seconds and nanoseconds.
334 *
335 * @returns pTime.
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.
339 */
340DECLINLINE(void) RTTimeSpecGetSecondsAndNano(PRTTIMESPEC pTime, int32_t *pi32Seconds, int32_t *pi32Nano)
341{
342 int64_t i64 = RTTimeSpecGetNano(pTime);
343 int32_t i32Nano = (int32_t)(i64 % RT_NS_1SEC);
344 i64 /= RT_NS_1SEC;
345 if (i32Nano < 0)
346 {
347 i32Nano += RT_NS_1SEC;
348 i64--;
349 }
350 *pi32Seconds = (int32_t)i64;
351 *pi32Nano = i32Nano;
352}
353
354
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/**
358 * Gets the time as POSIX timeval.
359 *
360 * @returns pTime.
361 * @param pTime The time spec to interpret.
362 * @param pTimeval Where to store the time as POSIX timeval.
363 */
364DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
365{
366 int64_t i64 = RTTimeSpecGetMicro(pTime);
367 int32_t i32Micro = (int32_t)(i64 % RT_US_1SEC);
368 i64 /= RT_US_1SEC;
369 if (i32Micro < 0)
370 {
371 i32Micro += RT_US_1SEC;
372 i64--;
373 }
374 pTimeval->tv_sec = (time_t)i64;
375 pTimeval->tv_usec = i32Micro;
376 return pTimeval;
377}
378
379/**
380 * Sets the time as POSIX timeval.
381 *
382 * @returns pTime.
383 * @param pTime The time spec to modify.
384 * @param pTimeval Pointer to the POSIX timeval struct with the new time.
385 */
386DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimeval(PRTTIMESPEC pTime, const struct timeval *pTimeval)
387{
388 return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_usec);
389}
390#endif /* various ways of detecting struct timeval */
391
392
393/* PORTME: Add struct timespec guard macro here. */
394#if defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(_SYS__TIMESPEC_H_) || defined(TIMEVAL_TO_TIMESPEC) || defined(_TIMESPEC)
395/**
396 * Gets the time as POSIX timespec.
397 *
398 * @returns pTime.
399 * @param pTime The time spec to interpret.
400 * @param pTimespec Where to store the time as POSIX timespec.
401 */
402DECLINLINE(struct timespec *) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime, struct timespec *pTimespec)
403{
404 int64_t i64 = RTTimeSpecGetNano(pTime);
405 int32_t i32Nano = (int32_t)(i64 % RT_NS_1SEC);
406 i64 /= RT_NS_1SEC;
407 if (i32Nano < 0)
408 {
409 i32Nano += RT_NS_1SEC;
410 i64--;
411 }
412 pTimespec->tv_sec = (time_t)i64;
413 pTimespec->tv_nsec = i32Nano;
414 return pTimespec;
415}
416
417/**
418 * Sets the time as POSIX timespec.
419 *
420 * @returns pTime.
421 * @param pTime The time spec to modify.
422 * @param pTimespec Pointer to the POSIX timespec struct with the new time.
423 */
424DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec(PRTTIMESPEC pTime, const struct timespec *pTimespec)
425{
426 return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimespec->tv_sec), pTimespec->tv_nsec);
427}
428#endif /* various ways of detecting struct timespec */
429
430
431
432/** The offset of the unix epoch and the base for NT time (in 100ns units).
433 * Nt time starts at 1601-01-01 00:00:00. */
434#define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
435
436
437/**
438 * Gets the time as NT time.
439 *
440 * @returns Nt time.
441 * @param pTime The time spec to interpret.
442 */
443DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime)
444{
445 return pTime->i64NanosecondsRelativeToUnixEpoch / 100
446 + RTTIME_NT_TIME_OFFSET_UNIX;
447}
448
449
450/**
451 * Sets the time given by Nt time.
452 *
453 * @returns pTime.
454 * @param pTime The time spec to modify.
455 * @param u64NtTime The new time in Nt time.
456 */
457DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtTime(PRTTIMESPEC pTime, uint64_t u64NtTime)
458{
459 pTime->i64NanosecondsRelativeToUnixEpoch =
460 ((int64_t)u64NtTime - RTTIME_NT_TIME_OFFSET_UNIX) * 100;
461 return pTime;
462}
463
464
465#ifdef _FILETIME_
466/**
467 * Gets the time as NT file time.
468 *
469 * @returns pFileTime.
470 * @param pTime The time spec to interpret.
471 * @param pFileTime Pointer to NT filetime structure.
472 */
473DECLINLINE(PFILETIME) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime, PFILETIME pFileTime)
474{
475 *((uint64_t *)pFileTime) = RTTimeSpecGetNtTime(pTime);
476 return pFileTime;
477}
478
479/**
480 * Sets the time as NT file time.
481 *
482 * @returns pTime.
483 * @param pTime The time spec to modify.
484 * @param pFileTime Where to store the time as Nt file time.
485 */
486DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime, const FILETIME *pFileTime)
487{
488 return RTTimeSpecSetNtTime(pTime, *(const uint64_t *)pFileTime);
489}
490#endif
491
492
493/** The offset to the start of DOS time.
494 * DOS time starts 1980-01-01 00:00:00. */
495#define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
496
497
498/**
499 * Gets the time as seconds relative to the start of dos time.
500 *
501 * @returns seconds relative to the start of dos time.
502 * @param pTime The time spec to interpret.
503 */
504DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime)
505{
506 return (pTime->i64NanosecondsRelativeToUnixEpoch - RTTIME_OFFSET_DOS_TIME)
507 / RT_NS_1SEC;
508}
509
510
511/**
512 * Sets the time given by seconds relative to the start of dos time.
513 *
514 * @returns pTime.
515 * @param pTime The time spec to modify.
516 * @param i64Seconds The new time in seconds relative to the start of dos time.
517 */
518DECLINLINE(PRTTIMESPEC) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
519{
520 pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * RT_NS_1SEC
521 + RTTIME_OFFSET_DOS_TIME;
522 return pTime;
523}
524
525
526/**
527 * Compare two time specs.
528 *
529 * @returns true they are equal.
530 * @returns false they are not equal.
531 * @param pTime1 The 1st time spec.
532 * @param pTime2 The 2nd time spec.
533 */
534DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1, PCRTTIMESPEC pTime2)
535{
536 return pTime1->i64NanosecondsRelativeToUnixEpoch == pTime2->i64NanosecondsRelativeToUnixEpoch;
537}
538
539
540/**
541 * Compare two time specs.
542 *
543 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
544 * @returns false they are not equal.
545 * @param pLeft The 1st time spec.
546 * @param pRight The 2nd time spec.
547 */
548DECLINLINE(int) RTTimeSpecCompare(PCRTTIMESPEC pLeft, PCRTTIMESPEC pRight)
549{
550 if (pLeft->i64NanosecondsRelativeToUnixEpoch == pRight->i64NanosecondsRelativeToUnixEpoch)
551 return 0;
552 return pLeft->i64NanosecondsRelativeToUnixEpoch < pRight->i64NanosecondsRelativeToUnixEpoch ? -1 : 1;
553}
554
555
556/**
557 * Converts a time spec to a ISO date string.
558 *
559 * @returns psz on success.
560 * @returns NULL on buffer underflow.
561 * @param pTime The time spec.
562 * @param psz Where to store the string.
563 * @param cb The size of the buffer.
564 */
565RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
566
567/**
568 * Attempts to convert an ISO date string to a time structure.
569 *
570 * We're a little forgiving with zero padding, unspecified parts, and leading
571 * and trailing spaces.
572 *
573 * @retval pTime on success,
574 * @retval NULL on failure.
575 * @param pTime The time spec.
576 * @param pszString The ISO date string to convert.
577 */
578RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString);
579
580/** @} */
581
582
583/**
584 * Exploded time.
585 */
586#pragma pack(1)
587typedef struct RTTIME
588{
589 /** The year number. */
590 int32_t i32Year;
591 /** The month of the year (1-12). January is 1. */
592 uint8_t u8Month;
593 /** The day of the week (0-6). Monday is 0. */
594 uint8_t u8WeekDay;
595 /** The day of the year (1-366). January the 1st is 1. */
596 uint16_t u16YearDay;
597 /** The day of the month (1-31). */
598 uint8_t u8MonthDay;
599 /** Hour of the day (0-23). */
600 uint8_t u8Hour;
601 /** The minute of the hour (0-59). */
602 uint8_t u8Minute;
603 /** The second of the minute (0-60).
604 * (u32Nanosecond / 1000000) */
605 uint8_t u8Second;
606 /** The nanoseconds of the second (0-999999999). */
607 uint32_t u32Nanosecond;
608 /** Flags, of the RTTIME_FLAGS_* \#defines. */
609 uint32_t fFlags;
610 /** UCT time offset in minutes (-840-840).
611 * @remarks The implementation of RTTimeLocal* isn't quite there yet, so this might not be 100% correct. */
612 int32_t offUTC;
613} RTTIME;
614#pragma pack()
615/** Pointer to a exploded time structure. */
616typedef RTTIME *PRTTIME;
617/** Pointer to a const exploded time structure. */
618typedef const RTTIME *PCRTTIME;
619
620/** @name RTTIME::fFlags values.
621 * @{ */
622/** Set if the time is UTC. If clear the time local time. */
623#define RTTIME_FLAGS_TYPE_MASK 3
624/** the time is UTC time. */
625#define RTTIME_FLAGS_TYPE_UTC 2
626/** The time is local time. */
627#define RTTIME_FLAGS_TYPE_LOCAL 3
628
629/** Set if the time is local and daylight saving time is in effect.
630 * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
631#define RTTIME_FLAGS_DST RT_BIT(4)
632/** Set if the time is local and there is no data available on daylight saving time. */
633#define RTTIME_FLAGS_NO_DST_DATA RT_BIT(5)
634/** Set if the year is a leap year.
635 * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
636#define RTTIME_FLAGS_LEAP_YEAR RT_BIT(6)
637/** Set if the year is a common year.
638 * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
639#define RTTIME_FLAGS_COMMON_YEAR RT_BIT(7)
640/** The mask of valid flags. */
641#define RTTIME_FLAGS_MASK UINT32_C(0xff)
642/** @} */
643
644
645/**
646 * Gets the current system time (UTC).
647 *
648 * @returns pTime.
649 * @param pTime Where to store the time.
650 */
651RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime);
652
653/**
654 * Sets the system time.
655 *
656 * @returns IPRT status code
657 * @param pTime The new system time (UTC).
658 *
659 * @remarks This will usually fail because changing the wall time is usually
660 * requires extra privileges.
661 */
662RTDECL(int) RTTimeSet(PCRTTIMESPEC pTime);
663
664/**
665 * Explodes a time spec (UTC).
666 *
667 * @returns pTime.
668 * @param pTime Where to store the exploded time.
669 * @param pTimeSpec The time spec to exploded.
670 */
671RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
672
673/**
674 * Implodes exploded time to a time spec (UTC).
675 *
676 * @returns pTime on success.
677 * @returns NULL if the pTime data is invalid.
678 * @param pTimeSpec Where to store the imploded UTC time.
679 * If pTime specifies a time which outside the range, maximum or
680 * minimum values will be returned.
681 * @param pTime Pointer to the exploded time to implode.
682 * The fields u8Month, u8WeekDay and u8MonthDay are not used,
683 * and all the other fields are expected to be within their
684 * bounds. Use RTTimeNormalize() to calculate u16YearDay and
685 * normalize the ranges of the fields.
686 */
687RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime);
688
689/**
690 * Normalizes the fields of a time structure.
691 *
692 * It is possible to calculate year-day from month/day and vice
693 * versa. If you adjust any of of these, make sure to zero the
694 * other so you make it clear which of the fields to use. If
695 * it's ambiguous, the year-day field is used (and you get
696 * assertions in debug builds).
697 *
698 * All the time fields and the year-day or month/day fields will
699 * be adjusted for overflows. (Since all fields are unsigned, there
700 * is no underflows.) It is possible to exploit this for simple
701 * date math, though the recommended way of doing that to implode
702 * the time into a timespec and do the math on that.
703 *
704 * @returns pTime on success.
705 * @returns NULL if the data is invalid.
706 *
707 * @param pTime The time structure to normalize.
708 *
709 * @remarks This function doesn't work with local time, only with UTC time.
710 */
711RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime);
712
713/**
714 * Gets the current local system time.
715 *
716 * @returns pTime.
717 * @param pTime Where to store the local time.
718 */
719RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime);
720
721/**
722 * Gets the delta between UTC and local time.
723 *
724 * @code
725 * RTTIMESPEC LocalTime;
726 * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
727 * @endcode
728 *
729 * @returns Returns the nanosecond delta between UTC and local time.
730 */
731RTDECL(int64_t) RTTimeLocalDeltaNano(void);
732
733/**
734 * Explodes a time spec to the localized timezone.
735 *
736 * @returns pTime.
737 * @param pTime Where to store the exploded time.
738 * @param pTimeSpec The time spec to exploded (UTC).
739 */
740RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
741
742/**
743 * Normalizes the fields of a time structure containing local time.
744 *
745 * See RTTimeNormalize for details.
746 *
747 * @returns pTime on success.
748 * @returns NULL if the data is invalid.
749 * @param pTime The time structure to normalize.
750 */
751RTDECL(PRTTIME) RTTimeLocalNormalize(PRTTIME pTime);
752
753/**
754 * Converts a time spec to a ISO date string.
755 *
756 * @returns psz on success.
757 * @returns NULL on buffer underflow.
758 * @param pTime The time. Caller should've normalized this.
759 * @param psz Where to store the string.
760 * @param cb The size of the buffer.
761 */
762RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb);
763
764/**
765 * Attempts to convert an ISO date string to a time structure.
766 *
767 * We're a little forgiving with zero padding, unspecified parts, and leading
768 * and trailing spaces.
769 *
770 * @retval pTime on success,
771 * @retval NULL on failure.
772 * @param pTime Where to store the time on success.
773 * @param pszString The ISO date string to convert.
774 */
775RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString);
776
777/**
778 * Checks if a year is a leap year or not.
779 *
780 * @returns true if it's a leap year.
781 * @returns false if it's a common year.
782 * @param i32Year The year in question.
783 */
784RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year);
785
786/**
787 * Gets the current nanosecond timestamp.
788 *
789 * @returns nanosecond timestamp.
790 */
791RTDECL(uint64_t) RTTimeNanoTS(void);
792
793/**
794 * Gets the current millisecond timestamp.
795 *
796 * @returns millisecond timestamp.
797 */
798RTDECL(uint64_t) RTTimeMilliTS(void);
799
800/**
801 * Debugging the time api.
802 *
803 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
804 */
805RTDECL(uint32_t) RTTimeDbgSteps(void);
806
807/**
808 * Debugging the time api.
809 *
810 * @returns the number of times the TSC interval expired RTTimeNanoTS().
811 */
812RTDECL(uint32_t) RTTimeDbgExpired(void);
813
814/**
815 * Debugging the time api.
816 *
817 * @returns the number of bad previous values encountered by RTTimeNanoTS().
818 */
819RTDECL(uint32_t) RTTimeDbgBad(void);
820
821/**
822 * Debugging the time api.
823 *
824 * @returns the number of update races in RTTimeNanoTS().
825 */
826RTDECL(uint32_t) RTTimeDbgRaces(void);
827
828/** @name RTTimeNanoTS GIP worker functions, for TM.
829 * @{ */
830/** Pointer to a RTTIMENANOTSDATA structure. */
831typedef struct RTTIMENANOTSDATA *PRTTIMENANOTSDATA;
832
833/**
834 * Nanosecond timestamp data.
835 *
836 * This is used to keep track of statistics and callback so IPRT
837 * and TM (VirtualBox) can share code.
838 *
839 * @remark Keep this in sync with the assembly version in timesupA.asm.
840 */
841typedef struct RTTIMENANOTSDATA
842{
843 /** Where the previous timestamp is stored.
844 * This is maintained to ensure that time doesn't go backwards or anything. */
845 uint64_t volatile *pu64Prev;
846
847 /**
848 * Helper function that's used by the assembly routines when something goes bust.
849 *
850 * @param pData Pointer to this structure.
851 * @param u64NanoTS The calculated nano ts.
852 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
853 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
854 */
855 DECLCALLBACKMEMBER(void, pfnBad)(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
856
857 /**
858 * Callback for when rediscovery is required.
859 *
860 * @returns Nanosecond timestamp.
861 * @param pData Pointer to this structure.
862 */
863 DECLCALLBACKMEMBER(uint64_t, pfnRediscover)(PRTTIMENANOTSDATA pData);
864
865 /**
866 * Callback for when some CPU index related stuff goes wrong.
867 *
868 * @returns Nanosecond timestamp.
869 * @param pData Pointer to this structure.
870 * @param idApic The APIC ID if available, otherwise (UINT16_MAX-1).
871 * @param iCpuSet The CPU set index if available, otherwise
872 * (UINT16_MAX-1).
873 * @param iGipCpu The GIP CPU array index if available, otherwise
874 * (UINT16_MAX-1).
875 */
876 DECLCALLBACKMEMBER(uint64_t, pfnBadCpuIndex)(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu);
877
878 /** Number of 1ns steps because of overshooting the period. */
879 uint32_t c1nsSteps;
880 /** The number of times the interval expired (overflow). */
881 uint32_t cExpired;
882 /** Number of "bad" previous values. */
883 uint32_t cBadPrev;
884 /** The number of update races. */
885 uint32_t cUpdateRaces;
886} RTTIMENANOTSDATA;
887
888#ifndef IN_RING3
889/**
890 * The Ring-3 layout of the RTTIMENANOTSDATA structure.
891 */
892typedef struct RTTIMENANOTSDATAR3
893{
894 R3PTRTYPE(uint64_t volatile *) pu64Prev;
895 DECLR3CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
896 DECLR3CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
897 DECLR3CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
898 uint32_t c1nsSteps;
899 uint32_t cExpired;
900 uint32_t cBadPrev;
901 uint32_t cUpdateRaces;
902} RTTIMENANOTSDATAR3;
903#else
904typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR3;
905#endif
906
907#ifndef IN_RING0
908/**
909 * The Ring-3 layout of the RTTIMENANOTSDATA structure.
910 */
911typedef struct RTTIMENANOTSDATAR0
912{
913 R0PTRTYPE(uint64_t volatile *) pu64Prev;
914 DECLR0CALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
915 DECLR0CALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
916 DECLR0CALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
917 uint32_t c1nsSteps;
918 uint32_t cExpired;
919 uint32_t cBadPrev;
920 uint32_t cUpdateRaces;
921} RTTIMENANOTSDATAR0;
922#else
923typedef RTTIMENANOTSDATA RTTIMENANOTSDATAR0;
924#endif
925
926#ifndef IN_RC
927/**
928 * The RC layout of the RTTIMENANOTSDATA structure.
929 */
930typedef struct RTTIMENANOTSDATARC
931{
932 RCPTRTYPE(uint64_t volatile *) pu64Prev;
933 DECLRCCALLBACKMEMBER(void, pfnBad,(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS));
934 DECLRCCALLBACKMEMBER(uint64_t, pfnRediscover,(PRTTIMENANOTSDATA pData));
935 DECLRCCALLBACKMEMBER(uint64_t, pfnBadCpuIndex,(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu));
936 uint32_t c1nsSteps;
937 uint32_t cExpired;
938 uint32_t cBadPrev;
939 uint32_t cUpdateRaces;
940} RTTIMENANOTSDATARC;
941#else
942typedef RTTIMENANOTSDATA RTTIMENANOTSDATARC;
943#endif
944
945/** Internal RTTimeNanoTS worker (assembly). */
946typedef DECLCALLBACK(uint64_t) FNTIMENANOTSINTERNAL(PRTTIMENANOTSDATA pData);
947/** Pointer to an internal RTTimeNanoTS worker (assembly). */
948typedef FNTIMENANOTSINTERNAL *PFNTIMENANOTSINTERNAL;
949RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarNoDelta(PRTTIMENANOTSDATA pData);
950RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarNoDelta(PRTTIMENANOTSDATA pData);
951#ifdef IN_RING3
952RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseApicId(PRTTIMENANOTSDATA pData);
953RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
954RTDECL(uint64_t) RTTimeNanoTSLegacyAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
955RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
956RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
957RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
958RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseApicId(PRTTIMENANOTSDATA pData);
959RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseRdtscp(PRTTIMENANOTSDATA pData);
960RTDECL(uint64_t) RTTimeNanoTSLFenceAsyncUseIdtrLim(PRTTIMENANOTSDATA pData);
961RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId(PRTTIMENANOTSDATA pData);
962RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp(PRTTIMENANOTSDATA pData);
963RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim(PRTTIMENANOTSDATA pData);
964#else
965RTDECL(uint64_t) RTTimeNanoTSLegacyAsync(PRTTIMENANOTSDATA pData);
966RTDECL(uint64_t) RTTimeNanoTSLegacySyncInvarWithDelta(PRTTIMENANOTSDATA pData);
967RTDECL(uint64_t) RTTimeNanoTSLFenceAsync(PRTTIMENANOTSDATA pData);
968RTDECL(uint64_t) RTTimeNanoTSLFenceSyncInvarWithDelta(PRTTIMENANOTSDATA pData);
969#endif
970
971/** @} */
972
973
974/**
975 * Gets the current nanosecond timestamp.
976 *
977 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
978 * resolution or performance optimizations.
979 *
980 * @returns nanosecond timestamp.
981 */
982RTDECL(uint64_t) RTTimeSystemNanoTS(void);
983
984/**
985 * Gets the current millisecond timestamp.
986 *
987 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
988 * resolution or performance optimizations.
989 *
990 * @returns millisecond timestamp.
991 */
992RTDECL(uint64_t) RTTimeSystemMilliTS(void);
993
994/**
995 * Get the nanosecond timestamp relative to program startup.
996 *
997 * @returns Timestamp relative to program startup.
998 */
999RTDECL(uint64_t) RTTimeProgramNanoTS(void);
1000
1001/**
1002 * Get the microsecond timestamp relative to program startup.
1003 *
1004 * @returns Timestamp relative to program startup.
1005 */
1006RTDECL(uint64_t) RTTimeProgramMicroTS(void);
1007
1008/**
1009 * Get the millisecond timestamp relative to program startup.
1010 *
1011 * @returns Timestamp relative to program startup.
1012 */
1013RTDECL(uint64_t) RTTimeProgramMilliTS(void);
1014
1015/**
1016 * Get the second timestamp relative to program startup.
1017 *
1018 * @returns Timestamp relative to program startup.
1019 */
1020RTDECL(uint32_t) RTTimeProgramSecTS(void);
1021
1022/**
1023 * Get the RTTimeNanoTS() of when the program started.
1024 *
1025 * @returns Program startup timestamp.
1026 */
1027RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
1028
1029/** @} */
1030
1031RT_C_DECLS_END
1032
1033#endif
1034
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