time: new module for dealing with time.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 31 May 2011 04:14:36 +0000 (13:44 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 31 May 2011 04:14:36 +0000 (13:44 +0930)
ccan/time/LICENSE [new symlink]
ccan/time/_info [new file with mode: 0644]
ccan/time/test/run.c [new file with mode: 0644]
ccan/time/time.c [new file with mode: 0644]
ccan/time/time.h [new file with mode: 0644]

diff --git a/ccan/time/LICENSE b/ccan/time/LICENSE
new file mode 120000 (symlink)
index 0000000..2354d12
--- /dev/null
@@ -0,0 +1 @@
+../../licenses/BSD-MIT
\ No newline at end of file
diff --git a/ccan/time/_info b/ccan/time/_info
new file mode 100644 (file)
index 0000000..fdad275
--- /dev/null
@@ -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/ccan/time/test/run.c b/ccan/time/test/run.c
new file mode 100644 (file)
index 0000000..8039169
--- /dev/null
@@ -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/ccan/time/time.c b/ccan/time/time.c
new file mode 100644 (file)
index 0000000..1f36c08
--- /dev/null
@@ -0,0 +1,107 @@
+#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/ccan/time/time.h b/ccan/time/time.h
new file mode 100644 (file)
index 0000000..e3a5791
--- /dev/null
@@ -0,0 +1,183 @@
+#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 */