TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / lib / util / util_time.c
1 /*
2   functions taken from samba4 for quick prototyping of ctdb. These are
3   not intended to remain part of ctdb
4 */
5
6 #include "includes.h"
7 #include "system/time.h"
8 #include "system/filesys.h"
9
10
11 /**
12   return a zero timeval
13 */
14 struct timeval timeval_zero(void)
15 {
16         struct timeval tv;
17         tv.tv_sec = 0;
18         tv.tv_usec = 0;
19         return tv;
20 }
21
22 /**
23   return True if a timeval is zero
24 */
25 bool timeval_is_zero(const struct timeval *tv)
26 {
27         return tv->tv_sec == 0 && tv->tv_usec == 0;
28 }
29
30 /**
31   return a timeval for the current time
32 */
33 struct timeval timeval_current(void)
34 {
35         struct timeval tv;
36         gettimeofday(&tv, NULL);
37         return tv;
38 }
39
40 double timeval_elapsed(struct timeval *tv)
41 {
42         struct timeval tv2 = timeval_current();
43         return (tv2.tv_sec - tv->tv_sec) + 
44                (tv2.tv_usec - tv->tv_usec)*1.0e-6;
45 }
46
47 double timeval_delta(struct timeval *tv2, struct timeval *tv)
48 {
49         return (tv2->tv_sec - tv->tv_sec) + 
50                (tv2->tv_usec - tv->tv_usec)*1.0e-6;
51 }
52
53 /**
54   return a timeval struct with the given elements
55 */
56 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
57 {
58         struct timeval tv;
59         tv.tv_sec = secs;
60         tv.tv_usec = usecs;
61         return tv;
62 }
63
64 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
65 {
66         if (tv1->tv_sec  > tv2->tv_sec)  return 1;
67         if (tv1->tv_sec  < tv2->tv_sec)  return -1;
68         if (tv1->tv_usec > tv2->tv_usec) return 1;
69         if (tv1->tv_usec < tv2->tv_usec) return -1;
70         return 0;
71 }
72
73 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
74                                       const struct timeval *tv2)
75 {
76         struct timeval t;
77         if (timeval_compare(tv1, tv2) >= 0) {
78                 return timeval_zero();
79         }
80         t.tv_sec = tv2->tv_sec - tv1->tv_sec;
81         if (tv1->tv_usec > tv2->tv_usec) {
82                 t.tv_sec--;
83                 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
84         } else {
85                 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
86         }
87         return t;
88 }
89
90 static struct timeval timeval_add(const struct timeval *tv,
91                            uint32_t secs, uint32_t usecs)
92 {
93         struct timeval tv2 = *tv;
94         const unsigned int million = 1000000;
95         tv2.tv_sec += secs;
96         tv2.tv_usec += usecs;
97         tv2.tv_sec += tv2.tv_usec / million;
98         tv2.tv_usec = tv2.tv_usec % million;
99         return tv2;
100 }
101
102
103 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
104 {
105         struct timeval tv = timeval_current();
106         return timeval_add(&tv, secs, usecs);
107 }
108