time: timemono_add.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 30 Oct 2016 05:47:55 +0000 (16:17 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 30 Oct 2016 05:47:55 +0000 (16:17 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/time/test/run-monotonic.c
ccan/time/time.h

index 787ca6820b4c5c1976cdcaae526475eb506fd48f..c9912a73a608a0fcc7b1c92d5b44d95e067b2652 100644 (file)
@@ -7,7 +7,7 @@ int main(void)
        struct timemono t1, t2;
        struct timerel t3;
 
        struct timemono t1, t2;
        struct timerel t3;
 
-       plan_tests(3);
+       plan_tests(5);
 
        /* Test time_mono */
        t1 = time_mono();
 
        /* Test time_mono */
        t1 = time_mono();
@@ -21,5 +21,8 @@ int main(void)
        ok1(time_less(timemono_between(t1, t2), t3));
        ok1(time_less(timemono_since(t1), t3));
 
        ok1(time_less(timemono_between(t1, t2), t3));
        ok1(time_less(timemono_since(t1), t3));
 
+       ok1(timemono_add(t1, t3).ts.tv_sec == t1.ts.tv_sec + 1);
+       ok1(timemono_add(t2, t3).ts.tv_nsec == t2.ts.tv_nsec);
+
        return exit_status();
 }
        return exit_status();
 }
index 54d92c57b89090152a9c1a42e10c656ee0c2e82c..f2b335cf45de0af387c2b0a9e1cba09c73ceb86a 100644 (file)
@@ -388,6 +388,28 @@ static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
        return t;
 }
 
        return t;
 }
 
+/**
+ * timemono_add - add a relative to a monotonic time
+ * @a: the monotonic time.
+ * @b: a relative time.
+ *
+ * The times must not overflow, or the results are undefined.
+ *
+ * Example:
+ *     // We do one every second.
+ *     static struct timemono next_timem(void)
+ *     {
+ *             return timemono_add(time_mono(), time_from_msec(1000));
+ *     }
+ */
+static inline struct timemono timemono_add(struct timemono a, struct timerel b)
+{
+       struct timemono t;
+
+       t.ts = time_add_(a.ts, b.ts);
+       return t;
+}
+
 /**
  * timerel_add - add two relative times
  * @a: one relative time.
 /**
  * timerel_add - add two relative times
  * @a: one relative time.