s3-utils: Do not disable the root check in smbpasswd.
[Samba.git] / lib / ccan / time / time.c
blob5e36bf7b48dec210ac3f9341b9caee53b7164acb
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/time/time.h>
3 #include <stdlib.h>
4 #include <assert.h>
6 struct timeval time_now(void)
8 struct timeval now;
9 gettimeofday(&now, NULL);
10 return now;
13 bool time_greater(struct timeval a, struct timeval b)
15 if (a.tv_sec > b.tv_sec)
16 return true;
17 else if (a.tv_sec < b.tv_sec)
18 return false;
20 return a.tv_usec > b.tv_usec;
23 bool time_less(struct timeval a, struct timeval b)
25 if (a.tv_sec < b.tv_sec)
26 return true;
27 else if (a.tv_sec > b.tv_sec)
28 return false;
30 return a.tv_usec < b.tv_usec;
33 bool time_eq(struct timeval a, struct timeval b)
35 return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec;
38 struct timeval time_sub(struct timeval recent, struct timeval old)
40 struct timeval diff;
42 diff.tv_sec = recent.tv_sec - old.tv_sec;
43 if (old.tv_usec > recent.tv_usec) {
44 diff.tv_sec--;
45 diff.tv_usec = 1000000 + recent.tv_usec - old.tv_usec;
46 } else
47 diff.tv_usec = recent.tv_usec - old.tv_usec;
49 assert(diff.tv_sec >= 0);
50 return diff;
53 struct timeval time_add(struct timeval a, struct timeval b)
55 struct timeval sum;
57 sum.tv_sec = a.tv_sec + b.tv_sec;
58 sum.tv_usec = a.tv_usec + b.tv_usec;
59 if (sum.tv_usec > 1000000) {
60 sum.tv_sec++;
61 sum.tv_usec -= 1000000;
63 return sum;
66 struct timeval time_divide(struct timeval t, unsigned long div)
68 return time_from_usec(time_to_usec(t) / div);
71 struct timeval time_multiply(struct timeval t, unsigned long mult)
73 return time_from_usec(time_to_usec(t) * mult);
76 uint64_t time_to_msec(struct timeval t)
78 uint64_t msec;
80 msec = t.tv_usec / 1000 + (uint64_t)t.tv_sec * 1000;
81 return msec;
84 uint64_t time_to_usec(struct timeval t)
86 uint64_t usec;
88 usec = t.tv_usec + (uint64_t)t.tv_sec * 1000000;
89 return usec;
92 struct timeval time_from_msec(uint64_t msec)
94 struct timeval t;
96 t.tv_usec = (msec % 1000) * 1000;
97 t.tv_sec = msec / 1000;
98 return t;
101 struct timeval time_from_usec(uint64_t usec)
103 struct timeval t;
105 t.tv_usec = usec % 1000000;
106 t.tv_sec = usec / 1000000;
107 return t;