9 ts [-r] [-i | -s] [format]
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 or -s switch is passed, ts timestamps incrementally instead. In case
31 of -i, every timestamp will be the time elapsed since the last timestamp. In
32 case of -s, the time elapsed since start of the program is used.
33 The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used
38 The standard TZ environment variable controls what time zone dates
39 are assumed to be in, if a timezone is not specified as part of the date.
43 Copyright 2006 by Joey Hess <joey@kitenet.net>
45 Licensed under the GNU GPL.
51 use POSIX
q{strftime};
59 GetOptions
("r" => \
$rel, "i" => \
$inc, "s" => \
$sincestart) || die "usage: ts [-r] [-i | -s] [format]\n";
70 my $format="%b %d %H:%M:%S";
71 if ($inc || $sincestart) {
75 $format=shift if @ARGV;
77 # For subsecond resolution, Time::HiRes is needed.
79 if ($format=~/\%\.[Ss]/) {
85 my $lastmicroseconds = 0;
88 ($lastseconds, $lastmicroseconds) = Time
::HiRes
::gettimeofday
();
98 my ($seconds, $microseconds) = Time
::HiRes
::gettimeofday
();
99 if ($inc || $sincestart) {
100 my $deltaseconds = $seconds - $lastseconds;
101 my $deltamicroseconds = $microseconds - $lastmicroseconds;
102 if ($deltamicroseconds < 0) {
104 $deltamicroseconds += 1000000;
107 $lastseconds = $seconds;
108 $lastmicroseconds = $microseconds;
110 $seconds = $deltaseconds;
111 $microseconds = $deltamicroseconds;
113 my $s=sprintf("%06i", $microseconds);
114 $f=~s/\%\.([Ss])/%$1.$s/g;
115 print strftime
($f, localtime($seconds));
118 if ($inc || $sincestart) {
120 my $deltaseconds = $seconds - $lastseconds;
122 $lastseconds = $seconds;
124 print strftime
($format, localtime($deltaseconds));
126 print strftime
($format, localtime);
133 \d\d
[-\s\
/]\w\w\w
# 21 dec 17:05
134 (?
:\
/\d\d+)? # 21 dec/93 17:05
135 [\s
:]\d\d
:\d\d
# (time part of above)
136 (?
::\d\d
)?
# (optional seconds)
137 (?
:\s
+[+-]\d\d\d\d
)?
# (optional timezone)
139 \w
{3}\s
+\d
{1,2}\s
+\d\d
:\d\d
:\d\d
# syslog form
141 \d\d\d
[-:]\d\d
[-:]\d\dT\d\d
:\d\d
:\d\d
.\d
+ # ISO-8601
143 (?
:\w\w\w
,?\s
+)?
# (optional Day)
144 \d
+\s
+\w\w\w\s
+\d\d
+\s
+\d\d
:\d\d
:\d\d
146 (?
:\s
+\w\w\w
|\s
[+-]\d\d\d\d
)?
147 # (optional timezone)
149 \w\w\w\s
+\w\w\w\s
+\d\d\s
+\d\d
:\d\d
154 ? strftime
($format, localtime(str2time
($1)))
155 : concise
(ago
(time - str2time
($1), 2))