timestamp: --delta --delta gives linewise delta time.

This commit is contained in:
Ole Tange 2014-02-14 16:23:54 +01:00
parent fc350ac6cc
commit 671b08deca

View file

@ -19,6 +19,8 @@ B<timestamp> prepends stdin (standard input) with a timestamp.
Regard start time as epoch and thus show difference between start time Regard start time as epoch and thus show difference between start time
and now. and now.
If B<--delta> is repeated: The time spent between each line.
=item B<--rfc> =item B<--rfc>
@ -189,18 +191,23 @@ set_defaults();
$|=1; $|=1;
my ($now,$last,$delta,$to_print,$out);
my $start = Time::HiRes::time(); my $start = Time::HiRes::time();
if($opt::delta) { if(@opt::delta) {
$start = Time::HiRes::time(); $now = $start = Time::HiRes::time();
} else { } else {
$start = 0; $start = 0;
} }
my ($now,$delta,$to_print,$out);
while(<>) { while(<>) {
$last = $now;
$now = Time::HiRes::time(); $now = Time::HiRes::time();
$to_print = $now - $start; if($#opt::delta) {
$to_print = $now - $last;
} else {
$to_print = $now - $start;
}
my $tz = strftime("%z", localtime($to_print)); my $tz = strftime("%z", localtime($to_print));
$tz =~ s/(\d{2})(\d{2})/$1:$2/; $tz =~ s/(\d{2})(\d{2})/$1:$2/;
@ -227,7 +234,9 @@ while(<>) {
sub set_defaults { sub set_defaults {
if(not($opt::rfc || $opt::iso || $opt::isotime || $opt::epoch)) { if(not($opt::rfc || $opt::iso || $opt::isotime || $opt::epoch)) {
$opt::epoch = 1; $opt::epoch = 1;
$opt::delta = 1; if(not @opt::delta) {
@opt::delta = (1);
}
} }
} }
@ -236,7 +245,7 @@ sub parse_options {
$Global::progname = 'timestamp'; $Global::progname = 'timestamp';
Getopt::Long::Configure ("bundling","require_order"); Getopt::Long::Configure ("bundling","require_order");
GetOptions("delta|d" => \$opt::delta, GetOptions("delta|d" => \@opt::delta,
# RFC822 # RFC822
"rfc|r" => \$opt::rfc, "rfc|r" => \$opt::rfc,
"iso|i" => \$opt::iso, "iso|i" => \$opt::iso,
@ -247,7 +256,6 @@ sub parse_options {
"help|h" => \$opt::help, "help|h" => \$opt::help,
"version|V" => \$opt::version, "version|V" => \$opt::version,
) || die_usage(); ) || die_usage();
if(defined $opt::help) { die_usage(); } if(defined $opt::help) { die_usage(); }
if(defined $opt::version) { version(); exit(0); } if(defined $opt::version) { version(); exit(0); }
$Global::debug = $opt::debug; $Global::debug = $opt::debug;