Commit c92b4966 authored by Rusty Russell's avatar Rusty Russell

time: split absolute and relative times.

As suggested by David Gibson.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent cff3316c
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
* time - routines for dealing with time * time - routines for dealing with time
* *
* This code provides convenient functions for working with time, in the * This code provides convenient functions for working with time, in the
* form of 'struct timespec'. * form of 'struct timerel' for durations and 'struct timeabs' for timestamps
* which are light wrappers around struct timespec.
* *
* Author: Rusty Russell <rusty@rustcorp.com.au> * Author: Rusty Russell <rusty@rustcorp.com.au>
* License: BSD-MIT * License: BSD-MIT
...@@ -18,19 +19,19 @@ ...@@ -18,19 +19,19 @@
* *
* int main(int argc, char *argv[]) * int main(int argc, char *argv[])
* { * {
* struct timespec t; * struct timeabs t;
* *
* if (argc != 2) * if (argc != 2)
* errx(1, "Usage: %s <diff in millisec>", argv[0]); * errx(1, "Usage: %s <diff in millisec>", argv[0]);
* *
* t = time_now(); * t = time_now();
* if (argv[1][0] == '-') * if (argv[1][0] == '-')
* t = time_sub(t, time_from_msec(atol(argv[1]+1))); * t = timeabs_sub(t, time_from_msec(atol(argv[1]+1)));
* else * else
* t = time_add(t, time_from_msec(atol(argv[1]))); * t = timeabs_add(t, time_from_msec(atol(argv[1])));
* *
* printf("%lu.%09u\n", * printf("%lu.%09u\n",
* (unsigned long)t.tv_sec, (unsigned)t.tv_nsec); * (unsigned long)t.ts.tv_sec, (unsigned)t.ts.tv_nsec);
* return 0; * return 0;
* } * }
*/ */
......
...@@ -16,140 +16,145 @@ void abort(void) ...@@ -16,140 +16,145 @@ void abort(void)
int main(void) int main(void)
{ {
struct timespec t1, t2, t3, zero = { 0, 0 }; struct timeabs t1, t2, epoch = { { 0, 0 } };
struct timerel t3, t4, zero = { { 0, 0 } };
int fds[2]; int fds[2];
plan_tests(62); plan_tests(64);
/* Test time_now */ /* Test time_now */
t1 = time_now(); t1 = time_now();
t2 = time_now(); t2 = time_now();
/* Test time_sub. */ /* Test time_between. */
t3 = time_sub(t2, t1); t3 = time_between(t2, t1);
ok1(t3.tv_sec > 0 || t3.tv_nsec >= 0); ok1(t3.ts.tv_sec > 0 || t3.ts.tv_nsec >= 0);
t3 = time_sub(t2, t2); t3 = time_between(t2, t2);
ok1(t3.tv_sec == 0 && t3.tv_nsec == 0); ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
t3 = time_sub(t1, t1); t3 = time_between(t1, t1);
ok1(t3.tv_sec == 0 && t3.tv_nsec == 0); ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
/* Test time_eq */ /* Test timeabs_eq / timerel_eq */
ok1(time_eq(t1, t1)); ok1(timeabs_eq(t1, t1));
ok1(time_eq(t2, t2)); ok1(timeabs_eq(t2, t2));
ok1(!time_eq(t1, t3)); ok1(!timeabs_eq(t1, epoch));
ok1(!time_eq(t2, t3)); ok1(!timeabs_eq(t2, epoch));
t3.ts.tv_sec = 1;
ok1(timerel_eq(t3, t3));
ok1(!timerel_eq(t3, zero));
/* Make sure t2 > t1. */ /* Make sure t2 > t1. */
t3.tv_sec = 0; t3.ts.tv_sec = 0;
t3.tv_nsec = 1; t3.ts.tv_nsec = 1;
t2 = time_add(t2, t3); t2 = timeabs_add(t2, t3);
/* Test time_less and time_greater. */ /* Test time_before and time_after. */
ok1(!time_eq(t1, t2)); ok1(!timeabs_eq(t1, t2));
ok1(!time_greater(t1, t2)); ok1(!time_after(t1, t2));
ok1(time_less(t1, t2)); ok1(time_before(t1, t2));
ok1(time_greater(t2, t1)); ok1(time_after(t2, t1));
ok1(!time_less(t2, t1)); ok1(!time_before(t2, t1));
t3.tv_sec = 0; t3.ts.tv_sec = 0;
t3.tv_nsec = 999999999; t3.ts.tv_nsec = 999999999;
t2 = time_add(t2, t3); t2 = timeabs_add(t2, t3);
ok1(!time_eq(t1, t2)); ok1(!timeabs_eq(t1, t2));
ok1(!time_greater(t1, t2)); ok1(!time_after(t1, t2));
ok1(time_less(t1, t2)); ok1(time_before(t1, t2));
ok1(time_greater(t2, t1)); ok1(time_after(t2, t1));
ok1(!time_less(t2, t1)); ok1(!time_before(t2, t1));
t3 = time_sub(t2, zero); t3 = time_between(t2, epoch);
ok1(time_eq(t3, t2)); ok1(t2.ts.tv_sec == t3.ts.tv_sec && t2.ts.tv_nsec == t3.ts.tv_nsec);
t3 = time_sub(t2, t2); t3 = time_between(t2, t2);
ok1(time_eq(t3, zero)); ok1(timerel_eq(t3, zero));
/* time_from_msec / time_to_msec */ /* time_from_msec / time_to_msec */
t3 = time_from_msec(500); t3 = time_from_msec(500);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_msec(t3) == 500); ok1(time_to_msec(t3) == 500);
t3 = time_from_msec(1000); t3 = time_from_msec(1000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_msec(t3) == 1000); ok1(time_to_msec(t3) == 1000);
t3 = time_from_msec(1500); t3 = time_from_msec(1500);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_msec(t3) == 1500); ok1(time_to_msec(t3) == 1500);
/* time_from_usec */ /* time_from_usec */
t3 = time_from_usec(500000); t3 = time_from_usec(500000);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_usec(t3) == 500000); ok1(time_to_usec(t3) == 500000);
t3 = time_from_usec(1000000); t3 = time_from_usec(1000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_usec(t3) == 1000000); ok1(time_to_usec(t3) == 1000000);
t3 = time_from_usec(1500000); t3 = time_from_usec(1500000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_usec(t3) == 1500000); ok1(time_to_usec(t3) == 1500000);
/* time_from_nsec */ /* time_from_nsec */
t3 = time_from_nsec(500000000); t3 = time_from_nsec(500000000);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_nsec(t3) == 500000000); ok1(time_to_nsec(t3) == 500000000);
t3 = time_from_nsec(1000000000); t3 = time_from_nsec(1000000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_nsec(t3) == 1000000000); ok1(time_to_nsec(t3) == 1000000000);
t3 = time_from_nsec(1500000000); t3 = time_from_nsec(1500000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_nsec(t3) == 1500000000); ok1(time_to_nsec(t3) == 1500000000);
/* Test wrapunder */ /* Test wrapunder */
t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500)); t1 = timeabs_sub(timeabs_sub(t2, time_from_msec(500)),
ok1(t3.tv_sec == t2.tv_sec - 1); time_from_msec(500));
ok1(t3.tv_nsec == t2.tv_nsec); ok1(t1.ts.tv_sec == t2.ts.tv_sec - 1);
ok1(t1.ts.tv_nsec == t2.ts.tv_nsec);
/* time_divide and time_multiply */ /* time_divide and time_multiply */
t1.tv_nsec = 100; t4.ts.tv_nsec = 100;
t1.tv_sec = 100; t4.ts.tv_sec = 100;
t3 = time_divide(t1, 2); t3 = time_divide(t4, 2);
ok1(t3.tv_sec == 50); ok1(t3.ts.tv_sec == 50);
ok1(t3.tv_nsec == 50); ok1(t3.ts.tv_nsec == 50);
t3 = time_divide(t1, 100); t3 = time_divide(t4, 100);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 1); ok1(t3.ts.tv_nsec == 1);
t3 = time_multiply(t3, 100); t3 = time_multiply(t3, 100);
ok1(time_eq(t3, t1)); ok1(timerel_eq(t3, t4));
t3 = time_divide(t1, 200); t3 = time_divide(t4, 200);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
/* Divide by huge number. */ /* Divide by huge number. */
t1.tv_sec = (1U << 31) - 1; t4.ts.tv_sec = (1U << 31) - 1;
t1.tv_nsec = 999999999; t4.ts.tv_nsec = 999999999;
t2 = time_divide(t1, 1 << 30); t3 = time_divide(t4, 1 << 30);
/* Allow us to round either way. */ /* Allow us to round either way. */
ok1((t2.tv_sec == 2 && t2.tv_nsec == 0) ok1((t3.ts.tv_sec == 2 && t3.ts.tv_nsec == 0)
|| (t2.tv_sec == 1 && t2.tv_nsec == 999999999)); || (t3.ts.tv_sec == 1 && t3.ts.tv_nsec == 999999999));
/* Multiply by huge number. */ /* Multiply by huge number. */
t1.tv_sec = 0; t4.ts.tv_sec = 0;
t1.tv_nsec = 1; t4.ts.tv_nsec = 1;
t2 = time_multiply(t1, 1UL << 31); t3 = time_multiply(t4, 1UL << 31);
ok1(t2.tv_sec == 2); ok1(t3.ts.tv_sec == 2);
ok1(t2.tv_nsec == 147483648); ok1(t3.ts.tv_nsec == 147483648);
pipe(fds); pipe(fds);
...@@ -159,20 +164,20 @@ int main(void) ...@@ -159,20 +164,20 @@ int main(void)
close(fds[0]); close(fds[0]);
dup2(fds[1], 1); dup2(fds[1], 1);
dup2(fds[1], 2); dup2(fds[1], 2);
t1.tv_sec = 7; t1.ts.tv_sec = 7;
t1.tv_nsec = 1000000001; t1.ts.tv_nsec = 1000000001;
t2 = time_check(t1, NULL); t2 = timeabs_check(t1, NULL);
if (t2.tv_sec != 8 || t2.tv_nsec != 1) if (t2.ts.tv_sec != 8 || t2.ts.tv_nsec != 1)
exit(1); exit(1);
t1.tv_sec = -1; t1.ts.tv_sec = -1;
t1.tv_nsec = 5; t1.ts.tv_nsec = 5;
t2 = time_check(t1, NULL); t2 = timeabs_check(t1, NULL);
if (t2.tv_sec != 0 || t2.tv_nsec != 5) if (t2.ts.tv_sec != 0 || t2.ts.tv_nsec != 5)
exit(1); exit(1);
t1.tv_sec = 8; t1.ts.tv_sec = 8;
t1.tv_nsec = 1000000002; t1.ts.tv_nsec = 1000000002;
/* We expect this to abort! */ /* We expect this to abort! */
t2 = time_check(t1, "abortstr"); t2 = timeabs_check(t1, "abortstr");
exit(1); exit(1);
default: { default: {
......
...@@ -4,145 +4,160 @@ ...@@ -4,145 +4,160 @@
int main(void) int main(void)
{ {
struct timespec t1, t2, t3, zero = { 0, 0 }; struct timeabs t1, t2;
struct timerel t3, t4, zero = { { 0, 0 } };
plan_tests(61); plan_tests(66);
/* Test time_now */ /* Test time_now */
t1 = time_now(); t1 = time_now();
t2 = time_now(); t2 = time_now();
/* Test time_sub. */ /* Test time_between. */
t3 = time_sub(t2, t1); t3 = time_between(t2, t1);
ok1(t3.tv_sec > 0 || t3.tv_nsec >= 0); ok1(t3.ts.tv_sec > 0 || t3.ts.tv_nsec >= 0);
t3 = time_sub(t2, t2); t3 = time_between(t2, t2);
ok1(t3.tv_sec == 0 && t3.tv_nsec == 0); ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
t3 = time_sub(t1, t1); t3 = time_between(t1, t1);
ok1(t3.tv_sec == 0 && t3.tv_nsec == 0); ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
/* Test time_eq */ /* Test timeabs_eq / timerel_eq */
ok1(time_eq(t1, t1)); ok1(timeabs_eq(t1, t1));
ok1(time_eq(t2, t2)); ok1(timeabs_eq(t2, t2));
ok1(!time_eq(t1, t3)); t3.ts.tv_sec = 0;
ok1(!time_eq(t2, t3)); t3.ts.tv_nsec = 1;
ok1(!timerel_eq(t3, zero));
ok1(!timerel_eq(t3, zero));
/* Make sure t2 > t1. */ /* Make sure t2 > t1. */
t3.tv_sec = 0; t2 = timeabs_add(t2, t3);
t3.tv_nsec = 1;
t2 = time_add(t2, t3); /* Test time_after and time_before. */
ok1(!timeabs_eq(t1, t2));
ok1(!time_after(t1, t2));
ok1(time_before(t1, t2));
ok1(time_after(t2, t1));
ok1(!time_before(t2, t1));
t3.ts.tv_sec = 0;
t3.ts.tv_nsec = 999999999;
t2 = timeabs_add(t2, t3);
ok1(!timeabs_eq(t1, t2));
ok1(!time_after(t1, t2));
ok1(time_before(t1, t2));
ok1(time_after(t2, t1));
ok1(!time_before(t2, t1));
/* Test time_less and time_greater. */ /* Test time_less and time_greater. */
ok1(!time_eq(t1, t2)); ok1(time_less(zero, t3));
ok1(!time_greater(t1, t2)); ok1(!time_greater(zero, t3));
ok1(time_less(t1, t2)); ok1(!time_less(t3, zero));
ok1(time_greater(t2, t1)); ok1(time_greater(t3, zero));
ok1(!time_less(t2, t1));
t3.tv_sec = 0; /* Test time_sub */
t3.tv_nsec = 999999999; t3 = time_sub(t3, t3);
t2 = time_add(t2, t3); ok1(timerel_eq(t3, zero));
ok1(!time_eq(t1, t2));
ok1(!time_greater(t1, t2)); /* Test time_between */
ok1(time_less(t1, t2)); t3 = time_between(t2, t2);
ok1(time_greater(t2, t1)); ok1(timerel_eq(t3, zero));
ok1(!time_less(t2, t1)); t3.ts.tv_sec = 0;
t3.ts.tv_nsec = 999999999;
t3 = time_sub(t2, zero); t1 = timeabs_add(t2, t3);
ok1(time_eq(t3, t2)); t3 = time_between(t1, t2);
t3 = time_sub(t2, t2); ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 999999999);
ok1(time_eq(t3, zero));
/* time_from_sec / time_to_sec */ /* time_from_sec / time_to_sec */
t3 = time_from_sec(500); t3 = time_from_sec(500);
ok1(t3.tv_sec == 500); ok1(t3.ts.tv_sec == 500);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_sec(t3) == 500); ok1(time_to_sec(t3) == 500);
/* time_from_msec / time_to_msec */ /* time_from_msec / time_to_msec */
t3 = time_from_msec(500); t3 = time_from_msec(500);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_msec(t3) == 500); ok1(time_to_msec(t3) == 500);
t3 = time_from_msec(1000); t3 = time_from_msec(1000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_msec(t3) == 1000); ok1(time_to_msec(t3) == 1000);
t3 = time_from_msec(1500); t3 = time_from_msec(1500);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_msec(t3) == 1500); ok1(time_to_msec(t3) == 1500);
/* time_from_usec */ /* time_from_usec */
t3 = time_from_usec(500000); t3 = time_from_usec(500000);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_usec(t3) == 500000); ok1(time_to_usec(t3) == 500000);
t3 = time_from_usec(1000000); t3 = time_from_usec(1000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_usec(t3) == 1000000); ok1(time_to_usec(t3) == 1000000);
t3 = time_from_usec(1500000); t3 = time_from_usec(1500000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_usec(t3) == 1500000); ok1(time_to_usec(t3) == 1500000);
/* time_from_nsec */ /* time_from_nsec */
t3 = time_from_nsec(500000000); t3 = time_from_nsec(500000000);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_nsec(t3) == 500000000); ok1(time_to_nsec(t3) == 500000000);
t3 = time_from_nsec(1000000000); t3 = time_from_nsec(1000000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 0); ok1(t3.ts.tv_nsec == 0);
ok1(time_to_nsec(t3) == 1000000000); ok1(time_to_nsec(t3) == 1000000000);
t3 = time_from_nsec(1500000000); t3 = time_from_nsec(1500000000);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
ok1(time_to_nsec(t3) == 1500000000); ok1(time_to_nsec(t3) == 1500000000);
/* Test wrapunder */ /* Test wrapunder */
t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500)); t1 = timeabs_sub(timeabs_sub(t2, time_from_msec(500)), time_from_msec(500));
ok1(t3.tv_sec == t2.tv_sec - 1); ok1(t1.ts.tv_sec == t2.ts.tv_sec - 1);
ok1(t3.tv_nsec == t2.tv_nsec); ok1(t1.ts.tv_nsec == t2.ts.tv_nsec);
/* time_divide and time_multiply */ /* time_divide and time_multiply */
t1.tv_nsec = 100; t4.ts.tv_nsec = 100;
t1.tv_sec = 100; t4.ts.tv_sec = 100;
t3 = time_divide(t1, 2); t3 = time_divide(t4, 2);
ok1(t3.tv_sec == 50); ok1(t3.ts.tv_sec == 50);
ok1(t3.tv_nsec == 50); ok1(t3.ts.tv_nsec == 50);
t3 = time_divide(t1, 100); t3 = time_divide(t4, 100);
ok1(t3.tv_sec == 1); ok1(t3.ts.tv_sec == 1);
ok1(t3.tv_nsec == 1); ok1(t3.ts.tv_nsec == 1);
t3 = time_multiply(t3, 100); t3 = time_multiply(t3, 100);
ok1(time_eq(t3, t1)); ok1(timerel_eq(t3, t4));
t3 = time_divide(t1, 200); t3 = time_divide(t4, 200);
ok1(t3.tv_sec == 0); ok1(t3.ts.tv_sec == 0);
ok1(t3.tv_nsec == 500000000); ok1(t3.ts.tv_nsec == 500000000);
/* Divide by huge number. */ /* Divide by huge number. */
t1.tv_sec = (1U << 31) - 1; t4.ts.tv_sec = (1U << 31) - 1;
t1.tv_nsec = 999999999; t4.ts.tv_nsec = 999999999;
t2 = time_divide(t1, 1 << 30); t3 = time_divide(t4, 1 << 30);
/* Allow us to round either way. */ /* Allow us to round either way. */
ok1((t2.tv_sec == 2 && t2.tv_nsec == 0) ok1((t3.ts.tv_sec == 2 && t3.ts.tv_nsec == 0)
|| (t2.tv_sec == 1 && t2.tv_nsec == 999999999)); || (t3.ts.tv_sec == 1 && t3.ts.tv_nsec == 999999999));
/* Multiply by huge number. */ /* Multiply by huge number. */
t1.tv_sec = 0; t4.ts.tv_sec = 0;
t1.tv_nsec = 1; t4.ts.tv_nsec = 1;
t2 = time_multiply(t1, 1UL << 31); t3 = time_multiply(t4, 1UL << 31);
ok1(t2.tv_sec == 2); ok1(t3.ts.tv_sec == 2);
ok1(t2.tv_nsec == 147483648); ok1(t3.ts.tv_nsec == 147483648);
return exit_status(); return exit_status();
} }
...@@ -6,33 +6,33 @@ ...@@ -6,33 +6,33 @@
#if !HAVE_CLOCK_GETTIME && !HAVE_CLOCK_GETTIME_IN_LIBRT #if !HAVE_CLOCK_GETTIME && !HAVE_CLOCK_GETTIME_IN_LIBRT
#include <sys/time.h> #include <sys/time.h>
struct timespec time_now(void) struct timeabs time_now(void)
{ {
struct timeval now; struct timeval now;
struct timespec ret; struct timeabs ret;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
ret.tv_sec = now.tv_sec; ret.ts.tv_sec = now.tv_sec;
ret.tv_nsec = now.tv_usec * 1000; ret.ts.tv_nsec = now.tv_usec * 1000;
return TIME_CHECK(ret); return TIMEABS_CHECK(ret);
} }
#else #else
#include <time.h> #include <time.h>
struct timespec time_now(void) struct timeabs time_now(void)
{ {
struct timespec ret; struct timeabs ret;
clock_gettime(CLOCK_REALTIME, &ret); clock_gettime(CLOCK_REALTIME, &ret.ts);
return TIME_CHECK(ret); return TIMEABS_CHECK(ret);
} }
#endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT */ #endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT */
struct timespec time_divide(struct timespec t, unsigned long div) struct timerel time_divide(struct timerel t, unsigned long div)
{ {
struct timespec res; struct timerel res;
uint64_t rem, ns; uint64_t rem, ns;
/* Dividing seconds is simple. */ /* Dividing seconds is simple. */
res.tv_sec = TIME_CHECK(t).tv_sec / div; res.ts.tv_sec = TIMEREL_CHECK(t).ts.tv_sec / div;
rem = t.tv_sec % div; rem = t.ts.tv_sec % div;
/* If we can't fit remainder * 1,000,000,000 in 64 bits? */ /* If we can't fit remainder * 1,000,000,000 in 64 bits? */
#if 0 /* ilog is great, but we use fp for multiply anyway. */ #if 0 /* ilog is great, but we use fp for multiply anyway. */
...@@ -45,37 +45,37 @@ struct timespec time_divide(struct timespec t, unsigned long div) ...@@ -45,37 +45,37 @@ struct timespec time_divide(struct timespec t, unsigned long div)
#endif #endif
if (rem & ~(((uint64_t)1 << 30) - 1)) { if (rem & ~(((uint64_t)1 << 30) - 1)) {
/* FIXME: fp is cheating! */ /* FIXME: fp is cheating! */
double nsec = rem * 1000000000.0 + t.tv_nsec; double nsec = rem * 1000000000.0 + t.ts.tv_nsec;
res.tv_nsec = nsec / div; res.ts.tv_nsec = nsec / div;
} else { } else {
ns = rem * 1000000000 + t.tv_nsec; ns = rem * 1000000000 + t.ts.tv_nsec;
res.tv_nsec = ns / div; res.ts.tv_nsec = ns / div;
} }
return TIME_CHECK(res); return TIMEREL_CHECK(res);
} }
struct timespec time_multiply(struct timespec t, unsigned long mult) struct timerel time_multiply(struct timerel t, unsigned long mult)
{ {
struct timespec res; struct timerel res;
/* Are we going to overflow if we multiply nsec? */ /* Are we going to overflow if we multiply nsec? */
if (mult & ~((1UL << 30) - 1)) { if (mult & ~((1UL << 30) - 1)) {
/* FIXME: fp is cheating! */ /* FIXME: fp is cheating! */
double nsec = (double)t.tv_nsec * mult; double nsec = (double)t.ts.tv_nsec * mult;
res.tv_sec = nsec / 1000000000.0; res.ts.tv_sec = nsec / 1000000000.0;
res.tv_nsec = nsec - (res.tv_sec * 1000000000.0); res.ts.tv_nsec = nsec - (res.ts.tv_sec * 1000000000.0);
} else { } else {
uint64_t nsec = t.tv_nsec * mult; uint64_t nsec = t.ts.tv_nsec * mult;
res.tv_nsec = nsec % 1000000000; res.ts.tv_nsec = nsec % 1000000000;
res.tv_sec = nsec / 1000000000; res.ts.tv_sec = nsec / 1000000000;
} }
res.tv_sec += TIME_CHECK(t).tv_sec * mult; res.ts.tv_sec += TIMEREL_CHECK(t).ts.tv_sec * mult;
return TIME_CHECK(res); return TIMEREL_CHECK(res);
} }
struct timespec time_check(struct timespec t, const char *abortstr) struct timespec time_check_(struct timespec t, const char *abortstr)
{ {
if (t.tv_sec < 0 || t.tv_nsec >= 1000000000) { if (t.tv_sec < 0 || t.tv_nsec >= 1000000000) {
if (abortstr) { if (abortstr) {
...@@ -101,3 +101,19 @@ struct timespec time_check(struct timespec t, const char *abortstr) ...@@ -101,3 +101,19 @@ struct timespec time_check(struct timespec t, const char *abortstr)
} }
return t; return t;
} }
struct timerel timerel_check(struct timerel t, const char *abortstr)
{
struct timerel ret;
ret.ts = time_check_(t.ts, abortstr);
return ret;
}
struct timeabs timeabs_check(struct timeabs t, const char *abortstr)
{
struct timeabs ret;
ret.ts = time_check_(t.ts, abortstr);
return ret;
}
...@@ -17,52 +17,89 @@ struct timespec { ...@@ -17,52 +17,89 @@ struct timespec {
#ifdef DEBUG #ifdef DEBUG
#include <ccan/str/str.h> #include <ccan/str/str.h>
#define TIME_CHECK(t) \ #define TIME_CHECK(t) \
time_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ") time_check_((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
#define TIMEREL_CHECK(t) \
timerel_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
#define TIMEABS_CHECK(t) \
timeabs_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
#else #else
#define TIME_CHECK(t) (t) #define TIME_CHECK(t) (t)
#define TIMEREL_CHECK(t) (t)
#define TIMEABS_CHECK(t) (t)
#endif #endif
/** /**
* time_check - check if a time is malformed. * struct timerel - a relative time.
* @in: the time to check (returned) * @ts: the actual timespec value.
*
* For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
*/
struct timerel {
struct timespec ts;
};
/**
* struct timeabs - an absolue time.
* @ts: the actual timespec value.
*
* For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
*/
struct timeabs {
struct timespec ts;
};
struct timespec time_check_(struct timespec in, const char *abortstr);
/**
* timerel_check - check if a relative time is malformed.
* @in: the relative time to check (returned)
* @abortstr: the string to print to stderr before aborting (if set). * @abortstr: the string to print to stderr before aborting (if set).
* *
* This can be used to make sure a time isn't negative and doesn't * This can be used to make sure a time isn't negative and doesn't
* have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL, * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
* that will be printed and abort() is called. Otherwise, if * that will be printed and abort() is called. Otherwise, if
* @abortstr is NULL then the returned timespec will be normalized and * @abortstr is NULL then the returned timerel will be normalized and
* tv_sec set to 0 if it was negative. * tv_sec set to 0 if it was negative.
* *
* Note that if ccan/time is compiled with DEBUG, then it will call this * Note that if ccan/time is compiled with DEBUG, then it will call this
* for all passed and returned times. * for all passed and returned times.
* *
* Example: * Example:
* printf("Now is %lu seconds since epoch\n", * printf("Time to calc this was %lu nanoseconds\n",
* (long)time_check(time_now(), "time_now() failed?").tv_sec); * (long)timerel_check(time_between(time_now(), time_now()),
* "time_now() failed?").ts.tv_nsec);
*/ */
struct timespec time_check(struct timespec in, const char *abortstr); struct timerel timerel_check(struct timerel in, const char *abortstr);
/** /**
* time_now - return the current time * timeabs_check - check if an absolute time is malformed.
* @in: the relative time to check (returned)
* @abortstr: the string to print to stderr before aborting (if set).
*
* This can be used to make sure a time isn't negative and doesn't
* have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
* that will be printed and abort() is called. Otherwise, if
* @abortstr is NULL then the returned timeabs will be normalized and
* tv_sec set to 0 if it was negative.
*
* Note that if ccan/time is compiled with DEBUG, then it will call this
* for all passed and returned times.
* *
* Example: * Example:
* printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec); * printf("Now is %lu seconds since epoch\n",
* (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
*/ */
struct timespec time_now(void); struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
/** /**
* time_greater - is a after b? * time_now - return the current time
* @a: one time.
* @b: another time.
* *
* Example: * Example:
* static bool timed_out(const struct timespec *start) * printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
* {
* #define TIMEOUT time_from_msec(1000)
* return time_greater(time_now(), time_add(*start, TIMEOUT));
* }
*/ */
static inline bool time_greater(struct timespec a, struct timespec b) struct timeabs time_now(void);
static inline bool time_greater_(struct timespec a, struct timespec b)
{ {
if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec) if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
return true; return true;
...@@ -73,18 +110,33 @@ static inline bool time_greater(struct timespec a, struct timespec b) ...@@ -73,18 +110,33 @@ static inline bool time_greater(struct timespec a, struct timespec b)
} }
/** /**
* time_less - is a before b? * time_after - is a after b?
* @a: one time. * @a: one abstime.
* @b: another time. * @b: another abstime.
* *
* Example: * Example:
* static bool still_valid(const struct timespec *start) * static bool timed_out(const struct timeabs *start)
* { * {
* #define TIMEOUT time_from_msec(1000) * #define TIMEOUT time_from_msec(1000)
* return time_less(time_now(), time_add(*start, TIMEOUT)); * return time_after(time_now(), timeabs_add(*start, TIMEOUT));
* } * }
*/ */
static inline bool time_less(struct timespec a, struct timespec b) static inline bool time_after(struct timeabs a, struct timeabs b)
{
return time_greater_(a.ts, b.ts);
}
/**
* time_greater - is a greater than b?
* @a: one reltime.
* @b: another reltime.
*/
static inline bool time_greater(struct timerel a, struct timerel b)
{
return time_greater_(a.ts, b.ts);
}
static inline bool time_less_(struct timespec a, struct timespec b)
{ {
if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec) if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
return true; return true;
...@@ -95,9 +147,36 @@ static inline bool time_less(struct timespec a, struct timespec b) ...@@ -95,9 +147,36 @@ static inline bool time_less(struct timespec a, struct timespec b)
} }
/** /**
* time_eq - is a equal to b? * time_before - is a before b?
* @a: one time. * @a: one absolute time.
* @b: another time. * @b: another absolute time.
*
* Example:
* static bool still_valid(const struct timeabs *start)
* {
* #define TIMEOUT time_from_msec(1000)
* return time_before(time_now(), timeabs_add(*start, TIMEOUT));
* }
*/
static inline bool time_before(struct timeabs a, struct timeabs b)
{
return time_less_(a.ts, b.ts);
}
/**
* time_less - is a before b?
* @a: one relative time.
* @b: another relative time.
*/
static inline bool time_less(struct timerel a, struct timerel b)
{
return time_less_(a.ts, b.ts);
}
/**
* timeabs_eq - is a equal to b?
* @a: one absolute time.
* @b: another absolute time.
* *
* Example: * Example:
* #include <sys/types.h> * #include <sys/types.h>
...@@ -106,34 +185,49 @@ static inline bool time_less(struct timespec a, struct timespec b) ...@@ -106,34 +185,49 @@ static inline bool time_less(struct timespec a, struct timespec b)
* // Can we fork in under a nanosecond? * // Can we fork in under a nanosecond?
* static bool fast_fork(void) * static bool fast_fork(void)
* { * {
* struct timespec start = time_now(); * struct timeabs start = time_now();
* if (fork() != 0) { * if (fork() != 0) {
* exit(0); * exit(0);
* } * }
* wait(NULL); * wait(NULL);
* return time_eq(start, time_now()); * return timeabs_eq(start, time_now());
* } * }
*/ */
static inline bool time_eq(struct timespec a, struct timespec b) static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
{ {
return TIME_CHECK(a).tv_sec == TIME_CHECK(b).tv_sec return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
&& a.tv_nsec == b.tv_nsec; && a.ts.tv_nsec == b.ts.tv_nsec;
} }
/** /**
* time_sub - subtract two times * timerel_eq - is a equal to b?
* @recent: the larger (more recent) time. * @a: one relative time.
* @old: the smaller (less recent) time. * @b: another relative time.
*
* This returns a well formed struct timespec.
* *
* Example: * Example:
* static bool was_recent(const struct timespec *start) * #include <sys/types.h>
* #include <sys/wait.h>
*
* // Can we fork in under a nanosecond?
* static bool fast_fork(void)
* { * {
* return time_sub(time_now(), *start).tv_sec < 1; * struct timeabs start = time_now();
* struct timerel diff, zero = { .ts = { 0, 0 } };
* if (fork() != 0) {
* exit(0);
* }
* wait(NULL);
* diff = time_between(start, time_now());
* return timerel_eq(diff, zero);
* } * }
*/ */
static inline struct timespec time_sub(struct timespec recent, static inline bool timerel_eq(struct timerel a, struct timerel b)
{
return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
&& a.ts.tv_nsec == b.ts.tv_nsec;
}
static inline struct timespec time_sub_(struct timespec recent,
struct timespec old) struct timespec old)
{ {
struct timespec diff; struct timespec diff;
...@@ -149,20 +243,58 @@ static inline struct timespec time_sub(struct timespec recent, ...@@ -149,20 +243,58 @@ static inline struct timespec time_sub(struct timespec recent,
} }
/** /**
* time_add - add two times * time_sub - subtract two relative times
* @a: one time. * @a: the larger time.
* @b: another time. * @b: the smaller time.
* *
* The times must not overflow, or the results are undefined. * This returns a well formed struct timerel of @a - @b.
*/
static inline struct timerel time_sub(struct timerel a, struct timerel b)
{
struct timerel t;
t.ts = time_sub_(a.ts, b.ts);
return t;
}
/**
* time_between - time between two absolute times
* @recent: the larger time.
* @old: the smaller time.
*
* This returns a well formed struct timerel of @a - @b.
*/
static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
{
struct timerel t;
t.ts = time_sub_(recent.ts, old.ts);
return t;
}
/**
* timeabs_sub - subtract a relative time from an absolute time
* @abs: the absolute time.
* @rel: the relative time.
*
* This returns a well formed struct timeabs of @a - @b.
* *
* Example: * Example:
* // We do one every second. * // We do one every second.
* static struct timespec next_time(void) * static struct timeabs previous_time(void)
* { * {
* return time_add(time_now(), time_from_msec(1000)); * return timeabs_sub(time_now(), time_from_msec(1000));
* } * }
*/ */
static inline struct timespec time_add(struct timespec a, struct timespec b) static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
{
struct timeabs t;
t.ts = time_sub_(abs.ts, rel.ts);
return t;
}
static inline struct timespec time_add_(struct timespec a, struct timespec b)
{ {
struct timespec sum; struct timespec sum;
...@@ -175,6 +307,49 @@ static inline struct timespec time_add(struct timespec a, struct timespec b) ...@@ -175,6 +307,49 @@ static inline struct timespec time_add(struct timespec a, struct timespec b)
return TIME_CHECK(sum); return TIME_CHECK(sum);
} }
/**
* timeabs_add - add a relative to an absolute time
* @a: the absolute time.
* @b: a relative time.
*
* The times must not overflow, or the results are undefined.
*
* Example:
* // We do one every second.
* static struct timeabs next_time(void)
* {
* return timeabs_add(time_now(), time_from_msec(1000));
* }
*/
static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
{
struct timeabs t;
t.ts = time_add_(a.ts, b.ts);
return t;
}
/**
* timerel_add - add two relative times
* @a: one relative time.
* @b: another relative time.
*
* The times must not overflow, or the results are undefined.
*
* Example:
* static struct timerel double_time(struct timerel a)
* {
* return timerel_add(a, a);
* }
*/
static inline struct timerel timerel_add(struct timerel a, struct timerel b)
{
struct timerel t;
t.ts = time_add_(a.ts, b.ts);
return t;
}
/** /**
* time_divide - divide a time by a value. * time_divide - divide a time by a value.
* @t: a time. * @t: a time.
...@@ -182,9 +357,9 @@ static inline struct timespec time_add(struct timespec a, struct timespec b) ...@@ -182,9 +357,9 @@ static inline struct timespec time_add(struct timespec a, struct timespec b)
* *
* Example: * Example:
* // How long does it take to do a fork? * // How long does it take to do a fork?
* static struct timespec forking_time(void) * static struct timerel forking_time(void)
* { * {
* struct timespec start = time_now(); * struct timeabs start = time_now();
* unsigned int i; * unsigned int i;
* *
* for (i = 0; i < 1000; i++) { * for (i = 0; i < 1000; i++) {
...@@ -193,22 +368,22 @@ static inline struct timespec time_add(struct timespec a, struct timespec b) ...@@ -193,22 +368,22 @@ static inline struct timespec time_add(struct timespec a, struct timespec b)
* } * }
* wait(NULL); * wait(NULL);
* } * }
* return time_divide(time_sub(time_now(), start), i); * return time_divide(time_between(time_now(), start), i);
* } * }
*/ */
struct timespec time_divide(struct timespec t, unsigned long div); struct timerel time_divide(struct timerel t, unsigned long div);
/** /**
* time_multiply - multiply a time by a value. * time_multiply - multiply a time by a value.
* @t: a time. * @t: a relative time.
* @mult: number to multiply it by. * @mult: number to multiply it by.
* *
* Example: * Example:
* ... * ...
* printf("Time to do 100000 forks would be %u sec\n", * printf("Time to do 100000 forks would be %u sec\n",
* (unsigned)time_multiply(forking_time(), 1000000).tv_sec); * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
*/ */
struct timespec time_multiply(struct timespec t, unsigned long mult); struct timerel time_multiply(struct timerel t, unsigned long mult);
/** /**
* time_to_sec - return number of seconds * time_to_sec - return number of seconds
...@@ -223,14 +398,14 @@ struct timespec time_multiply(struct timespec t, unsigned long mult); ...@@ -223,14 +398,14 @@ struct timespec time_multiply(struct timespec t, unsigned long mult);
* printf("Forking time is %u sec\n", * printf("Forking time is %u sec\n",
* (unsigned)time_to_sec(forking_time())); * (unsigned)time_to_sec(forking_time()));
*/ */
static inline uint64_t time_to_sec(struct timespec t) static inline uint64_t time_to_sec(struct timerel t)
{ {
return t.tv_sec; return t.ts.tv_sec;
} }
/** /**
* time_to_msec - return number of milliseconds * time_to_msec - return number of milliseconds
* @t: a time * @t: a relative time
* *
* It's often more convenient to deal with time values as * It's often more convenient to deal with time values as
* milliseconds. Note that this will fit into a 32-bit variable if * milliseconds. Note that this will fit into a 32-bit variable if
...@@ -241,17 +416,17 @@ static inline uint64_t time_to_sec(struct timespec t) ...@@ -241,17 +416,17 @@ static inline uint64_t time_to_sec(struct timespec t)
* printf("Forking time is %u msec\n", * printf("Forking time is %u msec\n",
* (unsigned)time_to_msec(forking_time())); * (unsigned)time_to_msec(forking_time()));
*/ */
static inline uint64_t time_to_msec(struct timespec t) static inline uint64_t time_to_msec(struct timerel t)
{ {
uint64_t msec; uint64_t msec;
msec = TIME_CHECK(t).tv_nsec / 1000000 + (uint64_t)t.tv_sec * 1000; msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
return msec; return msec;
} }
/** /**
* time_to_usec - return number of microseconds * time_to_usec - return number of microseconds
* @t: a time * @t: a relative time
* *
* It's often more convenient to deal with time values as * It's often more convenient to deal with time values as
* microseconds. Note that this will fit into a 32-bit variable if * microseconds. Note that this will fit into a 32-bit variable if
...@@ -263,17 +438,17 @@ static inline uint64_t time_to_msec(struct timespec t) ...@@ -263,17 +438,17 @@ static inline uint64_t time_to_msec(struct timespec t)
* (unsigned)time_to_usec(forking_time())); * (unsigned)time_to_usec(forking_time()));
* *
*/ */
static inline uint64_t time_to_usec(struct timespec t) static inline uint64_t time_to_usec(struct timerel t)
{ {
uint64_t usec; uint64_t usec;
usec = TIME_CHECK(t).tv_nsec / 1000 + (uint64_t)t.tv_sec * 1000000; usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
return usec; return usec;
} }
/** /**
* time_to_nsec - return number of nanoseconds * time_to_nsec - return number of nanoseconds
* @t: a time * @t: a relative time
* *
* It's sometimes more convenient to deal with time values as * It's sometimes more convenient to deal with time values as
* nanoseconds. Note that this will fit into a 32-bit variable if * nanoseconds. Note that this will fit into a 32-bit variable if
...@@ -285,111 +460,121 @@ static inline uint64_t time_to_usec(struct timespec t) ...@@ -285,111 +460,121 @@ static inline uint64_t time_to_usec(struct timespec t)
* (unsigned)time_to_nsec(forking_time())); * (unsigned)time_to_nsec(forking_time()));
* *
*/ */
static inline uint64_t time_to_nsec(struct timespec t) static inline uint64_t time_to_nsec(struct timerel t)
{ {
uint64_t nsec; uint64_t nsec;
nsec = TIME_CHECK(t).tv_nsec + (uint64_t)t.tv_sec * 1000000000; nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
return nsec; return nsec;
} }
/** /**
* time_from_sec - convert seconds to a timespec * time_from_sec - convert seconds to a relative time
* @msec: time in seconds * @msec: time in seconds
* *
* Example: * Example:
* // 1 minute timeout * // 1 minute timeout
* #define TIMEOUT time_from_sec(60) * #define TIMEOUT time_from_sec(60)
*/ */
static inline struct timespec time_from_sec(uint64_t sec) static inline struct timerel time_from_sec(uint64_t sec)
{ {
struct timespec t; struct timerel t;
t.tv_nsec = 0; t.ts.tv_nsec = 0;
t.tv_sec = sec; t.ts.tv_sec = sec;
return TIME_CHECK(t); return TIMEREL_CHECK(t);
} }
/** /**
* time_from_msec - convert milliseconds to a timespec * time_from_msec - convert milliseconds to a relative time
* @msec: time in milliseconds * @msec: time in milliseconds
* *
* Example: * Example:
* // 1/2 second timeout * // 1/2 second timeout
* #define TIMEOUT time_from_msec(500) * #define TIMEOUT time_from_msec(500)
*/ */
static inline struct timespec time_from_msec(uint64_t msec) static inline struct timerel time_from_msec(uint64_t msec)
{ {
struct timespec t; struct timerel t;
t.tv_nsec = (msec % 1000) * 1000000; t.ts.tv_nsec = (msec % 1000) * 1000000;
t.tv_sec = msec / 1000; t.ts.tv_sec = msec / 1000;
return TIME_CHECK(t); return TIMEREL_CHECK(t);
} }
/** /**
* time_from_usec - convert microseconds to a timespec * time_from_usec - convert microseconds to a relative time
* @usec: time in microseconds * @usec: time in microseconds
* *
* Example: * Example:
* // 1/2 second timeout * // 1/2 second timeout
* #define TIMEOUT time_from_usec(500000) * #define TIMEOUT time_from_usec(500000)
*/ */
static inline struct timespec time_from_usec(uint64_t usec) static inline struct timerel time_from_usec(uint64_t usec)
{ {
struct timespec t; struct timerel t;
t.tv_nsec = (usec % 1000000) * 1000; t.ts.tv_nsec = (usec % 1000000) * 1000;
t.tv_sec = usec / 1000000; t.ts.tv_sec = usec / 1000000;
return TIME_CHECK(t); return TIMEREL_CHECK(t);
} }
/** /**
* time_from_nsec - convert nanoseconds to a timespec * time_from_nsec - convert nanoseconds to a relative time
* @nsec: time in nanoseconds * @nsec: time in nanoseconds
* *
* Example: * Example:
* // 1/2 second timeout * // 1/2 second timeout
* #define TIMEOUT time_from_nsec(500000000) * #define TIMEOUT time_from_nsec(500000000)
*/ */
static inline struct timespec time_from_nsec(uint64_t nsec) static inline struct timerel time_from_nsec(uint64_t nsec)
{ {
struct timespec t; struct timerel t;
t.tv_nsec = nsec % 1000000000; t.ts.tv_nsec = nsec % 1000000000;
t.tv_sec = nsec / 1000000000; t.ts.tv_sec = nsec / 1000000000;
return TIME_CHECK(t); return TIMEREL_CHECK(t);
}
static inline struct timeval timespec_to_timeval(struct timespec ts)
{
struct timeval tv;
tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;
return tv;
} }
/** /**
* timespec_to_timeval - convert a timespec to a timeval. * timerel_to_timeval - convert a relative time to a timeval.
* @ts: a timespec. * @t: a relative time.
* *
* Example: * Example:
* struct timerel t = { { 100, 0 } }; // 100 seconds
* struct timeval tv; * struct timeval tv;
* *
* tv = timespec_to_timeval(time_now()); * tv = timerel_to_timeval(t);
* printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec); * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
*/ */
static inline struct timeval timespec_to_timeval(struct timespec ts) static inline struct timeval timerel_to_timeval(struct timerel t)
{ {
struct timeval tv; return timespec_to_timeval(t.ts);
tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;
return tv;
} }
/** /**
* timeval_to_timespec - convert a timeval to a timespec. * timeabs_to_timeval - convert an absolute time to a timeval.
* @tv: a timeval. * @t: an absolute time.
* *
* Example: * Example:
* struct timeval tv = { 0, 500 }; * struct timeval tv;
* struct timespec ts;
* *
* ts = timeval_to_timespec(tv); * tv = timeabs_to_timeval(time_now());
* printf("timespec = %lu.%09lu\n", (long)ts.tv_sec, (long)ts.tv_nsec); * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
*/ */
static inline struct timeval timeabs_to_timeval(struct timeabs t)
{
return timespec_to_timeval(t.ts);
}
static inline struct timespec timeval_to_timespec(struct timeval tv) static inline struct timespec timeval_to_timespec(struct timeval tv)
{ {
struct timespec ts; struct timespec ts;
...@@ -397,4 +582,40 @@ static inline struct timespec timeval_to_timespec(struct timeval tv) ...@@ -397,4 +582,40 @@ static inline struct timespec timeval_to_timespec(struct timeval tv)
ts.tv_nsec = tv.tv_usec * 1000; ts.tv_nsec = tv.tv_usec * 1000;
return ts; return ts;
} }
/**
* timeval_to_timerel - convert a timeval to a relative time.
* @tv: a timeval.
*
* Example:
* struct timeval tv = { 0, 500 };
* struct timerel t;
*
* t = timeval_to_timerel(tv);
* printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
*/
static inline struct timerel timeval_to_timerel(struct timeval tv)
{
struct timerel t;
t.ts = timeval_to_timespec(tv);
return TIMEREL_CHECK(t);
}
/**
* timeval_to_timeabs - convert a timeval to an absolute time.
* @tv: a timeval.
*
* Example:
* struct timeval tv = { 1401762008, 500 };
* struct timeabs t;
*
* t = timeval_to_timeabs(tv);
* printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
*/
static inline struct timeabs timeval_to_timeabs(struct timeval tv)
{
struct timeabs t;
t.ts = timeval_to_timespec(tv);
return TIMEABS_CHECK(t);
}
#endif /* CCAN_TIME_H */ #endif /* CCAN_TIME_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment