From 973698d7b119114c4d5621f0f811efc04f4bc6d9 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 26 Mar 2019 11:44:48 +0100 Subject: ts: use gettimeofday - we don't use nanoseconds here function old new delta ts_main 398 376 -22 Signed-off-by: Denys Vlasenko --- miscutils/ts.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'miscutils') diff --git a/miscutils/ts.c b/miscutils/ts.c index a2823721c..4e1c7739f 100644 --- a/miscutils/ts.c +++ b/miscutils/ts.c @@ -17,12 +17,11 @@ #include "libbb.h" #include "common_bufsiz.h" -# include int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ts_main(int argc UNUSED_PARAM, char **argv) { - struct timespec base; + struct timeval base; unsigned opt; char *frac; char *fmt_dt2str; @@ -48,28 +47,25 @@ int ts_main(int argc UNUSED_PARAM, char **argv) #define date_buf bb_common_bufsiz1 setup_common_bufsiz(); - syscall(__NR_clock_gettime, CLOCK_REALTIME, &base); + gettimeofday(&base, NULL); while ((line = xmalloc_fgets(stdin)) != NULL) { - struct timespec ts; + struct timeval ts; struct tm tm_time; - /* libc has incredibly messy way of doing this, - * typically requiring -lrt. We just skip all this mess - */ - syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts); + gettimeofday(&ts, NULL); if (opt) { /* -i and/or -s */ - struct timespec ts1 = ts1; + struct timeval ts1 = ts1; if (opt & 1) /* -i */ ts1 = ts; //printf("%d %d\n", ts.tv_sec, base.tv_sec); ts.tv_sec -= base.tv_sec; //printf("%d %d\n", ts.tv_sec, base.tv_sec); - ts.tv_nsec -= base.tv_nsec; - if ((int32_t)(ts.tv_nsec) < 0) { + ts.tv_usec -= base.tv_usec; + if ((int32_t)(ts.tv_usec) < 0) { ts.tv_sec--; - ts.tv_nsec += 1000*1000*1000; + ts.tv_usec += 1000*1000; } if (opt & 1) /* -i */ base = ts1; @@ -79,7 +75,7 @@ int ts_main(int argc UNUSED_PARAM, char **argv) if (!frac) { printf("%s %s", date_buf, line); } else { - printf("%s.%06u %s", date_buf, (unsigned)ts.tv_nsec / 1000u, line); + printf("%s.%06u %s", date_buf, (unsigned)ts.tv_usec, line); } free(line); } -- cgit v1.2.3