Added microsecond resolution to perl version.
Added C version.
This commit is contained in:
parent
51c015f368
commit
8327e6a6f4
|
@ -6,12 +6,15 @@ CONFDIR_exists=$(shell [ -d $(CONFDIR) ] && echo ok)
|
|||
all: configure ts
|
||||
|
||||
clean:
|
||||
rm ts
|
||||
rm -f ts ts.o
|
||||
|
||||
install: \
|
||||
$(BINDIR)/ts \
|
||||
|
||||
ts: ts.pl
|
||||
ts: %: %.o
|
||||
|
||||
|
||||
# $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
|
||||
|
||||
%: %.pl customize
|
||||
sh ./customize < $< > $@
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static char *cmnd;
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr, "Usage: %s [-u]\n", cmnd);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int c;
|
||||
int use_microseconds = 0;
|
||||
int print_ts = 1;
|
||||
|
||||
cmnd = argv[0];
|
||||
|
||||
while ((c = getopt(argc, argv, "u")) != EOF) {
|
||||
switch (c) {
|
||||
case 'u':
|
||||
use_microseconds = 1;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
}
|
||||
while ((c = getchar()) != EOF) {
|
||||
if (print_ts) {
|
||||
struct timeval tv;
|
||||
char s[sizeof("yyyy-mm-ddThh:mm:ss")];
|
||||
gettimeofday(&tv, NULL);
|
||||
strftime(s, sizeof(s), "%Y-%m-%dT%H:%M:%S",
|
||||
localtime(&tv.tv_sec));
|
||||
fputs(s, stdout);
|
||||
if (use_microseconds) {
|
||||
printf(".%06ld ", (long)tv.tv_usec);
|
||||
}
|
||||
putchar(' ');
|
||||
print_ts = 0;
|
||||
}
|
||||
putchar(c);
|
||||
if (c == '\n') print_ts = 1;
|
||||
}
|
||||
if (!print_ts) putchar('\n'); // force newline at end of output
|
||||
return 0;
|
||||
}
|
13
ts/ts.pl
13
ts/ts.pl
|
@ -1,8 +1,19 @@
|
|||
#!@@@perl@@@ -wT
|
||||
use strict;
|
||||
use POSIX;
|
||||
use Time::HiRes qw(gettimeofday);
|
||||
my $use_microseconds;
|
||||
if ($ARGV[0] eq '-u') {
|
||||
$use_microseconds = 1;
|
||||
shift;
|
||||
}
|
||||
while (<>) {
|
||||
chomp;
|
||||
my $now = strftime('%Y-%m-%dT%H:%M:%S', localtime);
|
||||
my ($seconds, $microseconds) = gettimeofday;
|
||||
my $now = strftime('%Y-%m-%dT%H:%M:%S', localtime($seconds));
|
||||
if ($use_microseconds) {
|
||||
printf "%s.%06d %s\n", $now, $microseconds, $_;
|
||||
} else {
|
||||
print "$now $_\n";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue