1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2019 Denys Vlasenko <vda.linux@googlemail.com>
4 * Licensed under GPLv2, see file LICENSE in this source tree.
7 //config: bool "ts (450 bytes)"
10 //applet:IF_TS(APPLET(ts, BB_DIR_USR_BIN, BB_SUID_DROP))
12 //kbuild:lib-$(CONFIG_TS) += ts.o
14 //usage:#define ts_trivial_usage
15 //usage: "[-is] [STRFTIME]"
16 //usage:#define ts_full_usage ""
19 #include "common_bufsiz.h"
21 int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22 int ts_main(int argc UNUSED_PARAM, char **argv)
30 opt = getopt32(argv, "^" "is" "\0" "?1" /*max one arg*/);
32 putenv((char*)"TZ=UTC0");
37 fmt_dt2str = argv[0] ? argv[0]
38 : (char*)(opt ? "%b %d %H:%M:%S"+6 : "%b %d %H:%M:%S");
39 frac = is_suffixed_with(fmt_dt2str, "%.S");
41 frac = is_suffixed_with(fmt_dt2str, "%.s");
48 #define date_buf bb_common_bufsiz1
49 setup_common_bufsiz();
50 gettimeofday(&base, NULL);
52 while ((line = xmalloc_fgets(stdin)) != NULL) {
56 gettimeofday(&ts, NULL);
59 struct timeval ts1 = ts1;
62 //printf("%d %d\n", ts.tv_sec, base.tv_sec);
63 ts.tv_sec -= base.tv_sec;
64 //printf("%d %d\n", ts.tv_sec, base.tv_sec);
65 ts.tv_usec -= base.tv_usec;
66 if ((int32_t)(ts.tv_usec) < 0) {
68 ts.tv_usec += 1000*1000;
73 localtime_r(&ts.tv_sec, &tm_time);
74 strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
76 printf("%s %s", date_buf, line);
78 printf("%s.%06u %s", date_buf, (unsigned)ts.tv_usec, line);