sponge, ifne: Ensure that suspending/resuming doesn't result in partial writes of...
[moreutils.git] / ts
blobd1cbb16e1c31b3393901b0633c251a6d37ae782b
1 #!/usr/bin/perl
3 =head1 NAME
5 ts - timestamp input
7 =head1 SYNOPSIS
9 ts [-r] [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".
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 =head1 ENVIRONMENT
25 The standard TZ environment variable controls what time zone dates
26 are assumed to be in, if a timezone is not specified as part of the date.
28 =head1 AUTHOR
30 Copyright 2006 by Joey Hess <joey@kitenet.net>
32 Licensed under the GNU GPL.
34 =cut
36 use warnings;
37 use strict;
38 use POSIX q{strftime};
40 $|=1;
42 my $rel=0;
43 use Getopt::Long;
44 GetOptions("r" => \$rel) || die "usage: ts [-r] [format]\n";
46 if ($rel) {
47 eval q{
48 use Date::Parse;
49 use Time::Duration;
51 die $@ if $@;
54 my $format="%b %d %H:%M:%S";
55 $format=shift if @ARGV;
57 while (<>) {
58 if (! $rel) {
59 print strftime($format, localtime)." ".$_;
61 else {
62 s{\b(
63 \d\d[-\s\/]\w\w\w # 21 dec 17:05
64 (?:\/\d\d+)? # 21 dec/93 17:05
65 [\s:]\d\d:\d\d # (time part of above)
66 (?::\d\d)? # (optional seconds)
67 (?:\s+[+-]\d\d\d\d)? # (optional timezone)
69 \w{3}\s+\d\d\s+\d\d:\d\d:\d\d # syslog form
71 \d\d\d[-:]\d\d[-:]\d\dT\d\d:\d\d:\d\d.\d+ # ISO-8601
73 (?:\w\w\w,?\s+)? # (optional Day)
74 \d+\s+\w\w\w\s+\d\d+\s+\d\d:\d\d:\d\d
75 # 16 Jun 94 07:29:35
76 (?:\s+\w\w\w|\s+-\d\d\d\d)?
77 # (optional timezone)
79 \w\w\w\s+\w\w\w\s+\d\d\s+\d\d:\d\d
80 # lastlog format
81 )\b
83 concise(ago(time - str2time($1), 2))
84 }exg;
86 print $_;