From d4e60b0737b1bc28705561321d1b851fa2f46e2a Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 4 Oct 2013 19:31:48 +0200 Subject: timeadd: support time difference --- timeadd.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/timeadd.c b/timeadd.c index 061a0ae..d2f0159 100644 --- a/timeadd.c +++ b/timeadd.c @@ -8,22 +8,77 @@ #include #include #include +#include +#include -int main() { +static void print_usage(const char *program_name) { + fprintf(stderr, "Usage: %s [-d]\n", program_name); + fputs("Options:\n" + " -d Display time difference instead of a monotonic clock\n", + stderr); +} + +struct timespec timediff(struct timespec begin, struct timespec end) { + struct timespec diff; + + diff.tv_sec = end.tv_sec - begin.tv_sec; + diff.tv_nsec = end.tv_nsec - begin.tv_nsec; + + if (end.tv_nsec < begin.tv_nsec) { + /* alternative case: 3.2 -> 4.1, diff is 0.9 */ + diff.tv_sec--; + diff.tv_nsec += 1000000000; + } + + return diff; +} + +int main(int argc, char **argv) { + const char *out_format; char buf[4096]; + struct timespec tp[2] = { { 0 } }; + int tp_i = 0; + int opt; + bool diff = false; + + while ((opt = getopt(argc, argv, "d")) != -1) { + switch (opt) { + case 'd': + diff = true; + break; + default: + print_usage(argv[0]); + return 1; + } + } + + if (diff) { + out_format = "[%+6lld.%06lu] %s\n"; + } else { + out_format = "[%6llu.%06lu] %s\n"; + } while (fgets(buf, sizeof(buf), stdin) != NULL) { char *newline = strrchr(buf, '\n'); - struct timespec tp; - clock_gettime(CLOCK_MONOTONIC, &tp); + clock_gettime(CLOCK_MONOTONIC, &tp[tp_i]); if (newline) *newline = '\0'; - printf("[%5llu.%06lu] %s\n", - (long long) tp.tv_sec, - tp.tv_nsec / 1000, + if (diff) { + /* new time is available in "tp_i". Old time (and print + * target) is the inverse of tp_i (modulo 2). */ + tp[tp_i ^ 1] = timediff(tp[tp_i ^ 1], tp[tp_i]); + /* flip such that "tp_i" refers to difference time and + * inverse "tp_i" points to the new time (in the next + * iteration it becomes the old time) */ + tp_i ^= 1; + } + + printf(out_format, + (long long) tp[tp_i].tv_sec, + tp[tp_i].tv_nsec / 1000, buf); } -- cgit v1.2.1