ts: use gettimeofday - we don't use nanoseconds here

function                                             old     new   delta
ts_main                                              398     376     -22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2019-03-26 11:44:48 +01:00
parent 3395e2a8ef
commit 973698d7b1

View File

@ -17,12 +17,11 @@
#include "libbb.h" #include "libbb.h"
#include "common_bufsiz.h" #include "common_bufsiz.h"
# include <sys/syscall.h>
int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ts_main(int argc UNUSED_PARAM, char **argv) int ts_main(int argc UNUSED_PARAM, char **argv)
{ {
struct timespec base; struct timeval base;
unsigned opt; unsigned opt;
char *frac; char *frac;
char *fmt_dt2str; char *fmt_dt2str;
@ -48,28 +47,25 @@ int ts_main(int argc UNUSED_PARAM, char **argv)
#define date_buf bb_common_bufsiz1 #define date_buf bb_common_bufsiz1
setup_common_bufsiz(); setup_common_bufsiz();
syscall(__NR_clock_gettime, CLOCK_REALTIME, &base); gettimeofday(&base, NULL);
while ((line = xmalloc_fgets(stdin)) != NULL) { while ((line = xmalloc_fgets(stdin)) != NULL) {
struct timespec ts; struct timeval ts;
struct tm tm_time; struct tm tm_time;
/* libc has incredibly messy way of doing this, gettimeofday(&ts, NULL);
* typically requiring -lrt. We just skip all this mess
*/
syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
if (opt) { if (opt) {
/* -i and/or -s */ /* -i and/or -s */
struct timespec ts1 = ts1; struct timeval ts1 = ts1;
if (opt & 1) /* -i */ if (opt & 1) /* -i */
ts1 = ts; ts1 = ts;
//printf("%d %d\n", ts.tv_sec, base.tv_sec); //printf("%d %d\n", ts.tv_sec, base.tv_sec);
ts.tv_sec -= base.tv_sec; ts.tv_sec -= base.tv_sec;
//printf("%d %d\n", ts.tv_sec, base.tv_sec); //printf("%d %d\n", ts.tv_sec, base.tv_sec);
ts.tv_nsec -= base.tv_nsec; ts.tv_usec -= base.tv_usec;
if ((int32_t)(ts.tv_nsec) < 0) { if ((int32_t)(ts.tv_usec) < 0) {
ts.tv_sec--; ts.tv_sec--;
ts.tv_nsec += 1000*1000*1000; ts.tv_usec += 1000*1000;
} }
if (opt & 1) /* -i */ if (opt & 1) /* -i */
base = ts1; base = ts1;
@ -79,7 +75,7 @@ int ts_main(int argc UNUSED_PARAM, char **argv)
if (!frac) { if (!frac) {
printf("%s %s", date_buf, line); printf("%s %s", date_buf, line);
} else { } 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); free(line);
} }