Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between log timestamps

0 views
Skip to first unread message

R C V

unread,
Mar 17, 2008, 9:08:35 PM3/17/08
to
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

sandy_s...@yahoo.com

unread,
Mar 17, 2008, 9:36:07 PM3/17/08
to

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/

sandy_s...@yahoo.com

unread,
Mar 17, 2008, 9:39:27 PM3/17/08
to
On Mar 17, 6:36 pm, sandy_sayda...@yahoo.com wrote:
> On Mar 17, 6:08 pm, R C V <rss...@gmail.com> wrote:

> nanosecond => 543 * 1000000,

Sorry, too many zeros, but you got an idea

/sandy
http://myperlquiz.com/

Gunnar Hjalmarsson

unread,
Mar 17, 2008, 10:14:23 PM3/17/08
to
R C V wrote:
> 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";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

0 new messages