]> err.no Git - moreutils/commitdiff
* Change the default ts format to include the month and day, for consistency
authorjoeyh <joeyh>
Fri, 18 Aug 2006 05:46:19 +0000 (05:46 +0000)
committerjoeyh <joeyh>
Fri, 18 Aug 2006 05:46:19 +0000 (05:46 +0000)
  with syslog format.
* Add -r switch to ts, which makes it convert existing timestamps in
  the input into relative times, such as "15m2s ago".

debian/changelog
debian/control
ts

index 966b116057ac84428296df0063c7f94ccebfbf48..1040cdf85fcf5d91605d111612782636ec3ebb39 100644 (file)
@@ -1,3 +1,12 @@
+moreutils (0.16) UNRELEASED; urgency=low
+
+  * Change the default ts format to include the month and day, for consistency
+    with syslog format.
+  * Add -r switch to ts, which makes it convert existing timestamps in
+    the input into relative times, such as "15m2s ago".
+
+ -- Joey Hess <joeyh@debian.org>  Fri, 18 Aug 2006 01:36:37 -0400
+
 moreutils (0.15) unstable; urgency=low
 
   * Remove notes about potential tools from README, moved to wiki.
index 8c6fc6fbb58d6e0b1907d21ce834cc378d3ba929..ced07eb9a88674af72c8ed8c34de8adadb24d1ce 100644 (file)
@@ -8,6 +8,7 @@ Standards-Version: 3.7.2
 Package: moreutils
 Architecture: any
 Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
+Suggests: libtime-duration-perl, libtimedate-perl
 Description: additional unix utilities
  This is a growing collection of the unix tools that nobody thought
  to write thirty years ago.
diff --git a/ts b/ts
index 42a1ce4e37dcfb80212882990dea8e87be85f48b..de620c87f59fcc28d85764384b4c4601fc619638 100755 (executable)
--- a/ts
+++ b/ts
@@ -6,14 +6,24 @@ ts - timestamp input
 
 =head1 SYNOPSIS
 
-ts [format]
+ts [-r] [format]
 
 =head1 DESCRIPTION
 
-ts adds a timestamp to the beginning of each line of input
+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<strftime(3)>. The default format is "%H:%M:%S".
+as used by L<strftime(3)>. The default format is "%b %d %H:%M:%S".
+
+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.
+
+=head1 ENVIRONMENT
+
+The standard TZ environment variable controls what time zone dates
+are assumed to be in, if a timezone is not specified as part of the date.
 
 =head1 AUTHOR
 
@@ -27,11 +37,49 @@ use warnings;
 use strict;
 use POSIX q{strftime};
 
-my $format="%H:%M:%S";
-$format=shift if @ARGV;
-
 $|=1;
 
+my $rel=0;
+use Getopt::Long;
+GetOptions("r" => \$rel) || die "usage: ts [-r] [format]\n";
+
+if ($rel) {
+       eval q{
+               use Date::Parse;
+               use Time::Duration;
+       };
+       die $@ if $@;
+}
+
+my $format="%b %d %H:%M:%S";
+$format=shift if @ARGV;
+
 while (<>) {
-       print strftime($format, localtime)." ".$_;
+       if (! $rel) {
+               print strftime($format, localtime)." ".$_;
+       }
+       else {
+               s{\b(
+                       \d\d[-\s\/]\w\w\w       # 21 dec 17:05
+                               (?:\/\d\d+)?    # 21 dec/93 17:05
+                               [\s:]\d\d:\d\d  #       (time part of above)
+                               (?::\d\d)?      #       (optional seconds)
+                               (?:\s+[+-]\d\d\d\d)? #  (optional timezone)
+                       |
+                       \w{3}\s+\d\d\s+\d\d:\d\d:\d\d # syslog form
+                       |
+                       \d\d\d[-:]\d\d[-:]\d\dT\d\d:\d\d:\d\d.\d+ # ISO-8601
+                       |
+                       (?:\w\w\w,?\s+)?        #       (optional Day)
+                       \d+\s+\w\w\w\s+\d\d+\s+\d\d:\d\d:\d\d
+                                               # 16 Jun 94 07:29:35
+                               (?:\s+\w\w\w|\s+-\d\d\d\d)?
+                                               #       (optional timezone)
+                 )\b
+               }{
+                       concise(ago(time - str2time($1), 2))
+               }exg;
+
+               print $_;
+       }
 }