date - Perl - Calculate difference between two timestamps -
i trying calculate difference between 2 timestamps in following format:
15:45:30.125
i have tried using date::parse package in perl following:
my $s1 = str2time( $timestamp1 ); $s2 = str2time( $timestamp2 );
and subtracting $s1 , $s2 find difference rounding nearest second rather including milliseconds... there alternative or way of including millisecond count when subtracting values?
i recommend use time::hires::value
module supports arithmetic on objects contain separate second , microsecond values — same value returned time::hires::gettimeofday
. provides convenient stringification overload
you need write simple parsing subroutine though, this
use strict; use warnings; use time::hires::value; $t0 = parse_time('15:45:30.125'); $t1 = parse_time('18:12:46.886'); print $t1 - $t0; sub parse_time { ($h, $m, $s, $ms) = shift =~ /\d+/g; time::hires::value->new(($h * 60 + $m) * 60 + $s, $ms * 1000); }
output
8836.761000