1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/time/time.h>
6 #if !HAVE_CLOCK_GETTIME && !HAVE_CLOCK_GETTIME_IN_LIBRT
9 struct timespec time_now(void)
13 gettimeofday(&now, NULL);
14 ret.tv_sec = now.tv_sec;
15 ret.tv_nsec = now.tv_usec * 1000;
20 struct timespec time_now(void)
23 clock_gettime(CLOCK_REALTIME, &ret);
26 #endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT */
28 bool time_greater(struct timespec a, struct timespec b)
30 if (a.tv_sec > b.tv_sec)
32 else if (a.tv_sec < b.tv_sec)
35 return a.tv_nsec > b.tv_nsec;
38 bool time_less(struct timespec a, struct timespec b)
40 if (a.tv_sec < b.tv_sec)
42 else if (a.tv_sec > b.tv_sec)
45 return a.tv_nsec < b.tv_nsec;
48 bool time_eq(struct timespec a, struct timespec b)
50 return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
53 struct timespec time_sub(struct timespec recent, struct timespec old)
57 diff.tv_sec = recent.tv_sec - old.tv_sec;
58 if (old.tv_nsec > recent.tv_nsec) {
60 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
62 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
64 assert(diff.tv_sec >= 0);
68 struct timespec time_add(struct timespec a, struct timespec b)
72 sum.tv_sec = a.tv_sec + b.tv_sec;
73 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
74 if (sum.tv_nsec >= 1000000000) {
76 sum.tv_nsec -= 1000000000;
81 struct timespec time_divide(struct timespec t, unsigned long div)
86 /* Dividing seconds is simple. */
87 res.tv_sec = t.tv_sec / div;
90 /* If we can't fit remainder * 1,000,000,000 in 64 bits? */
91 #if 0 /* ilog is great, but we use fp for multiply anyway. */
93 if (bits + 30 >= 64) {
94 /* Reduce accuracy slightly */
95 rem >>= (bits - (64 - 30));
96 div >>= (bits - (64 - 30));
99 if (rem & ~(((uint64_t)1 << 30) - 1)) {
100 /* FIXME: fp is cheating! */
101 double nsec = rem * 1000000000.0 + t.tv_nsec;
102 res.tv_nsec = nsec / div;
104 ns = rem * 1000000000 + t.tv_nsec;
105 res.tv_nsec = ns / div;
110 struct timespec time_multiply(struct timespec t, unsigned long mult)
114 /* Are we going to overflow if we multiply nsec? */
115 if (mult & ~((1UL << 30) - 1)) {
116 /* FIXME: fp is cheating! */
117 double nsec = (double)t.tv_nsec * mult;
119 res.tv_sec = nsec / 1000000000.0;
120 res.tv_nsec = nsec - (res.tv_sec * 1000000000.0);
122 uint64_t nsec = t.tv_nsec * mult;
124 res.tv_nsec = nsec % 1000000000;
125 res.tv_sec = nsec / 1000000000;
127 res.tv_sec += t.tv_sec * mult;
131 uint64_t time_to_msec(struct timespec t)
135 msec = t.tv_nsec / 1000000 + (uint64_t)t.tv_sec * 1000;
139 uint64_t time_to_usec(struct timespec t)
143 usec = t.tv_nsec / 1000 + (uint64_t)t.tv_sec * 1000000;
147 uint64_t time_to_nsec(struct timespec t)
151 nsec = t.tv_nsec + (uint64_t)t.tv_sec * 1000000000;
155 struct timespec time_from_msec(uint64_t msec)
159 t.tv_nsec = (msec % 1000) * 1000000;
160 t.tv_sec = msec / 1000;
164 struct timespec time_from_usec(uint64_t usec)
168 t.tv_nsec = (usec % 1000000) * 1000;
169 t.tv_sec = usec / 1000000;
173 struct timespec time_from_nsec(uint64_t nsec)
177 t.tv_nsec = nsec % 1000000000;
178 t.tv_sec = nsec / 1000000000;