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".
18 If the -r switch is passed, it instead converts existing timestamps in
19 the input to relative times, such as "15m5s ago". Many common timestamp
20 formats are supported. Note that the Time::Duration and Date::Parse perl
21 modules are required for this mode to work.
23 If both -r and a format is passed, the format existing timestamps are
24 converted to the specified format.
28 The standard TZ environment variable controls what time zone dates
29 are assumed to be in, if a timezone is not specified as part of the date.
33 Copyright 2006 by Joey Hess <joey@kitenet.net>
35 Licensed under the GNU GPL.
41 use POSIX
q{strftime};
47 GetOptions
("r" => \
$rel) || die "usage: ts [-r] [format]\n";
58 my $format="%b %d %H:%M:%S";
59 $format=shift if @ARGV;
63 print strftime
($format, localtime)." ".$_;
67 \d\d
[-\s\
/]\w\w\w
# 21 dec 17:05
68 (?
:\
/\d\d+)? # 21 dec/93 17:05
69 [\s
:]\d\d
:\d\d
# (time part of above)
70 (?
::\d\d
)?
# (optional seconds)
71 (?
:\s
+[+-]\d\d\d\d
)?
# (optional timezone)
73 \w
{3}\s
+\d\d\s
+\d\d
:\d\d
:\d\d
# syslog form
75 \d\d\d
[-:]\d\d
[-:]\d\dT\d\d
:\d\d
:\d\d
.\d
+ # ISO-8601
77 (?
:\w\w\w
,?\s
+)?
# (optional Day)
78 \d
+\s
+\w\w\w\s
+\d\d
+\s
+\d\d
:\d\d
:\d\d
80 (?
:\s
+\w\w\w
|\s
[+-]\d\d\d\d
)?
83 \w\w\w\s
+\w\w\w\s
+\d\d\s
+\d\d
:\d\d
88 ? strftime
($format, localtime(str2time
($1)))
89 : concise
(ago
(time - str2time
($1), 2))