summaryrefslogtreecommitdiffstats
path: root/ctdb/lib/util/util_time.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2007-06-07 22:30:29 +1000
committerAndrew Tridgell <tridge@samba.org>2007-06-07 22:30:29 +1000
commit14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0 (patch)
tree1137f34391f0bbe3ce0aa7a23e2798b78565bdc7 /ctdb/lib/util/util_time.c
parent06a71762a41f8602ccfdf21e6a6bbe884c9d1a4f (diff)
downloadsamba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.tar.gz
samba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.tar.xz
samba-14c788f3cb7b1d77a8046ad34fa09a9ea826ebd0.zip
move more util code to lib/util
(This used to be ctdb commit de5ab0584c978a6be4afeacd80c84015b206a3c6)
Diffstat (limited to 'ctdb/lib/util/util_time.c')
-rw-r--r--ctdb/lib/util/util_time.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/ctdb/lib/util/util_time.c b/ctdb/lib/util/util_time.c
new file mode 100644
index 0000000000..13ddfdb2b6
--- /dev/null
+++ b/ctdb/lib/util/util_time.c
@@ -0,0 +1,102 @@
+/*
+ functions taken from samba4 for quick prototyping of ctdb. These are
+ not intended to remain part of ctdb
+*/
+
+#include "includes.h"
+#include "system/time.h"
+#include "system/filesys.h"
+
+
+/**
+ return a zero timeval
+*/
+struct timeval timeval_zero(void)
+{
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ return tv;
+}
+
+/**
+ return True if a timeval is zero
+*/
+bool timeval_is_zero(const struct timeval *tv)
+{
+ return tv->tv_sec == 0 && tv->tv_usec == 0;
+}
+
+/**
+ return a timeval for the current time
+*/
+struct timeval timeval_current(void)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv;
+}
+
+double timeval_elapsed(struct timeval *tv)
+{
+ struct timeval tv2 = timeval_current();
+ return (tv2.tv_sec - tv->tv_sec) +
+ (tv2.tv_usec - tv->tv_usec)*1.0e-6;
+}
+
+/**
+ return a timeval struct with the given elements
+*/
+_PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
+{
+ struct timeval tv;
+ tv.tv_sec = secs;
+ tv.tv_usec = usecs;
+ return tv;
+}
+
+_PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
+{
+ if (tv1->tv_sec > tv2->tv_sec) return 1;
+ if (tv1->tv_sec < tv2->tv_sec) return -1;
+ if (tv1->tv_usec > tv2->tv_usec) return 1;
+ if (tv1->tv_usec < tv2->tv_usec) return -1;
+ return 0;
+}
+
+_PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
+ const struct timeval *tv2)
+{
+ struct timeval t;
+ if (timeval_compare(tv1, tv2) >= 0) {
+ return timeval_zero();
+ }
+ t.tv_sec = tv2->tv_sec - tv1->tv_sec;
+ if (tv1->tv_usec > tv2->tv_usec) {
+ t.tv_sec--;
+ t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
+ } else {
+ t.tv_usec = tv2->tv_usec - tv1->tv_usec;
+ }
+ return t;
+}
+
+static struct timeval timeval_add(const struct timeval *tv,
+ uint32_t secs, uint32_t usecs)
+{
+ struct timeval tv2 = *tv;
+ const unsigned int million = 1000000;
+ tv2.tv_sec += secs;
+ tv2.tv_usec += usecs;
+ tv2.tv_sec += tv2.tv_usec / million;
+ tv2.tv_usec = tv2.tv_usec % million;
+ return tv2;
+}
+
+
+_PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
+{
+ struct timeval tv = timeval_current();
+ return timeval_add(&tv, secs, usecs);
+}
+