Some code to implement the char device that will communicate with the daemon.
[vdi_driver.git] / src / time.h
bloba03fad280ed64af7bf66ce8f9601fa75d140a2a5
1 #ifndef TIME_H
2 #define TIME_H
4 #include "cdefs.h"
5 #include <stdint.h>
6 #include <sys/time.h>
8 /** Time Specification.
10 * Use the inline RTTimeSpecGet/Set to operate on structure this so we
11 * can easily change the representation if required later.
13 * The current representation is in nanoseconds relative to the unix epoch
14 * (1970-01-01 00:00:00 UTC). This gives us an approximate span from
15 * 1678 to 2262 without sacrifying the resolution offered by the various
16 * host OSes (BSD & LINUX 1ns, NT 100ns).
18 typedef struct RTTIMESPEC
20 /** Nanoseconds since epoch.
21 * The name is intentially too long to be comfortable to use because you should be
22 * using inline helpers! */
23 int64_t i64NanosecondsRelativeToUnixEpoch;
24 } RTTIMESPEC;
25 /** Pointer to a time spec structure. */
26 typedef RTTIMESPEC *PRTTIMESPEC;
27 /** Pointer to a const time spec structure. */
28 typedef const RTTIMESPEC *PCRTTIMESPEC;
31 /**
32 * Gets the time as microseconds relative to the unix epoch.
34 * @returns microseconds relative to unix epoch.
35 * @param pTime The time spec to interpret.
37 DECLINLINE(int64_t) RTTimeSpecGetMicro(PCRTTIMESPEC pTime)
39 return pTime->i64NanosecondsRelativeToUnixEpoch / 1000;
42 /**
43 * Gets the time as POSIX timeval.
45 * @returns pTime.
46 * @param pTime The time spec to interpret.
47 * @param pTimeval Where to store the time as POSIX timeval.
49 DECLINLINE(struct timeval *) RTTimeSpecGetTimeval(PCRTTIMESPEC pTime, struct timeval *pTimeval)
51 int64_t i64 = RTTimeSpecGetMicro(pTime);
52 int32_t i32Micro = (int32_t)(i64 % 1000000);
53 i64 /= 1000000;
54 if (i32Micro < 0)
56 i32Micro += 1000000;
57 i64++;
59 pTimeval->tv_sec = (time_t)i64;
60 pTimeval->tv_usec = i32Micro;
61 return pTimeval;
64 /**
65 * Sets the time given by seconds relative to the unix epoch.
67 * @returns pTime.
68 * @param pTime The time spec to modify.
69 * @param i64Seconds The new time in seconds.
71 DECLINLINE(PRTTIMESPEC) RTTimeSpecSetSeconds(PRTTIMESPEC pTime, int64_t i64Seconds)
73 pTime->i64NanosecondsRelativeToUnixEpoch = i64Seconds * 1000000000;
74 return pTime;
77 #endif