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