Time::HiRes::tv_interval seems to work just fine for this.
use strict; use Time::HiRes qw|tv_interval|; use Test::More tests=> 5; is(timeDiff('01:00:00.000', '02:00:00.000'), 1*60*60, 'one hour differ +ence'); is(timeDiff('01:00:00.000', '01:01:00.000'), 1*60, 'one minute differe +nce'); is(timeDiff('01:00:00.000', '01:00:01.000'), 1, 'one second difference +'); # 45 milliseconds is xx part of a second. is(timeDiff('01:00:00.000', '01:00:00.045'), 45/1000000, 'fraction dif +ference'); my $Starttime = "05:07:53.249"; my $Finishtime = "05:07:55.401"; my $result = timeDiff($Starttime, $Finishtime); is(timeDiff($Starttime, $Finishtime), 2 + ((401-249)/1000000)); print "Difference (floating point seconds): $result\n"; sub timeDiff { my $st = shift; my $et = shift; return tv_interval( buildHRTime($st), buildHRTime($et)); } sub buildHRTime { my $string = shift; my $seconds = 0; my @c = split(/[:.]/, $string); $seconds += $c[0] * 60 * 60; # hours to seconds $seconds += $c[1] * 60; # minutes to seconds $seconds += $c[2]; # seconds return [$seconds, $c[3]]; }
In reply to Re: time difference in milliseconds
by osunderdog
in thread time difference in milliseconds
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |