There's more than one way to do things PerlMonks

### 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.

OSUnderdog

Create A New User
Node Status?
node history
Node Type: note [id://405607]
help
Chatterbox?
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
Sections?
Information?
Find Nodes?
Leftovers?
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.

Notices?