ts: use gettimeofday - we don't use nanoseconds here
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 26 Mar 2019 10:44:48 +0000 (11:44 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 26 Mar 2019 10:44:48 +0000 (11:44 +0100)
function                                             old     new   delta
ts_main                                              398     376     -22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/ts.c

index a2823721caab1d5452f0e0d520671fb65645338e..4e1c7739f9320af254e77a4efcd3baee7f0648cd 100644 (file)
 
 #include "libbb.h"
 #include "common_bufsiz.h"
-# include <sys/syscall.h>
 
 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);
        }