[PBP-pm] Handling fractions of seconds
Tolkin, Steve
Steve.Tolkin at FMR.COM
Fri Dec 23 13:20:55 PST 2005
One problem with Time::Local is that it does not handle fractions of
seconds.
When I discovered this the hard way (error message) I looked for the
"use integer;" line I expected at the top of the module
and removed it. Then if failed
when the number of seconds was > 59 e.g. 59.7.
So I fixed that check to allow up to 60.
But then I discovered that it produced wrong results!
(I tested that by using "scalar localtime(...)" to convert
its output number back to a string format, and the output was
very different from the input.)
So I put the "use integer;" back and now I manually add
the number of seconds since this epoch like this:
my $end_seconds = timelocal(0,$min,$hour,$mday,$mon,$year)+$sec;
I am using this to compute elapsed time between two events
that can be less than 1 second apart.
They can cross a time boundary at any grain (e.g. minute, hour, day).
I also looked at Date::Manip but the POD seems to suggest
it also does not handle fractions of seconds, saying:
4. The amount of time between two dates.
...
$delta=&DateCalc($date1,$date2,\$err);
=> 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
and seconds between the two
Since it does not show SS.nnn I guess it does not support fractions of
seconds.
Two questions:
1. What date module supports fractional seconds.
Ideally it would be in the perl 5 core.
2. I thought use integer was supposed to be purely an performance
optimization, If this is true there is a bug in Time::Local
and I will try to create a small reproducible test case later/
Thanks,
Steve
---
Steven Tolkin
There is nothing so practical as a good theory. Comments are by me, not
Fidelity Investments, its subsidiaries or affiliates.
More information about the PBP-pm
mailing list