diff options
author | Thomas Vander Stichele <thomas (at) apestaart (dot) org> | 2013-01-24 17:11:52 +0100 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-01-25 08:11:42 +1100 |
commit | ac2b4e3fbf63a6fcd3be8b01e431eaa05295d1ec (patch) | |
tree | e09ac69a8d417a5e8a9fa0fa1a9a0d065f69ee60 /ts | |
parent | 68d871805878cd2452778d7ddd92e6791a9bcf86 (diff) | |
download | moreutils-ac2b4e3fbf63a6fcd3be8b01e431eaa05295d1ec.tar.gz |
add support for incremental timestamping
Diffstat (limited to 'ts')
-rwxr-xr-x | ts | 50 |
1 files changed, 46 insertions, 4 deletions
@@ -6,7 +6,7 @@ ts - timestamp input =head1 SYNOPSIS -ts [-r] [format] +ts [-r] [-i] [format] =head1 DESCRIPTION @@ -27,6 +27,11 @@ dates is not supported. If both -r and a format is passed, the existing timestamps are converted to the specified format. +If the -i switch is passed, ts timestamps incrementally instead. Every +timestamp will be the time elapsed since the last timestamp. +The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used +as well. + =head1 ENVIRONMENT The standard TZ environment variable controls what time zone dates @@ -47,8 +52,9 @@ use POSIX q{strftime}; $|=1; my $rel=0; +my $inc=0; use Getopt::Long; -GetOptions("r" => \$rel) || die "usage: ts [-r] [format]\n"; +GetOptions("r" => \$rel, "i" => \$inc) || die "usage: ts [-r] [-i] [format]\n"; if ($rel) { eval q{ @@ -60,6 +66,9 @@ if ($rel) { my $use_format=@ARGV; my $format="%b %d %H:%M:%S"; +if ($inc) { + $format="%H:%M:%S"; +} $format=shift if @ARGV; # For subsecond resolution, Time::HiRes is needed. @@ -69,17 +78,50 @@ if ($format=~/\%\.[Ss]/) { $hires=1; } +my $lastseconds = 0; +my $lastmicroseconds = 0; + +if ($hires) { + ($lastseconds, $lastmicroseconds) = Time::HiRes::gettimeofday(); +} else { + $lastseconds = time; +} + + while (<>) { if (! $rel) { if ($hires) { my $f=$format; my ($seconds, $microseconds) = Time::HiRes::gettimeofday(); + if ($inc) { + my $deltaseconds = $seconds - $lastseconds; + my $deltamicroseconds = $microseconds - $lastmicroseconds; + if ($deltamicroseconds < 0) { + $deltaseconds -= 1; + $deltamicroseconds += 1000000; + } + $lastseconds = $seconds; + $lastmicroseconds = $microseconds; + $seconds = $deltaseconds; + $microseconds = $deltamicroseconds; + } my $s=sprintf("%06i", $microseconds); $f=~s/\%\.([Ss])/%$1.$s/g; - print strftime($f, localtime($seconds)); + if (!$inc) { + print strftime($f, localtime($seconds)); + } else { + print strftime($f, gmtime($seconds)); + } } else { - print strftime($format, localtime); + if ($inc) { + my $seconds = time; + my $deltaseconds = $seconds - $lastseconds; + $lastseconds = $seconds; + print strftime($format, gmtime($deltaseconds)); + } else { + print strftime($format, localtime); + } } print " ".$_; } |