1 #include <ccan/time/time.h>
5 struct timeval time_now(void)
8 gettimeofday(&now, NULL);
12 bool time_greater(struct timeval a, struct timeval b)
14 if (a.tv_sec > b.tv_sec)
16 else if (a.tv_sec < b.tv_sec)
19 return a.tv_usec > b.tv_usec;
22 bool time_less(struct timeval a, struct timeval b)
24 if (a.tv_sec < b.tv_sec)
26 else if (a.tv_sec > b.tv_sec)
29 return a.tv_usec < b.tv_usec;
32 bool time_eq(struct timeval a, struct timeval b)
34 return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec;
37 struct timeval time_sub(struct timeval recent, struct timeval old)
41 diff.tv_sec = recent.tv_sec - old.tv_sec;
42 if (old.tv_usec > recent.tv_usec) {
44 diff.tv_usec = 1000000 + recent.tv_usec - old.tv_usec;
46 diff.tv_usec = recent.tv_usec - old.tv_usec;
48 assert(diff.tv_sec >= 0);
52 struct timeval time_add(struct timeval a, struct timeval b)
56 sum.tv_sec = a.tv_sec + b.tv_sec;
57 sum.tv_usec = a.tv_usec + b.tv_usec;
58 if (sum.tv_usec > 1000000) {
60 sum.tv_usec -= 1000000;
65 struct timeval time_divide(struct timeval t, unsigned long div)
67 return time_from_usec(time_to_usec(t) / div);
70 struct timeval time_multiply(struct timeval t, unsigned long mult)
72 return time_from_usec(time_to_usec(t) * mult);
75 uint64_t time_to_msec(struct timeval t)
79 msec = t.tv_usec / 1000 + (uint64_t)t.tv_sec * 1000;
83 uint64_t time_to_usec(struct timeval t)
87 usec = t.tv_usec + (uint64_t)t.tv_sec * 1000000;
91 struct timeval time_from_msec(uint64_t msec)
95 t.tv_usec = (msec % 1000) * 1000;
96 t.tv_sec = msec / 1000;
100 struct timeval time_from_usec(uint64_t usec)
104 t.tv_usec = usec % 1000000;
105 t.tv_sec = usec / 1000000;