Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: time difference in milliseconds

by osunderdog (Deacon)
on Nov 05, 2004 at 19:53 UTC ( #405607=note: print w/replies, xml ) Need Help??

in reply to time difference in milliseconds

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]]; }

"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://405607]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2018-07-22 03:23 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (451 votes). Check out past polls.