ts: Added %.T format like %T but with hi-res.
[moreutils.git] / ts
blob4dad3d4c7b46699d100f4ea8e343be7e65f31bd4
1 #!/usr/bin/perl
3 =head1 NAME
5 ts - timestamp input
7 =head1 SYNOPSIS
9 ts [-r] [-i | -s] [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,
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
35 as well.
37 The -m switch makes the system's monotonic clock be used.
39 =head1 ENVIRONMENT
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.
44 =head1 AUTHOR
46 Copyright 2006 by Joey Hess <id@joeyh.name>
48 Licensed under the GNU GPL.
50 =cut
52 use warnings;
53 use strict;
54 use POSIX q{strftime};
55 no warnings 'utf8';
57 $|=1;
59 my $rel=0;
60 my $inc=0;
61 my $sincestart=0;
62 my $mono=0;
63 use Getopt::Long;
64 GetOptions(
65 "r" => \$rel,
66 "i" => \$inc,
67 "s" => \$sincestart,
68 "m" => \$mono
69 ) || die "usage: ts [-r] [-i | -s] [-m] [format]\n";
71 if ($rel) {
72 eval q{
73 use Date::Parse;
74 use Time::Duration;
76 die $@ if $@;
79 my $use_format=@ARGV;
80 my $format="%b %d %H:%M:%S";
81 if ($inc || $sincestart) {
82 $format="%H:%M:%S";
83 $ENV{TZ}='GMT';
85 $format=shift if @ARGV;
87 # For subsecond resolution, Time::HiRes is needed.
88 my $hires=0;
89 if ($format=~/\%\.[SsT]/ || $mono) {
90 require Time::HiRes;
91 use Time::HiRes qw(CLOCK_MONOTONIC);
92 $hires=1;
95 my $lastseconds = 0;
96 my $lastmicroseconds = 0;
97 my $monodelta;
99 if ($mono) {
100 my $raw_time = Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
101 $lastseconds = time;
102 $lastmicroseconds = int(1000000 * ($raw_time - int($raw_time)));
103 $monodelta = time - int($raw_time);
105 elsif ($hires) {
106 ($lastseconds, $lastmicroseconds) = Time::HiRes::gettimeofday();
108 else {
109 $lastseconds = time;
113 while (<>) {
114 if (! $rel) {
115 if ($hires) {
116 my $f=$format;
117 my $seconds;
118 my $microseconds;
119 if ($mono) {
120 my $raw_time =
121 Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
122 $seconds = $monodelta + int($raw_time);
123 $microseconds = int(1000000 * ($raw_time - $seconds));
125 else {
126 ($seconds, $microseconds) = Time::HiRes::gettimeofday();
129 if ($inc || $sincestart) {
130 my $deltaseconds = $seconds - $lastseconds;
131 my $deltamicroseconds = $microseconds - $lastmicroseconds;
132 if ($deltamicroseconds < 0) {
133 $deltaseconds -= 1;
134 $deltamicroseconds += 1000000;
136 if ($inc) {
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));
147 else {
148 if ($inc || $sincestart) {
149 my $seconds = time;
150 my $deltaseconds = $seconds - $lastseconds;
151 if ($inc) {
152 $lastseconds = $seconds;
154 print strftime($format, localtime($deltaseconds));
156 else {
157 print strftime($format, localtime);
160 print " ".$_;
162 else {
163 s{\b(
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
176 # 16 Jun 94 07:29:35
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
181 # lastlog format
184 $use_format
185 ? strftime($format, localtime(str2time($1)))
186 : concise(ago(time - str2time($1), 2))
187 }exg;
189 print $_;