1 | /** @file
|
---|
2 | *
|
---|
3 | * innotek Portable Runtime - Time.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef __iprt_time_h__
|
---|
23 | #define __iprt_time_h__
|
---|
24 |
|
---|
25 | #include <iprt/cdefs.h>
|
---|
26 | #include <iprt/types.h>
|
---|
27 |
|
---|
28 | __BEGIN_DECLS
|
---|
29 |
|
---|
30 | /** @defgroup grp_rt_time RTTime - Time
|
---|
31 | * @ingroup grp_rt
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** Time Specification.
|
---|
36 | *
|
---|
37 | * Use the inline RTTimeSpecGet/Set to operate on structure this so we
|
---|
38 | * can easily change the representation if required later.
|
---|
39 | *
|
---|
40 | * The current representation is in nanoseconds relative to the unix epoch
|
---|
41 | * (1970-01-01 00:00:00 UTC). This gives us an approximate span from
|
---|
42 | * 1678 to 2262 without sacrifying the resolution offered by the various
|
---|
43 | * host OSes (BSD & LINUX 1ns, NT 100ns).
|
---|
44 | */
|
---|
45 | typedef struct RTTIMESPEC
|
---|
46 | {
|
---|
47 | /** Nanoseconds since epoch.
|
---|
48 | * The name is intentially too long to be comfortable to use because you should be
|
---|
49 | * using inline helpers! */
|
---|
50 | int64_t i64NanosecondsRelativeToUnixEpoch;
|
---|
51 | } RTTIMESPEC;
|
---|
52 | /** Pointer to a time spec structure. */
|
---|
53 | typedef RTTIMESPEC *PRTTIMESPEC;
|
---|
54 | /** Pointer to a const time spec structure. */
|
---|
55 | typedef const RTTIMESPEC *PCRTTIMESPEC;
|
---|
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 | */
|
---|
67 | DECLINLINE(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 | */
|
---|
80 | DECLINLINE(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 | */
|
---|
93 | DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime)
|
---|
94 | {
|
---|
95 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000;
|
---|
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 | */
|
---|
106 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
107 | {
|
---|
108 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Micro * 1000;
|
---|
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 | */
|
---|
119 | DECLINLINE(int64_t) RTTimeSpecGetMilli(PCRTTIMESPEC pTime)
|
---|
120 | {
|
---|
121 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000000;
|
---|
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 | */
|
---|
132 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
133 | {
|
---|
134 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Milli * 1000000;
|
---|
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 | */
|
---|
145 | DECLINLINE(int64_t) RTTimeSpecGetSeconds(PCRTTIMESPEC pTime)
|
---|
146 | {
|
---|
147 | return pTime->i64NanosecondsRelativeToUnixEpoch / 1000000000;
|
---|
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 | */
|
---|
158 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
159 | {
|
---|
160 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * 1000000000;
|
---|
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 | */
|
---|
171 | DECLINLINE(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 | */
|
---|
185 | DECLINLINE(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 | */
|
---|
199 | DECLINLINE(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 | */
|
---|
213 | DECLINLINE(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 | */
|
---|
227 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
228 | {
|
---|
229 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Micro * 1000;
|
---|
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 | */
|
---|
241 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
242 | {
|
---|
243 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Milli * 1000000;
|
---|
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 | */
|
---|
255 | DECLINLINE(PRTTIMESPEC) RTTimeSpecAddSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
256 | {
|
---|
257 | pTime->i64NanosecondsRelativeToUnixEpoch += i64Seconds * 1000000000;
|
---|
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 | */
|
---|
269 | DECLINLINE(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 | */
|
---|
283 | DECLINLINE(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 | */
|
---|
297 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMicro(PRTTIMESPEC pTime, int64_t i64Micro)
|
---|
298 | {
|
---|
299 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Micro * 1000;
|
---|
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 | */
|
---|
311 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubMilli(PRTTIMESPEC pTime, int64_t i64Milli)
|
---|
312 | {
|
---|
313 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Milli * 1000000;
|
---|
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 | */
|
---|
325 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSubSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
326 | {
|
---|
327 | pTime->i64NanosecondsRelativeToUnixEpoch -= i64Seconds * 100000000;
|
---|
328 | return pTime;
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | #if defined(RTTIME_INCL_TIMEVAL) || defined(_STRUCT_TIMEVAL) || defined(TIMEVAL_TO_TIMESPEC)
|
---|
333 | /**
|
---|
334 | * Gets the time as POSIX timeval.
|
---|
335 | *
|
---|
336 | * @returns pTime.
|
---|
337 | * @param pTime The time spec to interpret.
|
---|
338 | * @param pTimeval Where to store the time as POSIX timeval.
|
---|
339 | */
|
---|
340 | DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
|
---|
341 | {
|
---|
342 | int64_t i64 = RTTimeSpecGetMicro(pTime);
|
---|
343 | int32_t i32Micro = (int32_t)(i64 % 1000000);
|
---|
344 | i64 /= 1000000;
|
---|
345 | if (i32Micro < 0)
|
---|
346 | {
|
---|
347 | i32Micro += 1000000;
|
---|
348 | i64++;
|
---|
349 | }
|
---|
350 | pTimeval->tv_sec = (time_t)i64;
|
---|
351 | pTimeval->tv_usec = i32Micro;
|
---|
352 | return pTimeval;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Sets the time as POSIX timeval.
|
---|
357 | *
|
---|
358 | * @returns pTime.
|
---|
359 | * @param pTime The time spec to modify.
|
---|
360 | * @param pTimeval Pointer to the POSIX timeval struct with the new time.
|
---|
361 | */
|
---|
362 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimeval(PRTTIMESPEC pTime, const struct timeval *pTimeval)
|
---|
363 | {
|
---|
364 | return RTTimeSpecAddMicro(RTTimeSpecSetSeconds(pTime, pTimeval->tv_sec), pTimeval->tv_usec);
|
---|
365 | }
|
---|
366 | #endif /* various ways of detecting struct timeval */
|
---|
367 |
|
---|
368 |
|
---|
369 | #if defined(RTTIME_INCL_TIMESPEC) || defined(_STRUCT_TIMESPEC) || defined(TIMEVAL_TO_TIMESPEC)
|
---|
370 | /**
|
---|
371 | * Gets the time as POSIX timespec.
|
---|
372 | *
|
---|
373 | * @returns pTime.
|
---|
374 | * @param pTime The time spec to interpret.
|
---|
375 | * @param pTimespec Where to store the time as POSIX timespec.
|
---|
376 | */
|
---|
377 | DECLINLINE(struct timespec *) RTTimeSpecGetTimespec(PCRTTIMESPEC pTime, struct timespec *pTimespec)
|
---|
378 | {
|
---|
379 | int64_t i64 = RTTimeSpecGetNano(pTime);
|
---|
380 | int32_t i32Nano = (int32_t)(i64 % 1000000000);
|
---|
381 | i64 /= 1000000000;
|
---|
382 | if (i32Nano < 0)
|
---|
383 | {
|
---|
384 | i32Nano += 1000000000;
|
---|
385 | i64++;
|
---|
386 | }
|
---|
387 | pTimespec->tv_sec = (time_t)i64;
|
---|
388 | pTimespec->tv_nsec = i32Nano;
|
---|
389 | return pTimespec;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Sets the time as POSIX timespec.
|
---|
394 | *
|
---|
395 | * @returns pTime.
|
---|
396 | * @param pTime The time spec to modify.
|
---|
397 | * @param pTimespec Pointer to the POSIX timespec struct with the new time.
|
---|
398 | */
|
---|
399 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetTimespec(PRTTIMESPEC pTime, const struct timespec *pTimespec)
|
---|
400 | {
|
---|
401 | return RTTimeSpecAddNano(RTTimeSpecSetSeconds(pTime, pTimespec->tv_sec), pTimespec->tv_nsec);
|
---|
402 | }
|
---|
403 | #endif /* various ways of detecting struct timespec */
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 | /** The offset of the unix epoch and the base for NT time (in 100ns units).
|
---|
408 | * Nt time starts at 1601-01-01 00:00:00. */
|
---|
409 | #define RTTIME_NT_TIME_OFFSET_UNIX (116444736000000000LL)
|
---|
410 |
|
---|
411 |
|
---|
412 | /**
|
---|
413 | * Gets the time as NT time.
|
---|
414 | *
|
---|
415 | * @returns Nt time.
|
---|
416 | * @param pTime The time spec to interpret.
|
---|
417 | */
|
---|
418 | DECLINLINE(uint64_t) RTTimeSpecGetNtTime(PCRTTIMESPEC pTime)
|
---|
419 | {
|
---|
420 | return pTime->i64NanosecondsRelativeToUnixEpoch / 100
|
---|
421 | + RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Sets the time given by Nt time.
|
---|
427 | *
|
---|
428 | * @returns pTime.
|
---|
429 | * @param pTime The time spec to modify.
|
---|
430 | * @param u64NtTime The new time in Nt time.
|
---|
431 | */
|
---|
432 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtTime(PRTTIMESPEC pTime, uint64_t u64NtTime)
|
---|
433 | {
|
---|
434 | pTime->i64NanosecondsRelativeToUnixEpoch =
|
---|
435 | ((int64_t)u64NtTime - RTTIME_NT_TIME_OFFSET_UNIX) * 100;
|
---|
436 | return pTime;
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | #ifdef _FILETIME_
|
---|
441 | /**
|
---|
442 | * Gets the time as NT file time.
|
---|
443 | *
|
---|
444 | * @returns pFileTime.
|
---|
445 | * @param pTime The time spec to interpret.
|
---|
446 | * @param pFileTime Pointer to NT filetime structure.
|
---|
447 | */
|
---|
448 | DECLINLINE(PFILETIME) RTTimeSpecGetNtFileTime(PCRTTIMESPEC pTime, PFILETIME pFileTime)
|
---|
449 | {
|
---|
450 | *((uint64_t *)pFileTime) = RTTimeSpecGetNtTime(pTime);
|
---|
451 | return pFileTime;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Sets the time as NT file time.
|
---|
456 | *
|
---|
457 | * @returns pTime.
|
---|
458 | * @param pTime The time spec to modify.
|
---|
459 | * @param pFileTime Where to store the time as Nt file time.
|
---|
460 | */
|
---|
461 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetNtFileTime(PRTTIMESPEC pTime, const FILETIME *pFileTime)
|
---|
462 | {
|
---|
463 | return RTTimeSpecSetNtTime(pTime, *(const uint64_t *)pFileTime);
|
---|
464 | }
|
---|
465 | #endif
|
---|
466 |
|
---|
467 |
|
---|
468 | /** The offset to the start of DOS time.
|
---|
469 | * DOS time starts 1980-01-01 00:00:00. */
|
---|
470 | #define RTTIME_OFFSET_DOS_TIME (315532800000000000LL)
|
---|
471 |
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Gets the time as seconds relative to the start of dos time.
|
---|
475 | *
|
---|
476 | * @returns seconds relative to the start of dos time.
|
---|
477 | * @param pTime The time spec to interpret.
|
---|
478 | */
|
---|
479 | DECLINLINE(int64_t) RTTimeSpecGetDosSeconds(PCRTTIMESPEC pTime)
|
---|
480 | {
|
---|
481 | return (pTime->i64NanosecondsRelativeToUnixEpoch + RTTIME_OFFSET_DOS_TIME)
|
---|
482 | / 1000000000;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Sets the time given by seconds relative to the start of dos time.
|
---|
488 | *
|
---|
489 | * @returns pTime.
|
---|
490 | * @param pTime The time spec to modify.
|
---|
491 | * @param i64Seconds The new time in seconds relative to the start of dos time.
|
---|
492 | */
|
---|
493 | DECLINLINE(PRTTIMESPEC) RTTimeSpecSetDosSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
|
---|
494 | {
|
---|
495 | pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * 1000000000
|
---|
496 | - RTTIME_NT_TIME_OFFSET_UNIX;
|
---|
497 | return pTime;
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Compare two time specs.
|
---|
503 | *
|
---|
504 | * @returns true they are equal.
|
---|
505 | * @returns false they are not equal.
|
---|
506 | * @param pTime1 The 1st time spec.
|
---|
507 | * @param pTime2 The 2nd time spec.
|
---|
508 | */
|
---|
509 | DECLINLINE(bool) RTTimeSpecIsEqual(PCRTTIMESPEC pTime1, PCRTTIMESPEC pTime2)
|
---|
510 | {
|
---|
511 | return pTime1->i64NanosecondsRelativeToUnixEpoch == pTime2->i64NanosecondsRelativeToUnixEpoch;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Converts a time spec to a ISO date string.
|
---|
516 | *
|
---|
517 | * @returns psz on success.
|
---|
518 | * @returns NULL on buffer underflow.
|
---|
519 | * @param pTime The time spec.
|
---|
520 | * @param psz Where to store the string.
|
---|
521 | * @param cb The size of the buffer.
|
---|
522 | */
|
---|
523 | RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
|
---|
524 |
|
---|
525 | /** @} */
|
---|
526 |
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Exploded time.
|
---|
530 | */
|
---|
531 | #pragma pack(1)
|
---|
532 | typedef struct RTTIME
|
---|
533 | {
|
---|
534 | /** The year number. */
|
---|
535 | int32_t i32Year;
|
---|
536 | /** The month of the year (1-12). January is 1. */
|
---|
537 | uint8_t u8Month;
|
---|
538 | /** The day of the week (0-6). Monday is 0. */
|
---|
539 | uint8_t u8WeekDay;
|
---|
540 | /** The day of the year (1-366). January the 1st is 1. */
|
---|
541 | uint16_t u16YearDay;
|
---|
542 | /** The day of the month (1-31). */
|
---|
543 | uint8_t u8MonthDay;
|
---|
544 | /** Hour of the day (0-23). */
|
---|
545 | uint8_t u8Hour;
|
---|
546 | /** The minute of the hour (0-59). */
|
---|
547 | uint8_t u8Minute;
|
---|
548 | /** The second of the minute (0-60).
|
---|
549 | * (u32Nanosecond / 1000000) */
|
---|
550 | uint8_t u8Second;
|
---|
551 | /** The nanoseconds of the second (0-999999999). */
|
---|
552 | uint32_t u32Nanosecond;
|
---|
553 | /** Flags, of the RTTIME_FLAGS_* \#defines. */
|
---|
554 | uint32_t fFlags;
|
---|
555 | /** @todo we need a UTC offset field. */
|
---|
556 | } RTTIME;
|
---|
557 | #pragma pack()
|
---|
558 | /** Pointer to a exploded time structure. */
|
---|
559 | typedef RTTIME *PRTTIME;
|
---|
560 | /** Pointer to a const exploded time structure. */
|
---|
561 | typedef const RTTIME *PCRTTIME;
|
---|
562 |
|
---|
563 | /** @name RTTIME::fFlags values.
|
---|
564 | * @{ */
|
---|
565 | /** Set if the time is UTC. If clear the time local time. */
|
---|
566 | #define RTTIME_FLAGS_TYPE_MASK 3
|
---|
567 | /** the time is UTC time. */
|
---|
568 | #define RTTIME_FLAGS_TYPE_UTC 2
|
---|
569 | /** The time is local time. */
|
---|
570 | #define RTTIME_FLAGS_TYPE_LOCAL 3
|
---|
571 |
|
---|
572 | /** Set if the time is local and daylight saving time is in effect.
|
---|
573 | * Not bit is not valid if RTTIME_FLAGS_NO_DST_DATA is set. */
|
---|
574 | #define RTTIME_FLAGS_DST BIT(4)
|
---|
575 | /** Set if the time is local and there is no data available on daylight saving time. */
|
---|
576 | #define RTTIME_FLAGS_NO_DST_DATA BIT(5)
|
---|
577 | /** Set if the year is a leap year.
|
---|
578 | * This is mutual exclusiv with RTTIME_FLAGS_COMMON_YEAR. */
|
---|
579 | #define RTTIME_FLAGS_LEAP_YEAR BIT(6)
|
---|
580 | /** Set if the year is a common year.
|
---|
581 | * This is mutual exclusiv with RTTIME_FLAGS_LEAP_YEAR. */
|
---|
582 | #define RTTIME_FLAGS_COMMON_YEAR BIT(7)
|
---|
583 | /** @} */
|
---|
584 |
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Gets the current system time (UTC).
|
---|
588 | *
|
---|
589 | * @returns pTime.
|
---|
590 | * @param pTime Where to store the time.
|
---|
591 | */
|
---|
592 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime);
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Explodes a time spec (UTC).
|
---|
596 | *
|
---|
597 | * @returns pTime.
|
---|
598 | * @param pTime Where to store the exploded time.
|
---|
599 | * @param pTimeSpec The time spec to exploded.
|
---|
600 | */
|
---|
601 | RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * Implodes exploded time to a time spec (UTC).
|
---|
605 | *
|
---|
606 | * @returns pTime on success.
|
---|
607 | * @returns NULL if the pTime data is invalid.
|
---|
608 | * @param pTimeSpec Where to store the imploded UTC time.
|
---|
609 | * If pTime specifies a time which outside the range, maximum or
|
---|
610 | * minimum values will be returned.
|
---|
611 | * @param pTime Pointer to the exploded time to implode.
|
---|
612 | * The fields u8Month, u8WeekDay and u8MonthDay are not used,
|
---|
613 | * and all the other fields are expected to be within their
|
---|
614 | * bounds. Use RTTimeNormalize() to calculate u16YearDay and
|
---|
615 | * normalize the ranges of the fields.
|
---|
616 | */
|
---|
617 | RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime);
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Normalizes the fields of a timestructure.
|
---|
621 | *
|
---|
622 | * It is possible to calculate month/day fields in some
|
---|
623 | * combinations. It's also possible to overflow other
|
---|
624 | * fields, and these overflows will be adjusted for.
|
---|
625 | *
|
---|
626 | * @returns pTime on success.
|
---|
627 | * @returns NULL if the data is invalid.
|
---|
628 | * @param pTime The time structure to normalize.
|
---|
629 | */
|
---|
630 | RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime);
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Gets the current local system time.
|
---|
634 | *
|
---|
635 | * @returns pTime.
|
---|
636 | * @param pTime Where to store the local time.
|
---|
637 | */
|
---|
638 | RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime);
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Gets the delta between UTC and local time.
|
---|
642 | *
|
---|
643 | * @code
|
---|
644 | * RTTIMESPEC LocalTime;
|
---|
645 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
646 | * @endcode
|
---|
647 | *
|
---|
648 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
649 | */
|
---|
650 | RTDECL(int64_t) RTTimeLocalDeltaNano(void);
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Explodes a time spec to the localized timezone.
|
---|
654 | *
|
---|
655 | * @returns pTime.
|
---|
656 | * @param pTime Where to store the exploded time.
|
---|
657 | * @param pTimeSpec The time spec to exploded (UTC).
|
---|
658 | */
|
---|
659 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec);
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Converts a time spec to a ISO date string.
|
---|
663 | *
|
---|
664 | * @returns psz on success.
|
---|
665 | * @returns NULL on buffer underflow.
|
---|
666 | * @param pTime The time. Caller should've normalized this.
|
---|
667 | * @param psz Where to store the string.
|
---|
668 | * @param cb The size of the buffer.
|
---|
669 | */
|
---|
670 | RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb);
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Gets the current nanosecond timestamp.
|
---|
674 | *
|
---|
675 | * @returns nanosecond timestamp.
|
---|
676 | */
|
---|
677 | RTDECL(uint64_t) RTTimeNanoTS(void);
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Gets the current millisecond timestamp.
|
---|
681 | *
|
---|
682 | * @returns millisecond timestamp.
|
---|
683 | */
|
---|
684 | RTDECL(uint64_t) RTTimeMilliTS(void);
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Debugging the time api.
|
---|
688 | *
|
---|
689 | * @returns the number of 1ns steps which has been applied by rtTimeNanoTSInternal().
|
---|
690 | */
|
---|
691 | RTDECL(uint32_t) RTTime1nsSteps(void);
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Gets the current nanosecond timestamp.
|
---|
695 | *
|
---|
696 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
697 | * resolution or performance optimizations.
|
---|
698 | *
|
---|
699 | * @returns nanosecond timestamp.
|
---|
700 | */
|
---|
701 | RTDECL(uint64_t) RTTimeSystemNanoTS(void);
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Gets the current millisecond timestamp.
|
---|
705 | *
|
---|
706 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
707 | * resolution or performance optimizations.
|
---|
708 | *
|
---|
709 | * @returns millisecond timestamp.
|
---|
710 | */
|
---|
711 | RTDECL(uint64_t) RTTimeSystemMilliTS(void);
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Get the nanosecond timestamp relative to program startup.
|
---|
715 | *
|
---|
716 | * @returns Timestamp relative to program startup.
|
---|
717 | */
|
---|
718 | RTDECL(uint64_t) RTTimeProgramNanoTS(void);
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * Get the microsecond timestamp relative to program startup.
|
---|
722 | *
|
---|
723 | * @returns Timestamp relative to program startup.
|
---|
724 | */
|
---|
725 | RTDECL(uint64_t) RTTimeProgramMicroTS(void);
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Get the millisecond timestamp relative to program startup.
|
---|
729 | *
|
---|
730 | * @returns Timestamp relative to program startup.
|
---|
731 | */
|
---|
732 | RTDECL(uint64_t) RTTimeProgramMilliTS(void);
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * Get the second timestamp relative to program startup.
|
---|
736 | *
|
---|
737 | * @returns Timestamp relative to program startup.
|
---|
738 | */
|
---|
739 | RTDECL(uint32_t) RTTimeProgramSecTS(void);
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * Get the RTTimeNanoTS() of when the program started.
|
---|
743 | *
|
---|
744 | * @returns Program startup timestamp.
|
---|
745 | */
|
---|
746 | RTDECL(uint64_t) RTTimeProgramStartNanoTS(void);
|
---|
747 |
|
---|
748 | /** @} */
|
---|
749 |
|
---|
750 | __END_DECLS
|
---|
751 |
|
---|
752 | #endif
|
---|
753 |
|
---|