Difference between log timestamps

R

R C V

Hi,
Can someone tell if they know of any perl modules which can
find the difference between 2 timestamps in a log file..

The timestamp format is as follows:
20080317 130146543
20080317 130156581

So the difference here is 38 msec.

Thanks !
R C
 
S

sandy_saydakov

Hi,
Can someone tell if they know of any perl modules which can
find the difference between 2 timestamps in a log file..

The timestamp format is as follows:
20080317 130146543
20080317 130156581

So the difference here is 38 msec.

Just construct DateTime objects and subtract:

my $dt1 = DateTime->new(
year => 2008,
month => 03,
day => 17,
hour => 13,
minute => 01,
second => 46,
nanosecond => 543 * 1000000,
);

my $dt2 = DateTime->new(
year => 2008,
month => 03,
day => 17,
hour => 13,
minute => 01,
second => 56,
nanosecond => 581 * 1000000,
);

my $duration = $dt2 - $dt1;

print $duration->in_units('days', 'hours', 'seconds', 'nanoseconds'),
"\n";

/sandy
http://myperlquiz.com/
 
G

Gunnar Hjalmarsson

R said:
Can someone tell if they know of any perl modules which can
find the difference between 2 timestamps in a log file..

Most Perl programmers can tell you about a whole bunch of modules that
can help you do that.
The timestamp format is as follows:
20080317 130146543
20080317 130156581

I for one would make use of the most(?) useful date/time module Time::Local.

use Time::Local;
sub timediff {
my @t;
foreach ( @_ ) {
my ($y, $mo, $d, $h, $min, $s, $ms) =
/^(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})(\d{3})$/
or die "Failed to parse timestamp '$_'";
push @t, timelocal($s, $min, $h, $d, $mo-1, $y) + $ms/1000;
}
sprintf '%.3f', $t[1]-$t[0];
}

my $ts1 = '20080317 130146543';
my $ts2 = '20080317 130156581';

print "ts1: $ts1\n",
"ts2: $ts2\n",
'time diff: ', timediff($ts1, $ts2), " seconds\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top