-
Notifications
You must be signed in to change notification settings - Fork 13
/
dts
executable file
·71 lines (62 loc) · 1.75 KB
/
dts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env perl
#
# dts - runs a command, date/time-stamping each line of STDOUT
#
# Understands a single optional argument for the type of format to use.
# l => localtime long format - DEFAULT
# g => gmtime long format, e.g. 2003/09/05 09:52:17.139092
# a => absolute seconds
#
# See also ts from moreutils which does pretty much the same thing:
# http://git.kitenet.net/?p=moreutils.git;a=blob;f=ts;hb=HEAD
use strict;
use warnings;
use Date::Calc 'Day_of_Week_Abbreviation';
use Getopt::Long;
use Time::HiRes;
$| = 1; # turn output buffering off
(my $ME = $0) =~ s,.*/,,;
my %opts = ();
Getopt::Long::Configure('bundling', 'passthrough');
GetOptions(
\%opts,
'help|h',
'gmtime|g', 'epoch|e', 'usec|u',
) or usage();
usage() if $opts{help};
sub usage {
warn @_, "\n" if @_;
die <<EOUSAGE;
Usage: $ME [options] COMMAND [ARGS...]
If COMMAND is omitted, acts as a STDIN filter instead.
Options:
-h, --help Show this help
-g, --gmtime Use UTC not local time
-e, --epoch Show seconds since UNIX epoch
-u, --usec Show microseconds
EOUSAGE
}
my $printformat = "%04d/%02d/%02d %3s %02d:%02d:%02d";
$printformat .= ".%06d" if $opts{usec};
$printformat .= " ";
open(CMD, @ARGV ? "@ARGV |" : "<&STDIN") or die $!;
while(<CMD>) {
my ($seconds, $usec) = Time::HiRes::gettimeofday();
if ($opts{epoch}) {
print $seconds . "." . $usec . " " . $_;
}
else {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= $opts{gmtime} ? gmtime($seconds) : localtime($seconds);
printf $printformat,
1900 + $year,
$mon + 1,
$mday,
Day_of_Week_Abbreviation($wday || 7),
$hour,
$min,
$sec,
$opts{usec} ? $usec : ();
print;
}
}