Added microsecond resolution to perl version.

Added C version.
This commit is contained in:
hjp 2003-08-23 15:44:14 +00:00
parent 51c015f368
commit 8327e6a6f4
3 changed files with 68 additions and 4 deletions

View File

@ -6,12 +6,15 @@ CONFDIR_exists=$(shell [ -d $(CONFDIR) ] && echo ok)
all: configure ts all: configure ts
clean: clean:
rm ts rm -f ts ts.o
install: \ install: \
$(BINDIR)/ts \ $(BINDIR)/ts \
ts: ts.pl ts: %: %.o
# $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
%: %.pl customize %: %.pl customize
sh ./customize < $< > $@ sh ./customize < $< > $@

50
ts/ts.c Normal file
View File

@ -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;
}

View File

@ -1,8 +1,19 @@
#!@@@perl@@@ -wT #!@@@perl@@@ -wT
use strict; use strict;
use POSIX; use POSIX;
use Time::HiRes qw(gettimeofday);
my $use_microseconds;
if ($ARGV[0] eq '-u') {
$use_microseconds = 1;
shift;
}
while (<>) { while (<>) {
chomp; chomp;
my $now = strftime('%Y-%m-%dT%H:%M:%S', localtime); my ($seconds, $microseconds) = gettimeofday;
print "$now $_\n"; 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";
}
} }