summaryrefslogtreecommitdiff
path: root/lib/ccan/time
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ccan/time')
-rw-r--r--lib/ccan/time/LICENSE17
-rw-r--r--lib/ccan/time/_info47
-rw-r--r--lib/ccan/time/test/run.c111
-rw-r--r--lib/ccan/time/time.c108
-rw-r--r--lib/ccan/time/time.d25
-rw-r--r--lib/ccan/time/time.h184
6 files changed, 492 insertions, 0 deletions
diff --git a/lib/ccan/time/LICENSE b/lib/ccan/time/LICENSE
new file mode 100644
index 0000000000..89de354795
--- /dev/null
+++ b/lib/ccan/time/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/lib/ccan/time/_info b/lib/ccan/time/_info
new file mode 100644
index 0000000000..fdad27561a
--- /dev/null
+++ b/lib/ccan/time/_info
@@ -0,0 +1,47 @@
+#include <string.h>
+#include "config.h"
+
+/**
+ * time - routines for dealing with time
+ *
+ * This code provides convenient functions for working with time.
+ *
+ * Author: Rusty Russell <rusty@rustcorp.com.au>
+ * License: BSD-MIT
+ *
+ * Example:
+ * #include <ccan/time/time.h>
+ * #include <stdlib.h>
+ * #include <stdio.h>
+ * #include <err.h>
+ *
+ * int main(int argc, char *argv[])
+ * {
+ * struct timeval t;
+ *
+ * if (argc != 2)
+ * errx(1, "Usage: %s <diff in millisec>", argv[0]);
+ *
+ * t = time_now();
+ * if (argv[1][0] == '-')
+ * t = time_sub(t, time_from_msec(atol(argv[1]+1)));
+ * else
+ * t = time_add(t, time_from_msec(atol(argv[1])));
+ *
+ * printf("%lu.%06u\n",
+ * (unsigned long)t.tv_sec, (unsigned)t.tv_usec);
+ * return 0;
+ * }
+ */
+int main(int argc, char *argv[])
+{
+ /* Expect exactly one argument */
+ if (argc != 2)
+ return 1;
+
+ if (strcmp(argv[1], "depends") == 0) {
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/lib/ccan/time/test/run.c b/lib/ccan/time/test/run.c
new file mode 100644
index 0000000000..80391694cd
--- /dev/null
+++ b/lib/ccan/time/test/run.c
@@ -0,0 +1,111 @@
+#include <ccan/time/time.h>
+#include <ccan/time/time.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+ struct timeval t1, t2, t3, zero = { 0, 0 };
+
+ plan_tests(46);
+
+ /* Test time_now */
+ t1 = time_now();
+ t2 = time_now();
+
+ /* Test time_sub. */
+ t3 = time_sub(t2, t1);
+ ok1(t3.tv_sec > 0 || t3.tv_usec >= 0);
+ t3 = time_sub(t2, t2);
+ ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
+ t3 = time_sub(t1, t1);
+ ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
+
+ /* Test time_eq */
+ ok1(time_eq(t1, t1));
+ ok1(time_eq(t2, t2));
+ ok1(!time_eq(t1, t3));
+ ok1(!time_eq(t2, t3));
+
+ /* Make sure t2 > t1. */
+ t3.tv_sec = 0;
+ t3.tv_usec = 1;
+ t2 = time_add(t2, t3);
+
+ /* Test time_less and time_greater. */
+ ok1(!time_eq(t1, t2));
+ ok1(!time_greater(t1, t2));
+ ok1(time_less(t1, t2));
+ ok1(time_greater(t2, t1));
+ ok1(!time_less(t2, t1));
+ t3.tv_sec = 0;
+ t3.tv_usec = 999999;
+ t2 = time_add(t2, t3);
+ ok1(!time_eq(t1, t2));
+ ok1(!time_greater(t1, t2));
+ ok1(time_less(t1, t2));
+ ok1(time_greater(t2, t1));
+ ok1(!time_less(t2, t1));
+
+ t3 = time_sub(t2, zero);
+ ok1(time_eq(t3, t2));
+ t3 = time_sub(t2, t2);
+ ok1(time_eq(t3, zero));
+
+ /* time_from_msec / time_to_msec */
+ t3 = time_from_msec(500);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_msec(t3) == 500);
+
+ t3 = time_from_msec(1000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 0);
+ ok1(time_to_msec(t3) == 1000);
+
+ t3 = time_from_msec(1500);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_msec(t3) == 1500);
+
+ /* time_from_usec */
+ t3 = time_from_usec(500000);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_usec(t3) == 500000);
+
+ t3 = time_from_usec(1000000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 0);
+ ok1(time_to_usec(t3) == 1000000);
+
+ t3 = time_from_usec(1500000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_usec(t3) == 1500000);
+
+ /* Test wrapunder */
+ t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
+ ok1(t3.tv_sec == t2.tv_sec - 1);
+ ok1(t3.tv_usec == t2.tv_usec);
+
+ /* time_divide and time_multiply */
+ t1.tv_usec = 100;
+ t1.tv_sec = 100;
+
+ t3 = time_divide(t1, 2);
+ ok1(t3.tv_sec == 50);
+ ok1(t3.tv_usec == 50);
+
+ t3 = time_divide(t1, 100);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 1);
+
+ t3 = time_multiply(t3, 100);
+ ok1(time_eq(t3, t1));
+
+ t3 = time_divide(t1, 200);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+
+ return exit_status();
+}
diff --git a/lib/ccan/time/time.c b/lib/ccan/time/time.c
new file mode 100644
index 0000000000..5e36bf7b48
--- /dev/null
+++ b/lib/ccan/time/time.c
@@ -0,0 +1,108 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#include <ccan/time/time.h>
+#include <stdlib.h>
+#include <assert.h>
+
+struct timeval time_now(void)
+{
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ return now;
+}
+
+bool time_greater(struct timeval a, struct timeval b)
+{
+ if (a.tv_sec > b.tv_sec)
+ return true;
+ else if (a.tv_sec < b.tv_sec)
+ return false;
+
+ return a.tv_usec > b.tv_usec;
+}
+
+bool time_less(struct timeval a, struct timeval b)
+{
+ if (a.tv_sec < b.tv_sec)
+ return true;
+ else if (a.tv_sec > b.tv_sec)
+ return false;
+
+ return a.tv_usec < b.tv_usec;
+}
+
+bool time_eq(struct timeval a, struct timeval b)
+{
+ return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec;
+}
+
+struct timeval time_sub(struct timeval recent, struct timeval old)
+{
+ struct timeval diff;
+
+ diff.tv_sec = recent.tv_sec - old.tv_sec;
+ if (old.tv_usec > recent.tv_usec) {
+ diff.tv_sec--;
+ diff.tv_usec = 1000000 + recent.tv_usec - old.tv_usec;
+ } else
+ diff.tv_usec = recent.tv_usec - old.tv_usec;
+
+ assert(diff.tv_sec >= 0);
+ return diff;
+}
+
+struct timeval time_add(struct timeval a, struct timeval b)
+{
+ struct timeval sum;
+
+ sum.tv_sec = a.tv_sec + b.tv_sec;
+ sum.tv_usec = a.tv_usec + b.tv_usec;
+ if (sum.tv_usec > 1000000) {
+ sum.tv_sec++;
+ sum.tv_usec -= 1000000;
+ }
+ return sum;
+}
+
+struct timeval time_divide(struct timeval t, unsigned long div)
+{
+ return time_from_usec(time_to_usec(t) / div);
+}
+
+struct timeval time_multiply(struct timeval t, unsigned long mult)
+{
+ return time_from_usec(time_to_usec(t) * mult);
+}
+
+uint64_t time_to_msec(struct timeval t)
+{
+ uint64_t msec;
+
+ msec = t.tv_usec / 1000 + (uint64_t)t.tv_sec * 1000;
+ return msec;
+}
+
+uint64_t time_to_usec(struct timeval t)
+{
+ uint64_t usec;
+
+ usec = t.tv_usec + (uint64_t)t.tv_sec * 1000000;
+ return usec;
+}
+
+struct timeval time_from_msec(uint64_t msec)
+{
+ struct timeval t;
+
+ t.tv_usec = (msec % 1000) * 1000;
+ t.tv_sec = msec / 1000;
+ return t;
+}
+
+struct timeval time_from_usec(uint64_t usec)
+{
+ struct timeval t;
+
+ t.tv_usec = usec % 1000000;
+ t.tv_sec = usec / 1000000;
+ return t;
+}
diff --git a/lib/ccan/time/time.d b/lib/ccan/time/time.d
new file mode 100644
index 0000000000..d61a6265ea
--- /dev/null
+++ b/lib/ccan/time/time.d
@@ -0,0 +1,25 @@
+ccan/time/time.o: ccan/time/time.c ccan/time/time.h config.h \
+ /usr/include/i386-linux-gnu/sys/time.h /usr/include/features.h \
+ /usr/include/i386-linux-gnu/bits/predefs.h \
+ /usr/include/i386-linux-gnu/sys/cdefs.h \
+ /usr/include/i386-linux-gnu/bits/wordsize.h \
+ /usr/include/i386-linux-gnu/gnu/stubs.h \
+ /usr/include/i386-linux-gnu/gnu/stubs-32.h \
+ /usr/include/i386-linux-gnu/bits/types.h \
+ /usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/time.h \
+ /usr/include/i386-linux-gnu/bits/time.h \
+ /usr/include/i386-linux-gnu/sys/select.h \
+ /usr/include/i386-linux-gnu/bits/select.h \
+ /usr/include/i386-linux-gnu/bits/sigset.h \
+ /usr/lib/gcc/i686-linux-gnu/4.5.4/include/stdint.h /usr/include/stdint.h \
+ /usr/include/i386-linux-gnu/bits/wchar.h \
+ /usr/lib/gcc/i686-linux-gnu/4.5.4/include/stdbool.h \
+ /usr/include/stdlib.h /usr/lib/gcc/i686-linux-gnu/4.5.4/include/stddef.h \
+ /usr/include/i386-linux-gnu/bits/waitflags.h \
+ /usr/include/i386-linux-gnu/bits/waitstatus.h /usr/include/endian.h \
+ /usr/include/i386-linux-gnu/bits/endian.h \
+ /usr/include/i386-linux-gnu/bits/byteswap.h /usr/include/xlocale.h \
+ /usr/include/i386-linux-gnu/sys/types.h \
+ /usr/include/i386-linux-gnu/sys/sysmacros.h \
+ /usr/include/i386-linux-gnu/bits/pthreadtypes.h /usr/include/alloca.h \
+ /usr/include/assert.h
diff --git a/lib/ccan/time/time.h b/lib/ccan/time/time.h
new file mode 100644
index 0000000000..fb2ee459d6
--- /dev/null
+++ b/lib/ccan/time/time.h
@@ -0,0 +1,184 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#ifndef CCAN_TIME_H
+#define CCAN_TIME_H
+#include "config.h"
+#include <sys/time.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+/**
+ * time_now - return the current time
+ *
+ * Example:
+ * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
+ */
+struct timeval time_now(void);
+
+/**
+ * time_greater - is a after b?
+ * @a: one time.
+ * @b: another time.
+ *
+ * Example:
+ * static bool timed_out(const struct timeval *start)
+ * {
+ * #define TIMEOUT time_from_msec(1000)
+ * return time_greater(time_now(), time_add(*start, TIMEOUT));
+ * }
+ */
+bool time_greater(struct timeval a, struct timeval b);
+
+/**
+ * time_less - is a before b?
+ * @a: one time.
+ * @b: another time.
+ *
+ * Example:
+ * static bool still_valid(const struct timeval *start)
+ * {
+ * #define TIMEOUT time_from_msec(1000)
+ * return time_less(time_now(), time_add(*start, TIMEOUT));
+ * }
+ */
+bool time_less(struct timeval a, struct timeval b);
+
+/**
+ * time_eq - is a equal to b?
+ * @a: one time.
+ * @b: another time.
+ *
+ * Example:
+ * #include <sys/types.h>
+ * #include <sys/wait.h>
+ *
+ * // Can we fork in under a microsecond?
+ * static bool fast_fork(void)
+ * {
+ * struct timeval start = time_now();
+ * if (fork() != 0) {
+ * exit(0);
+ * }
+ * wait(NULL);
+ * return time_eq(start, time_now());
+ * }
+ */
+bool time_eq(struct timeval a, struct timeval b);
+
+/**
+ * time_sub - subtract two times
+ * @recent: the larger (more recent) time.
+ * @old: the smaller (less recent) time.
+ *
+ * This returns a well formed struct timeval.
+ *
+ * Example:
+ * static bool was_recent(const struct timeval *start)
+ * {
+ * return time_sub(time_now(), *start).tv_sec < 1;
+ * }
+ */
+struct timeval time_sub(struct timeval recent, struct timeval old);
+
+/**
+ * time_add - add two times
+ * @a: one time.
+ * @b: another time.
+ *
+ * The times must not overflow, or the results are undefined.
+ *
+ * Example:
+ * // We do one every second.
+ * static struct timeval next_time(void)
+ * {
+ * return time_add(time_now(), time_from_msec(1000));
+ * }
+ */
+struct timeval time_add(struct timeval a, struct timeval b);
+
+/**
+ * time_divide - divide a time by a value.
+ * @t: a time.
+ * @div: number to divide it by.
+ *
+ * Example:
+ * // How long does it take to do a fork?
+ * static struct timeval forking_time(void)
+ * {
+ * struct timeval start = time_now();
+ * unsigned int i;
+ *
+ * for (i = 0; i < 1000; i++) {
+ * if (fork() != 0) {
+ * exit(0);
+ * }
+ * wait(NULL);
+ * }
+ * return time_divide(time_sub(time_now(), start), i);
+ * }
+ */
+struct timeval time_divide(struct timeval t, unsigned long div);
+
+/**
+ * time_multiply - multiply a time by a value.
+ * @t: a time.
+ * @mult: number to multiply it by.
+ *
+ * Example:
+ * ...
+ * printf("Time to do 100000 forks would be %u sec\n",
+ * (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
+ */
+struct timeval time_multiply(struct timeval t, unsigned long mult);
+
+/**
+ * time_to_msec - return number of milliseconds
+ * @t: a time
+ *
+ * It's often more convenient to deal with time values as
+ * milliseconds. Note that this will fit into a 32-bit variable if
+ * it's a time difference of less than ~7 weeks.
+ *
+ * Example:
+ * ...
+ * printf("Forking time is %u msec\n",
+ * (unsigned)time_to_msec(forking_time()));
+ */
+uint64_t time_to_msec(struct timeval t);
+
+/**
+ * time_to_usec - return number of microseconds
+ * @t: a time
+ *
+ * It's often more convenient to deal with time values as
+ * microseconds. Note that this will fit into a 32-bit variable if
+ * it's a time difference of less than ~1 hour.
+ *
+ * Example:
+ * ...
+ * printf("Forking time is %u usec\n",
+ * (unsigned)time_to_usec(forking_time()));
+ *
+ */
+uint64_t time_to_usec(struct timeval t);
+
+/**
+ * time_from_msec - convert milliseconds to a timeval
+ * @msec: time in milliseconds
+ *
+ * Example:
+ * // 1/2 second timeout
+ * #define TIMEOUT time_from_msec(500)
+ */
+struct timeval time_from_msec(uint64_t msec);
+
+/**
+ * time_from_usec - convert microseconds to a timeval
+ * @usec: time in microseconds
+ *
+ * Example:
+ * // 1/2 second timeout
+ * #define TIMEOUT time_from_usec(500000)
+ */
+struct timeval time_from_usec(uint64_t usec);
+
+#endif /* CCAN_TIME_H */