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,
18 "%.S" and "%.s" and "%.T"
19 are like "%S" and "%s" and "%T", but provide subsecond resolution
20 (ie, "30.00001" and "1301682593.00001" and "1:15:30.00001").
22 If the -r switch is passed, it instead converts existing timestamps in
23 the input to relative times, such as "15m5s ago". Many common timestamp
24 formats are supported. Note that the Time::Duration and Date::Parse perl
25 modules are required for this mode to work. Currently, converting localized
26 dates is not supported.
28 If both -r and a format is passed, the existing timestamps are
29 converted to the specified format.
31 If the -i or -s switch is passed, ts timestamps incrementally instead. In case
32 of -i, every timestamp will be the time elapsed since the last timestamp. In
33 case of -s, the time elapsed since start of the program is used.
34 The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used
37 The -m switch makes the system's monotonic clock be used.
41 The standard TZ environment variable controls what time zone dates
42 are assumed to be in, if a timezone is not specified as part of the date.
46 Copyright 2006 by Joey Hess <id@joeyh.name>
48 Licensed under the GNU GPL.
54 use POSIX
q{strftime};
69 ) || die "usage: ts [-r] [-i | -s] [-m] [format]\n";
80 my $format="%b %d %H:%M:%S";
81 if ($inc || $sincestart) {
85 $format=shift if @ARGV;
87 # For subsecond resolution, Time::HiRes is needed.
89 if ($format=~/\%\.[SsT]/ || $mono) {
91 use Time
::HiRes
qw(CLOCK_MONOTONIC);
96 my $lastmicroseconds = 0;
100 my $raw_time = Time
::HiRes
::clock_gettime
(CLOCK_MONOTONIC
);
102 $lastmicroseconds = int(1000000 * ($raw_time - int($raw_time)));
103 $monodelta = time - int($raw_time);
106 ($lastseconds, $lastmicroseconds) = Time
::HiRes
::gettimeofday
();
121 Time
::HiRes
::clock_gettime
(CLOCK_MONOTONIC
);
122 $seconds = $monodelta + int($raw_time);
123 $microseconds = int(1000000 * ($raw_time - $seconds));
126 ($seconds, $microseconds) = Time
::HiRes
::gettimeofday
();
129 if ($inc || $sincestart) {
130 my $deltaseconds = $seconds - $lastseconds;
131 my $deltamicroseconds = $microseconds - $lastmicroseconds;
132 if ($deltamicroseconds < 0) {
134 $deltamicroseconds += 1000000;
137 $lastseconds = $seconds;
138 $lastmicroseconds = $microseconds;
140 $seconds = $deltaseconds;
141 $microseconds = $deltamicroseconds;
143 my $s=sprintf("%06i", $microseconds);
144 $f=~s/\%\.([SsT])/%$1.$s/g;
145 print strftime
($f, localtime($seconds));
148 if ($inc || $sincestart) {
150 my $deltaseconds = $seconds - $lastseconds;
152 $lastseconds = $seconds;
154 print strftime
($format, localtime($deltaseconds));
157 print strftime
($format, localtime);
164 \d\d
[-\s\
/]\w\w\w
# 21 dec 17:05
165 (?
:\
/\d\d+)? # 21 dec/93 17:05
166 [\s
:]\d\d
:\d\d
# (time part of above)
167 (?
::\d\d
)?
# (optional seconds)
168 (?
:\s
+[+-]\d\d\d\d
)?
# (optional timezone)
170 \w
{3}\s
+\d
{1,2}\s
+\d\d
:\d\d
:\d\d
# syslog form
172 \d\d\d
[-:]\d\d
[-:]\d\dT\d\d
:\d\d
:\d\d
.\d
+ # ISO-8601
174 (?
:\w\w\w
,?\s
+)?
# (optional Day)
175 \d
+\s
+\w\w\w\s
+\d\d
+\s
+\d\d
:\d\d
:\d\d
177 (?
:\s
+\w\w\w
|\s
[+-]\d\d\d\d
)?
178 # (optional timezone)
180 \w\w\w\s
+\w\w\w\s
+\d\d\s
+\d\d
:\d\d
185 ? strftime
($format, localtime(str2time
($1)))
186 : concise
(ago
(time - str2time
($1), 2))