From: Joey Hess Date: Wed, 28 May 2008 18:32:48 +0000 (-0400) Subject: ts: Support displaying fractional seconds via a "%.S" conversion specification. Close... X-Git-Tag: 0.31~1 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=729a9ec3a9be3b611a180c08daf9191c20506a09;p=moreutils ts: Support displaying fractional seconds via a "%.S" conversion specification. Closes: #482789 --- diff --git a/debian/changelog b/debian/changelog index b102cc5..7cb9bd0 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,8 @@ moreutils (0.31) UNRELEASED; urgency=low * pee.1: Document difference with tee in stdout. + * ts: Support displaying fractional seconds via a "%.S" conversion + specification. Closes: #482789 -- Joey Hess Wed, 28 May 2008 13:42:01 -0400 diff --git a/ts b/ts index d8d8719..ca150b4 100755 --- a/ts +++ b/ts @@ -13,14 +13,16 @@ ts [-r] [format] ts adds a timestamp to the beginning of each line of input. The optional format parameter controls how the timestamp is formatted, -as used by L. The default format is "%b %d %H:%M:%S". +as used by L. The default format is "%b %d %H:%M:%S". In +addition to the regular strftime conversion specifications, "%.S" is +expanded to fractional seconds (ie, "30.00001"). If the -r switch is passed, it instead converts existing timestamps in the input to relative times, such as "15m5s ago". Many common timestamp formats are supported. Note that the Time::Duration and Date::Parse perl modules are required for this mode to work. -If both -r and a format is passed, the format existing timestamps are +If both -r and a format is passed, the existing timestamps are converted to the specified format. =head1 ENVIRONMENT @@ -58,9 +60,26 @@ my $use_format=@ARGV; my $format="%b %d %H:%M:%S"; $format=shift if @ARGV; +# For fractional seconds, Time::HiRes is needed. +my $hires=0; +if ($format=~/\%\.S/) { + require Time::HiRes; + $hires=1; +} + while (<>) { if (! $rel) { - print strftime($format, localtime)." ".$_; + if ($hires) { + my $f=$format; + my ($seconds, $microseconds) = Time::HiRes::gettimeofday(); + my $s=sprintf("%06i", $microseconds); + $f=~s/\%\.S/%S.$s/g; + print strftime($f, localtime($seconds)); + } + else { + print strftime($format, localtime); + } + print " ".$_; } else { s{\b(