home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / ts < prev    next >
Encoding:
Text File  |  2010-09-02  |  2.5 KB  |  115 lines

  1. #!/usr/bin/perl
  2.  
  3. =head1 NAME
  4.  
  5. ts - timestamp input
  6.  
  7. =head1 SYNOPSIS
  8.  
  9. ts [-r] [format]
  10.  
  11. =head1 DESCRIPTION
  12.  
  13. ts adds a timestamp to the beginning of each line of input.
  14.  
  15. The optional format parameter controls how the timestamp is formatted,
  16. as used by L<strftime(3)>. The default format is "%b %d %H:%M:%S". In
  17. addition to the regular strftime conversion specifications, "%.S" is
  18. expanded to fractional seconds (ie, "30.00001").
  19.  
  20. If the -r switch is passed, it instead converts existing timestamps in
  21. the input to relative times, such as "15m5s ago". Many common timestamp
  22. formats are supported. Note that the Time::Duration and Date::Parse perl
  23. modules are required for this mode to work. Currently, converting localized
  24. dates is not supported.
  25.  
  26. If both -r and a format is passed, the existing timestamps are
  27. converted to the specified format.
  28.  
  29. =head1 ENVIRONMENT
  30.  
  31. The standard TZ environment variable controls what time zone dates
  32. are assumed to be in, if a timezone is not specified as part of the date.
  33.  
  34. =head1 AUTHOR
  35.  
  36. Copyright 2006 by Joey Hess <joey@kitenet.net>
  37.  
  38. Licensed under the GNU GPL.
  39.  
  40. =cut
  41.  
  42. use warnings;
  43. use strict;
  44. use POSIX q{strftime};
  45.  
  46. $|=1;
  47.  
  48. my $rel=0;
  49. use Getopt::Long;
  50. GetOptions("r" => \$rel) || die "usage: ts [-r] [format]\n";
  51.  
  52. if ($rel) {
  53.     eval q{
  54.         use Date::Parse;
  55.         use Time::Duration;
  56.     };
  57.     die $@ if $@;
  58. }
  59.  
  60. my $use_format=@ARGV;
  61. my $format="%b %d %H:%M:%S";
  62. $format=shift if @ARGV;
  63.  
  64. # For fractional seconds, Time::HiRes is needed.
  65. my $hires=0;
  66. if ($format=~/\%\.S/) {
  67.     require Time::HiRes;
  68.     $hires=1;
  69. }
  70.  
  71. while (<>) {
  72.     if (! $rel) {
  73.         if ($hires) {
  74.             my $f=$format;
  75.             my ($seconds, $microseconds) = Time::HiRes::gettimeofday();
  76.             my $s=sprintf("%06i", $microseconds);
  77.             $f=~s/\%\.S/%S.$s/g;
  78.             print strftime($f, localtime($seconds));
  79.         }
  80.         else {
  81.             print strftime($format, localtime);
  82.         }
  83.         print " ".$_;
  84.     }
  85.     else {
  86.         s{\b(
  87.             \d\d[-\s\/]\w\w\w    # 21 dec 17:05
  88.                 (?:\/\d\d+)?    # 21 dec/93 17:05
  89.                 [\s:]\d\d:\d\d    #       (time part of above)
  90.                 (?::\d\d)?    #       (optional seconds)
  91.                 (?:\s+[+-]\d\d\d\d)? #  (optional timezone)
  92.             |
  93.             \w{3}\s+\d\d\s+\d\d:\d\d:\d\d # syslog form
  94.             |
  95.             \d\d\d[-:]\d\d[-:]\d\dT\d\d:\d\d:\d\d.\d+ # ISO-8601
  96.             |
  97.             (?:\w\w\w,?\s+)?    #       (optional Day)
  98.             \d+\s+\w\w\w\s+\d\d+\s+\d\d:\d\d:\d\d
  99.                         # 16 Jun 94 07:29:35
  100.                 (?:\s+\w\w\w|\s[+-]\d\d\d\d)?
  101.                         #    (optional timezone)
  102.             |
  103.             \w\w\w\s+\w\w\w\s+\d\d\s+\d\d:\d\d
  104.                         # lastlog format
  105.           )\b
  106.         }{
  107.             $use_format
  108.                 ? strftime($format, localtime(str2time($1)))
  109.                 : concise(ago(time - str2time($1), 2))
  110.         }exg;
  111.  
  112.         print $_;
  113.     }
  114. }
  115.