struct timemono t1, t2;
struct timerel t3;
- plan_tests(5);
+ plan_tests(7);
/* Test time_mono */
t1 = time_mono();
t2 = time_mono();
- ok1(!time_less_(t2.ts, t1.ts));
+ ok1(!timemono_before(t2, t1));
t3.ts.tv_sec = 1;
t3.ts.tv_nsec = 0;
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);
+ ok1(timemono_sub(timemono_add(t1, t3), t3).ts.tv_sec == t1.ts.tv_sec);
+ ok1(timemono_sub(timemono_add(t1, t3), t3).ts.tv_nsec == t1.ts.tv_nsec);
+
return exit_status();
}
return time_less_(a.ts, b.ts);
}
+/**
+ * timemono_before - is a before b?
+ * @a: one monotonic time.
+ * @b: another monotonic time.
+ *
+ * Example:
+ * static bool still_valid(const struct timemono *start)
+ * {
+ * #define TIMEOUT time_from_msec(1000)
+ * return timemono_before(time_mono(), timemono_add(*start, TIMEOUT));
+ * }
+ */
+static inline bool timemono_before(struct timemono a, struct timemono b)
+{
+ return time_less_(a.ts, b.ts);
+}
+
/**
* time_less - is a before b?
* @a: one relative time.
return t;
}
+/**
+ * timemono_sub - subtract a relative time from a monotonic time
+ * @mono: the monotonic time.
+ * @rel: the relative time.
+ *
+ * This returns a well formed struct timemono of @mono - @rel.
+ *
+ * Example:
+ * // We do one every second.
+ * static struct timemono previous_time(void)
+ * {
+ * return timemono_sub(time_mono(), time_from_msec(1000));
+ * }
+ */
+static inline struct timemono timemono_sub(struct timemono mono, struct timerel rel)
+{
+ struct timemono t;
+
+ t.ts = time_sub_(mono.ts, rel.ts);
+ return t;
+}
+
+
static inline struct timespec time_add_(struct timespec a, struct timespec b)
{
struct timespec sum;