a90b1cdc6257808d3b30f83f7139bc9681797eea
[dana/dcompmgr.git] / time.c
1 #include "time.h"
2
3 static inline void
4 time_fix(struct timeval *tv)
5 {
6     while (tv->tv_usec >= 1000000) {
7         tv->tv_usec -= 1000000;
8         ++tv->tv_sec;
9     }
10     while (tv->tv_usec < 0) {
11         tv->tv_usec += 1000000;
12         --tv->tv_sec;
13     }
14 }
15
16 void
17 time_add(struct timeval *tv, long microseconds)
18 {
19     tv->tv_usec += microseconds;
20     time_fix(tv);
21 }
22
23 long
24 time_compare(struct timeval *a, struct timeval *b)
25 {
26     long r;
27     if ((r = a->tv_sec - b->tv_sec)) return r;
28     return a->tv_usec - b->tv_usec;
29 }
30
31 void
32 time_difference(struct timeval *a, struct timeval *b, struct timeval *r)
33 {
34     struct timeval v;
35     v.tv_sec = a->tv_sec - b->tv_sec;
36     v.tv_usec = a->tv_usec - b->tv_usec;
37     time_fix(&v);
38     *r = v;
39 }