1 | /** @file
|
---|
2 | * innotek Portable Runtime - Time.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifndef ___iprt_time_h
|
---|
18 | #define ___iprt_time_h
|
---|
19 |
|
---|
20 | #include <iprt/cdefs.h>
|
---|
21 | #include <iprt/types.h>
|
---|
22 |
|
---|
23 | __BEGIN_DECLS
|
---|
24 |
|
---|
25 | /** @defgroup grp_rt_time RTTime - Time
|
---|
26 | * @ingroup grp_rt
|
---|
27 | * @{
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** Time Specification.
|
---|
31 | *
|
---|
32 | * Use the inline RTTimeSpecGet/Set to operate on structure this so we
|
---|
33 | * can easily change the representation if required later.
|
---|
34 | *
|
---|
35 | * The current representation is in nanoseconds relative to the unix epoch
|
---|
36 | * (1970-01-01 00:00:00 UTC). This gives us an approximate span from
|
---|
37 | * 1678 to 2262 without sacrifying the resolution offered by the various
|
---|
38 | * host OSes (BSD & LINUX 1ns, NT 100ns).
|
---|
39 | */
|
---|
40 | typedef struct RTTIMESPEC
|
---|
41 | {
|
---|
42 | /** Nanoseconds since epoch.
|
---|
43 | * The name is intentially too long to be comfortable to use because you should be
|
---|
44 | * using inline helpers! */
|
---|
45 | int64_t i64NanosecondsRelativeToUnixEpoch;
|
---|
46 | } RTTIMESPEC;
|
---|
47 | /** Pointer to a time spec structure. */
|
---|
48 | typedef RTTIMESPEC *PRTTIMESPEC;
|
---|
49 | /** Pointer to a const time spec structure. */
|
---|
50 | typedef const RTTIMESPEC *PCRTTIMESPEC;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** @name RTTIMESPEC methods
|
---|
54 | * @{ */
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Gets the time as nanoseconds relative to the unix epoch.
|
---|
58 | *
|
---|
59 | * @returns Nanoseconds relative to unix epoch.
|
---|
60 | * @param pTime The time spec to interpret.
|
---|
61 | */
|
---|
62 | DECLINLINE(int64_t) RTTimeSpecGetNano(PCRTTIMESPEC pTime)
|
---|
63 | {
|
---|
64 | return pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Sets the time give by nanoseconds relative to the unix epoch.
|
---|
70 | *
|
---|
71 | * @returns pTime.
|
---|
72 | * @param pTime The time spec to modify.
|
---|
73 | * @param i64Nano The new time in nanoseconds.
|
---|
74 | */
|
---|
75 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
76 | {
|
---|
77 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Nano;
|
---|
78 | return pTime;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Gets the time as microseconds relative to the unix epoch.
|
---|
84 | *
|
---|
85 | * @returns microseconds relative to unix epoch.
|
---|
86 | * @param pTime The time spec to interpret.
|
---|
87 | */
|
---|
88 | DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime)
|
---|
89 | {
|
---|
90 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Sets the time given by microseconds relative to the unix epoch.
|
---|
96 | *
|
---|
97 | * @returns pTime.
|
---|
98 | * @param pTime The time spec to modify.
|
---|
99 | * @param i64Micro The new time in microsecond.
|
---|
100 | */
|
---|
101 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
102 | {
|
---|
103 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Micro * 1000;
|
---|
104 | return pTime;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Gets the time as milliseconds relative to the unix epoch.
|
---|
110 | *
|
---|
111 | * @returns milliseconds relative to unix epoch.
|
---|
112 | * @param pTime The time spec to interpret.
|
---|
113 | */
|
---|
114 | DECLINLINE(int64_t) RTTimeSpecGetMilli(PCRTTIMESPEC pTime)
|
---|
115 | {
|
---|
116 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000000;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Sets the time given by milliseconds relative to the unix epoch.
|
---|
122 | *
|
---|
123 | * @returns pTime.
|
---|
124 | * @param pTime The time spec to modify.
|
---|
125 | * @param i64Milli The new time in milliseconds.
|
---|
126 | */
|
---|
127 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
128 | {
|
---|
129 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Milli * 1000000;
|
---|
130 | return pTime;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Gets the time as seconds relative to the unix epoch.
|
---|
136 | *
|
---|
137 | * @returns seconds relative to unix epoch.
|
---|
138 | * @param pTime The time spec to interpret.
|
---|
139 | */
|
---|
140 | DECLINLINE(int64_t) RTTimeSpecGetSeconds(PCRTTIMESPEC pTime)
|
---|
141 | {
|
---|
142 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000000000;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Sets the time given by seconds relative to the unix epoch.
|
---|
148 | *
|
---|
149 | * @returns pTime.
|
---|
150 | * @param pTime The time spec to modify.
|
---|
151 | * @param i64Seconds The new time in seconds.
|
---|
152 | */
|
---|
153 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
154 | {
|
---|
155 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * 1000000000;
|
---|
156 | return pTime;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Makes the time spec absolute like abs() does (i.e. a positive value).
|
---|
162 | *
|
---|
163 | * @returns pTime.
|
---|
164 | * @param pTime The time spec to modify.
|
---|
165 | */
|
---|
166 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAbsolute(PRTTIMESPEC pTime)
|
---|
167 | {
|
---|
168 | if (pTime->i64NanosecondsRelativeToUnixEpoch < 0)
|
---|
169 | pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
170 | return pTime;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Negates the time.
|
---|
176 | *
|
---|
177 | * @returns pTime.
|
---|
178 | * @param pTime The time spec to modify.
|
---|
179 | */
|
---|
180 | DECLINLINE(PRTTIMESPEC) RTTimeSpecNegate(PRTTIMESPEC pTime)
|
---|
181 | {
|
---|
182 | pTime->i64NanosecondsRelativeToUnixEpoch = -pTime->i64NanosecondsRelativeToUnixEpoch;
|
---|
183 | return pTime;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Adds a time period to the time.
|
---|
189 | *
|
---|
190 | * @returns pTime.
|
---|
191 | * @param pTime The time spec to modify.
|
---|
192 | * @param pTimeAdd The time spec to add to pTime.
|
---|
193 | */
|
---|
194 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAdd(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeAdd)
|
---|
195 | {
|
---|
196 | pTime->i64NanosecondsRelativeToUnixEpoch += pTimeAdd->i64NanosecondsRelativeToUnixEpoch;
|
---|
197 | return pTime;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Adds a time period give as nanoseconds from the time.
|
---|
203 | *
|
---|
204 | * @returns pTime.
|
---|
205 | * @param pTime The time spec to modify.
|
---|
206 | * @param i64Nano The time period in nanoseconds.
|
---|
207 | */
|
---|
208 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
209 | {
|
---|
210 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Nano;
|
---|
211 | return pTime;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Adds a time period give as microseconds from the time.
|
---|
217 | *
|
---|
218 | * @returns pTime.
|
---|
219 | * @param pTime The time spec to modify.
|
---|
220 | * @param i64Micro The time period in microseconds.
|
---|
221 | */
|
---|
222 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
223 | {
|
---|
224 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Micro * 1000;
|
---|
225 | return pTime;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Adds a time period give as milliseconds from the time.
|
---|
231 | *
|
---|
232 | * @returns pTime.
|
---|
233 | * @param pTime The time spec to modify.
|
---|
234 | * @param i64Milli The time period in milliseconds.
|
---|
235 | */
|
---|
236 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
237 | {
|
---|
238 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Milli * 1000000;
|
---|
239 | return pTime;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Adds a time period give as seconds from the time.
|
---|
245 | *
|
---|
246 | * @returns pTime.
|
---|
247 | * @param pTime The time spec to modify.
|
---|
248 | * @param i64Seconds The time period in seconds.
|
---|
249 | */
|
---|
250 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
251 | {
|
---|
252 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Seconds * 1000000000;
|
---|
253 | return pTime;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Subtracts a time period from the time.
|
---|
259 | *
|
---|
260 | * @returns pTime.
|
---|
261 | * @param pTime The time spec to modify.
|
---|
262 | * @param pTimeSub The time spec to subtract from pTime.
|
---|
263 | */
|
---|
264 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSub(PRTTIMESPEC pTime, PCRTTIMESPEC pTimeSub)
|
---|
265 | {
|
---|
266 | pTime->i64NanosecondsRelativeToUnixEpoch -= pTimeSub->i64NanosecondsRelativeToUnixEpoch;
|
---|
267 | return pTime;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Subtracts a time period give as nanoseconds from the time.
|
---|
273 | *
|
---|
274 | * @returns pTime.
|
---|
275 | * @param pTime The time spec to modify.
|
---|
276 | * @param i64Nano The time period in nanoseconds.
|
---|
277 | */
|
---|
278 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubNano(PRTTIMESPEC pTime, int64_t i64Nano)
|
---|
279 | {
|
---|
280 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Nano;
|
---|
281 | return pTime;
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Subtracts a time period give as microseconds from the time.
|
---|
287 | *
|
---|
288 | * @returns pTime.
|
---|
289 | * @param pTime The time spec to modify.
|
---|
290 | * @param i64Micro The time period in microseconds.
|
---|
291 | */
|
---|
292 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
293 | {
|
---|
294 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Micro * 1000;
|
---|
295 | return pTime;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Subtracts a time period give as milliseconds from the time.
|
---|
301 | *
|
---|
302 | * @returns pTime.
|
---|
303 | * @param pTime The time spec to modify.
|
---|
304 | * @param i64Milli The time period in milliseconds.
|
---|
305 | */
|
---|
306 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
307 | {
|
---|
308 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Milli * 1000000;
|
---|
309 | return pTime;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Subtracts a time period give as seconds from the time.
|
---|
315 | *
|
---|
316 | * @returns pTime.
|
---|
317 | * @param pTime The time spec to modify.
|
---|
318 | * @param i64Seconds The time period in seconds.
|
---|
319 | */
|
---|
320 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
321 | {
|
---|
322 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Seconds * 100000000;
|
---|
323 | return pTime;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | /* PORTME: Add struct timeval guard macro here. */
|
---|
328 | #if defined(RTTIME_INCL_TIMEVAL) || defined(_STRUCT_TIMEVAL) || defined(_SYS__TIMEVAL_H_) || defined(_SYS_TIME_H) || defined(_TIMEVAL)
|
---|
329 | /**
|
---|
330 | * Gets the time as POSIX timeval.
|
---|
331 | *
|
---|
332 | * @returns pTime.
|
---|
333 | * @param pTime The time spec to interpret.
|
---|
334 | * @param pTimeval Where to store the time as POSIX timeval.
|
---|
335 | */
|
---|
336 | DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
|
---|
337 | {
|
---|
338 | int64_t i64 = RTTimeSpecGetMicro(pTime);
|
---|
339 | int32_t i32Micro = (int32_t)(i64 % 1000000);
|
---|
340 | i64 /= 1000000;
|
---|
341 | if (i32Micro < 0)
|
---|
342 | {
|
---|
343 | i32Micro += 1000000;
|
---|
344 | i64++;
|
---|
345 | }
|
---|
346 | pTimeval->tv_sec = (time_t)i64;
|
---|
347 | pTimeval->tv_usec = i32Micro;
|
---|
348 | return pTimeval;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Sets the time as POSIX timeval.
|
---|
353 | *
|
---|
354 | * @returns pTime.
|
---|
355 | * @param pTime The time spec to modify.
|
---|
356 | * @param pTimeval Pointer to the POSIX timeval struct with the new time.
|
---|
357 | */
|
---|
358 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimeval(PRTTIMESPEC pTime, const struct timeval *pTimeval)
|
---|
359 | {
|
---|
360 | return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_usec);
|
---|
361 | }
|
---|
362 | #endif /* various ways of detecting struct timeval */
|
---|
363 |
|
---|
364 |
|
---|
365 | /* PORTME: Add struct timespec guard macro here. */
|
---|
366 | #if defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(_SYS__TIMESPEC_H_) || defined(TIMEVAL_TO_TIMESPEC) || defined(_TIMESPEC)
|
---|
367 | /**
|
---|
368 | * Gets the time as POSIX timespec.
|
---|
369 | *
|
---|
370 | * @returns pTime.
|
---|
371 | * @param pTime The time spec to interpret.
|
---|
372 | * @param pTimespec Where to store the time as POSIX timespec.
|
---|
373 | */
|
---|
374 | DECLINLINE(struct timespec *) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime, struct timespec *pTimespec)
|
---|
375 | {
|
---|
376 | int64_t i64 = RTTimeSpecGetNano(pTime);
|
---|
377 | int32_t i32Nano = (int32_t)(i64 % 1000000000);
|
---|
378 | i64 /= 1000000000;
|
---|
379 | if (i32Nano < 0)
|
---|
380 | {
|
---|
381 | i32Nano += 1000000000;
|
---|
382 | i64++;
|
---|
383 | }
|
---|
384 | pTimespec->tv_sec = (time_t)i64;
|
---|
385 | pTimespec->tv_nsec = i32Nano;
|
---|
386 | return pTimespec;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Sets the time as POSIX timespec.
|
---|
391 | *
|
---|
392 | * @returns pTime.
|
---|
393 | * @param pTime The time spec to modify.
|
---|
394 | * @param pTimespec Pointer to the POSIX timespec struct with the new time.
|
---|
395 | */
|
---|
396 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec(PRTTIMESPEC pTime, const struct timespec *pTimespec)
|
---|
397 | {
|
---|
398 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimespec->tv_sec), pTimespec->tv_nsec);
|
---|
399 | }
|
---|
400 | #endif /* various ways of detecting struct timespec */
|
---|
401 |
|
---|
402 |
|
---|
403 |
|
---|
404 | /** The offset of the unix epoch and the base for NT time (in 100ns units).
|
---|
405 | * Nt time starts at 1601-01-01 00:00:00. */
|
---|
406 | #define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
|
---|
407 |
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Gets the time as NT time.
|
---|
411 | *
|
---|
412 | * @returns Nt time.
|
---|
413 | * @param pTime The time spec to interpret.
|
---|
414 | */
|
---|
415 | DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime)
|
---|
416 | {
|
---|
417 | return pTime->i64NanosecondsRelativeToUnixEpoch / 100
|
---|
418 | + RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Sets the time given by Nt time.
|
---|
424 | *
|
---|
425 | * @returns pTime.
|
---|
426 | * @param pTime The time spec to modify.
|
---|
427 | * @param u64NtTime The new time in Nt time.
|
---|
428 | */
|
---|
429 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtTime(PRTTIMESPEC pTime, uint64_t u64NtTime)
|
---|
430 | {
|
---|
431 | pTime->i64NanosecondsRelativeToUnixEpoch =
|
---|
432 | ((int64_t)u64NtTime - RTTIME_NT_TIME_OFFSET_UNIX) * 100;
|
---|
433 | return pTime;
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | #ifdef _FILETIME_
|
---|
438 | /**
|
---|
439 | * Gets the time as NT file time.
|
---|
440 | *
|
---|
441 | * @returns pFileTime.
|
---|
442 | * @param pTime The time spec to interpret.
|
---|
443 | * @param pFileTime Pointer to NT filetime structure.
|
---|
444 | */
|
---|
445 | DECLINLINE(PFILETIME) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime, PFILETIME pFileTime)
|
---|
446 | {
|
---|
447 | *((uint64_t *)pFileTime) = RTTimeSpecGetNtTime(pTime);
|
---|
448 | return pFileTime;
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Sets the time as NT file time.
|
---|
453 | *
|
---|
454 | * @returns pTime.
|
---|
455 | * @param pTime The time spec to modify.
|
---|
456 | * @param pFileTime Where to store the time as Nt file time.
|
---|
457 | */
|
---|
458 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime, const FILETIME *pFileTime)
|
---|
459 | {
|
---|
460 | return RTTimeSpecSetNtTime(pTime, *(const uint64_t *)pFileTime);
|
---|
461 | }
|
---|
462 | #endif
|
---|
463 |
|
---|
464 |
|
---|
465 | /** The offset to the start of DOS time.
|
---|
466 | * DOS time starts 1980-01-01 00:00:00. */
|
---|
467 | #define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Gets the time as seconds relative to the start of dos time.
|
---|
472 | *
|
---|
473 | * @returns seconds relative to the start of dos time.
|
---|
474 | * @param pTime The time spec to interpret.
|
---|
475 | */
|
---|
476 | DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime)
|
---|
477 | {
|
---|
478 | return (pTime->i64NanosecondsRelativeToUnixEpoch + RTTIME_OFFSET_DOS_TIME)
|
---|
479 | / 1000000000;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Sets the time given by seconds relative to the start of dos time.
|
---|
485 | *
|
---|
486 | * @returns pTime.
|
---|
487 | * @param pTime The time spec to modify.
|
---|
488 | * @param i64Seconds The new time in seconds relative to the start of dos time.
|
---|
489 | */
|
---|
490 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
491 | {
|
---|
492 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * 1000000000
|
---|
493 | - RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
494 | return pTime;
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Compare two time specs.
|
---|
500 | *
|
---|
501 | * @returns true they are equal.
|
---|
502 | * @returns false they are not equal.
|
---|
503 | * @param pTime1 The 1st time spec.
|
---|
504 | * @param pTime2 The 2nd time spec.
|
---|
505 | */
|
---|
506 | DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1, PCRTTIMESPEC pTime2)
|
---|
507 | {
|
---|
508 | return pTime1->i64NanosecondsRelativeToUnixEpoch == pTime2->i64NanosecondsRelativeToUnixEpoch;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Converts a time spec to a ISO date string.
|
---|
513 | *
|
---|
514 | * @returns psz on success.
|
---|
515 | * @returns NULL on buffer underflow.
|
---|
516 | * @param pTime The time spec.
|
---|
517 | * @param psz Where to store the string.
|
---|
518 | * @param cb The size of the buffer.
|
---|
519 | */
|
---|
520 | RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
|
---|
521 |
|
---|
522 | /** @} */
|
---|
523 |
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Exploded time.
|
---|
527 | */
|
---|
528 | #pragma pack(1)
|
---|
529 | typedef struct RTTIME
|
---|
530 | {
|
---|
531 | /** The year number. */
|
---|
532 | int32_t i32Year;
|
---|
533 | /** The month of the year (1-12). January is 1. */
|
---|
534 | uint8_t u8Month;
|
---|
535 | /** The day of the week (0-6). Monday is 0. */
|
---|
536 | uint8_t u8WeekDay;
|
---|
537 | /** The day of the year (1-366). January the 1st is 1. */
|
---|
538 | uint16_t u16YearDay;
|
---|
539 | /** The day of the month (1-31). */
|
---|
540 | uint8_t u8MonthDay;
|
---|
541 | /** Hour of the day (0-23). */
|
---|
542 | uint8_t u8Hour;
|
---|
543 | /** The minute of the hour (0-59). */
|
---|
544 | uint8_t u8Minute;
|
---|
545 | /** The second of the minute (0-60).
|
---|
546 | * (u32Nanosecond / 1000000) */
|
---|
547 | uint8_t u8Second;
|
---|
548 | /** The nanoseconds of the second (0-999999999). */
|
---|
549 | uint32_t u32Nanosecond;
|
---|
550 | /** Flags, of the RTTIME_FLAGS_* \#defines. */
|
---|
551 | uint32_t fFlags;
|
---|
552 | /** @todo we need a UTC offset field. */
|
---|
553 | } RTTIME;
|
---|
554 | #pragma pack()
|
---|
555 | /** Pointer to a exploded time structure. */
|
---|
556 | typedef RTTIME *PRTTIME;
|
---|
557 | /** Pointer to a const exploded time structure. */
|
---|
558 | typedef const RTTIME *PCRTTIME;
|
---|
559 |
|
---|
560 | /** @name RTTIME::fFlags values.
|
---|
561 | * @{ */
|
---|
562 | /** Set if the time is UTC. If clear the time local time. */
|
---|
563 | #define RTTIME_FLAGS_TYPE_MASK 3
|
---|
564 | /** the time is UTC time. */
|
---|
565 | #define RTTIME_FLAGS_TYPE_UTC 2
|
---|
566 | /** The time is local time. */
|
---|
567 | #define RTTIME_FLAGS_TYPE_LOCAL 3
|
---|
568 |
|
---|
569 | /** Set if the time is local and daylight saving time is in effect.
|
---|
570 | * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
|
---|
571 | #define RTTIME_FLAGS_DST BIT(4)
|
---|
572 | /** Set if the time is local and there is no data available on daylight saving time. */
|
---|
573 | #define RTTIME_FLAGS_NO_DST_DATA BIT(5)
|
---|
574 | /** Set if the year is a leap year.
|
---|
575 | * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
|
---|
576 | #define RTTIME_FLAGS_LEAP_YEAR BIT(6)
|
---|
577 | /** Set if the year is a common year.
|
---|
578 | * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
|
---|
579 | #define RTTIME_FLAGS_COMMON_YEAR BIT(7)
|
---|
580 | /** @} */
|
---|
581 |
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Gets the current system time (UTC).
|
---|
585 | *
|
---|
586 | * @returns pTime.
|
---|
587 | * @param pTime Where to store the time.
|
---|
588 | */
|
---|
589 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime);
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Explodes a time spec (UTC).
|
---|
593 | *
|
---|
594 | * @returns pTime.
|
---|
595 | * @param pTime Where to store the exploded time.
|
---|
596 | * @param pTimeSpec The time spec to exploded.
|
---|
597 | */
|
---|
598 | RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Implodes exploded time to a time spec (UTC).
|
---|
602 | *
|
---|
603 | * @returns pTime on success.
|
---|
604 | * @returns NULL if the pTime data is invalid.
|
---|
605 | * @param pTimeSpec Where to store the imploded UTC time.
|
---|
606 | * If pTime specifies a time which outside the range, maximum or
|
---|
607 | * minimum values will be returned.
|
---|
608 | * @param pTime Pointer to the exploded time to implode.
|
---|
609 | * The fields u8Month, u8WeekDay and u8MonthDay are not used,
|
---|
610 | * and all the other fields are expected to be within their
|
---|
611 | * bounds. Use RTTimeNormalize() to calculate u16YearDay and
|
---|
612 | * normalize the ranges of the fields.
|
---|
613 | */
|
---|
614 | RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime);
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Normalizes the fields of a timestructure.
|
---|
618 | *
|
---|
619 | * It is possible to calculate month/day fields in some
|
---|
620 | * combinations. It's also possible to overflow other
|
---|
621 | * fields, and these overflows will be adjusted for.
|
---|
622 | *
|
---|
623 | * @returns pTime on success.
|
---|
624 | * @returns NULL if the data is invalid.
|
---|
625 | * @param pTime The time structure to normalize.
|
---|
626 | */
|
---|
627 | RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime);
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Gets the current local system time.
|
---|
631 | *
|
---|
632 | * @returns pTime.
|
---|
633 | * @param pTime Where to store the local time.
|
---|
634 | */
|
---|
635 | RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime);
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Gets the delta between UTC and local time.
|
---|
639 | *
|
---|
640 | * @code
|
---|
641 | * RTTIMESPEC LocalTime;
|
---|
642 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
643 | * @endcode
|
---|
644 | *
|
---|
645 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
646 | */
|
---|
647 | RTDECL(int64_t) RTTimeLocalDeltaNano(void);
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Explodes a time spec to the localized timezone.
|
---|
651 | *
|
---|
652 | * @returns pTime.
|
---|
653 | * @param pTime Where to store the exploded time.
|
---|
654 | * @param pTimeSpec The time spec to exploded (UTC).
|
---|
655 | */
|
---|
656 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Converts a time spec to a ISO date string.
|
---|
660 | *
|
---|
661 | * @returns psz on success.
|
---|
662 | * @returns NULL on buffer underflow.
|
---|
663 | * @param pTime The time. Caller should've normalized this.
|
---|
664 | * @param psz Where to store the string.
|
---|
665 | * @param cb The size of the buffer.
|
---|
666 | */
|
---|
667 | RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb);
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Gets the current nanosecond timestamp.
|
---|
671 | *
|
---|
672 | * @returns nanosecond timestamp.
|
---|
673 | */
|
---|
674 | RTDECL(uint64_t) RTTimeNanoTS(void);
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Gets the current millisecond timestamp.
|
---|
678 | *
|
---|
679 | * @returns millisecond timestamp.
|
---|
680 | */
|
---|
681 | RTDECL(uint64_t) RTTimeMilliTS(void);
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Debugging the time api.
|
---|
685 | *
|
---|
686 | * @returns the number of 1ns steps which has been applied by rtTimeNanoTSInternal().
|
---|
687 | */
|
---|
688 | RTDECL(uint32_t) RTTime1nsSteps(void);
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Gets the current nanosecond timestamp.
|
---|
692 | *
|
---|
693 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
694 | * resolution or performance optimizations.
|
---|
695 | *
|
---|
696 | * @returns nanosecond timestamp.
|
---|
697 | */
|
---|
698 | RTDECL(uint64_t) RTTimeSystemNanoTS(void);
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Gets the current millisecond timestamp.
|
---|
702 | *
|
---|
703 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
704 | * resolution or performance optimizations.
|
---|
705 | *
|
---|
706 | * @returns millisecond timestamp.
|
---|
707 | */
|
---|
708 | RTDECL(uint64_t) RTTimeSystemMilliTS(void);
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Get the nanosecond timestamp relative to program startup.
|
---|
712 | *
|
---|
713 | * @returns Timestamp relative to program startup.
|
---|
714 | */
|
---|
715 | RTDECL(uint64_t) RTTimeProgramNanoTS(void);
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Get the microsecond timestamp relative to program startup.
|
---|
719 | *
|
---|
720 | * @returns Timestamp relative to program startup.
|
---|
721 | */
|
---|
722 | RTDECL(uint64_t) RTTimeProgramMicroTS(void);
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Get the millisecond timestamp relative to program startup.
|
---|
726 | *
|
---|
727 | * @returns Timestamp relative to program startup.
|
---|
728 | */
|
---|
729 | RTDECL(uint64_t) RTTimeProgramMilliTS(void);
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Get the second timestamp relative to program startup.
|
---|
733 | *
|
---|
734 | * @returns Timestamp relative to program startup.
|
---|
735 | */
|
---|
736 | RTDECL(uint32_t) RTTimeProgramSecTS(void);
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * Get the RTTimeNanoTS() of when the program started.
|
---|
740 | *
|
---|
741 | * @returns Program startup timestamp.
|
---|
742 | */
|
---|
743 | RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
|
---|
744 |
|
---|
745 | /** @} */
|
---|
746 |
|
---|
747 | __END_DECLS
|
---|
748 |
|
---|
749 | #endif
|
---|
750 |
|
---|