VirtualBox

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

Last change on this file since 74125 was 74125, checked in by vboxsync, 6 years ago

IPRT/time: Extended RTTimeToRfc2822 with a flag for specifying RFC-7231 behaviour. Added simple testcase for it. bugref:9167

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