Merge remote-tracking branch 'refs/remotes/dgit/dgit/sid'
[moreutils.git] / ts
blob2a04392c49a4e3f6d1e99721f3634fc9b97f2e2a
1 #!/usr/bin/perl
3 =head1 NAME
5 ts - timestamp input
7 =head1 SYNOPSIS
9 ts [-r] [-i] [format]
11 =head1 DESCRIPTION
13 ts adds a timestamp to the beginning of each line of input.
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" and "%.s"
18 are like "%S" and "%s", but provide subsecond resolution
19 (ie, "30.00001" and "1301682593.00001").
21 If the -r switch is passed, it instead converts existing timestamps in
22 the input to relative times, such as "15m5s ago". Many common timestamp
23 formats are supported. Note that the Time::Duration and Date::Parse perl
24 modules are required for this mode to work. Currently, converting localized
25 dates is not supported.
27 If both -r and a format is passed, the existing timestamps are
28 converted to the specified format.
30 If the -i switch is passed, ts timestamps incrementally instead. Every
31 timestamp will be the time elapsed since the last timestamp.
32 The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used
33 as well.
35 =head1 ENVIRONMENT
37 The standard TZ environment variable controls what time zone dates
38 are assumed to be in, if a timezone is not specified as part of the date.
40 =head1 AUTHOR
42 Copyright 2006 by Joey Hess <joey@kitenet.net>
44 Licensed under the GNU GPL.
46 =cut
48 use warnings;
49 use strict;
50 use POSIX q{strftime};
52 $|=1;
54 my $rel=0;
55 my $inc=0;
56 use Getopt::Long;
57 GetOptions("r" => \$rel, "i" => \$inc) || die "usage: ts [-r] [-i] [format]\n";
59 if ($rel) {
60 eval q{
61 use Date::Parse;
62 use Time::Duration;
64 die $@ if $@;
67 my $use_format=@ARGV;
68 my $format="%b %d %H:%M:%S";
69 if ($inc) {
70 $format="%H:%M:%S";
71 $ENV{TZ}='GMT';
73 $format=shift if @ARGV;
75 # For subsecond resolution, Time::HiRes is needed.
76 my $hires=0;
77 if ($format=~/\%\.[Ss]/) {
78 require Time::HiRes;
79 $hires=1;
82 my $lastseconds = 0;
83 my $lastmicroseconds = 0;
85 if ($hires) {
86 ($lastseconds, $lastmicroseconds) = Time::HiRes::gettimeofday();
87 } else {
88 $lastseconds = time;
92 while (<>) {
93 if (! $rel) {
94 if ($hires) {
95 my $f=$format;
96 my ($seconds, $microseconds) = Time::HiRes::gettimeofday();
97 if ($inc) {
98 my $deltaseconds = $seconds - $lastseconds;
99 my $deltamicroseconds = $microseconds - $lastmicroseconds;
100 if ($deltamicroseconds < 0) {
101 $deltaseconds -= 1;
102 $deltamicroseconds += 1000000;
104 $lastseconds = $seconds;
105 $lastmicroseconds = $microseconds;
106 $seconds = $deltaseconds;
107 $microseconds = $deltamicroseconds;
109 my $s=sprintf("%06i", $microseconds);
110 $f=~s/\%\.([Ss])/%$1.$s/g;
111 print strftime($f, localtime($seconds));
113 else {
114 if ($inc) {
115 my $seconds = time;
116 my $deltaseconds = $seconds - $lastseconds;
117 $lastseconds = $seconds;
118 print strftime($format, localtime($deltaseconds));
119 } else {
120 print strftime($format, localtime);
123 print " ".$_;
125 else {
126 s{\b(
127 \d\d[-\s\/]\w\w\w # 21 dec 17:05
128 (?:\/\d\d+)? # 21 dec/93 17:05
129 [\s:]\d\d:\d\d # (time part of above)
130 (?::\d\d)? # (optional seconds)
131 (?:\s+[+-]\d\d\d\d)? # (optional timezone)
133 \w{3}\s+\d{1,2}\s+\d\d:\d\d:\d\d # syslog form
135 \d\d\d[-:]\d\d[-:]\d\dT\d\d:\d\d:\d\d.\d+ # ISO-8601
137 (?:\w\w\w,?\s+)? # (optional Day)
138 \d+\s+\w\w\w\s+\d\d+\s+\d\d:\d\d:\d\d
139 # 16 Jun 94 07:29:35
140 (?:\s+\w\w\w|\s[+-]\d\d\d\d)?
141 # (optional timezone)
143 \w\w\w\s+\w\w\w\s+\d\d\s+\d\d:\d\d
144 # lastlog format
147 $use_format
148 ? strftime($format, localtime(str2time($1)))
149 : concise(ago(time - str2time($1), 2))
150 }exg;
152 print $_;