2 functions taken from samba4 for quick prototyping of ctdb. These are
3 not intended to remain part of ctdb
7 #include "system/time.h"
8 #include "system/filesys.h"
14 struct timeval timeval_zero(void)
23 return True if a timeval is zero
25 bool timeval_is_zero(const struct timeval *tv)
27 return tv->tv_sec == 0 && tv->tv_usec == 0;
31 return a timeval for the current time
33 struct timeval timeval_current(void)
36 gettimeofday(&tv, NULL);
40 double timeval_elapsed(struct timeval *tv)
42 struct timeval tv2 = timeval_current();
43 return (tv2.tv_sec - tv->tv_sec) +
44 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
47 double timeval_delta(struct timeval *tv2, struct timeval *tv)
49 return (tv2->tv_sec - tv->tv_sec) +
50 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
54 return a timeval struct with the given elements
56 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
64 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
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;
73 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
74 const struct timeval *tv2)
77 if (timeval_compare(tv1, tv2) >= 0) {
78 return timeval_zero();
80 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
81 if (tv1->tv_usec > tv2->tv_usec) {
83 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
85 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
90 static struct timeval timeval_add(const struct timeval *tv,
91 uint32_t secs, uint32_t usecs)
93 struct timeval tv2 = *tv;
94 const unsigned int million = 1000000;
97 tv2.tv_sec += tv2.tv_usec / million;
98 tv2.tv_usec = tv2.tv_usec % million;
103 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
105 struct timeval tv = timeval_current();
106 return timeval_add(&tv, secs, usecs);