f



time manipulation getting invalid times



I have a script that deals with time in hours, minures, seconds and
milliconds. Initially I was using the round function from Math::Round 

It was working fine then I noticed I was getting an error occasionaly.

Turned out it was when the seconds were 59 and the milliseconds were
above 500, I was rounding the minutes from 59 to 60 yeilding invalid
times like 13 minutes and 60 seconds instead of 14 minutes and 00
seconds.

Example:
HH:MM:SS,miliseconds

$lines[0] = 00:13:59,934

my $roundedSeconds = sprintf("%02d",(round(substr($lines[0],-6)))); 

Using the round function from Math::Round

I get $roundedSeconds = 13:60 which is 13 minutes and 60 seconds.
Obviously an invalid time.


I believe that a workaround is to just use the int function and
truncate the milliseconds

$lines[0] = 00:13:59,934

my $roundedSeconds = sprintf("%02d",(int(substr($lines[0],-6)))); 

Using the int function I get  $roundedSeconds = 13:59 which is 13
minutes and 59 seconds.

The files have thousands of times in them and I am trying to avoid
building in another error that will take hours to check.

Does anybody see any errors with me using this method (int)

thanks

jbl
0
jbl
6/26/2007 2:08:56 PM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader. Post Follow

2 Replies
485 Views

Similar Articles

[PageSpeed] 34

jbl <jbl02NO@SPAMhotmail.com> wrote in message-id:  <4f72831ktj71d9so522o8cdn8pi8sjf691@4ax.com>

> 
> 
> 
> 
> I have a script that deals with time in hours, minures, seconds and
> milliconds. Initially I was using the round function from Math::Round
> 
> It was working fine then I noticed I was getting an error occasionaly.
> 
> Turned out it was when the seconds were 59 and the milliseconds were
> above 500, I was rounding the minutes from 59 to 60 yeilding invalid
> times like 13 minutes and 60 seconds instead of 14 minutes and 00
> seconds.
> 
> Example:
> HH:MM:SS,miliseconds
> 
> $lines[0] = 00:13:59,934
> 
> my $roundedSeconds = sprintf("%02d",(round(substr($lines[0],-6))));
> 
> Using the round function from Math::Round
> 
> I get $roundedSeconds = 13:60 which is 13 minutes and 60 seconds.
> Obviously an invalid time.
> 
> 
> I believe that a workaround is to just use the int function and
> truncate the milliseconds
> 
> $lines[0] = 00:13:59,934
> 
> my $roundedSeconds = sprintf("%02d",(int(substr($lines[0],-6))));
> 
> Using the int function I get  $roundedSeconds = 13:59 which is 13
> minutes and 59 seconds.
> 
> The files have thousands of times in them and I am trying to avoid
> building in another error that will take hours to check.
> 
> Does anybody see any errors with me using this method (int)
> 
> thanks
> 
> jbl

Yeah your watch runs slow.
int just removes whats to the right of the decimal.
If your script 'deals' with milliseconds then why round or disregard them?
Guess if the script really doesnt need them, why bother checking ms anyway.

Here is a link ive found useful when doing some time related math:
http://datetime.perl.org/

yw

jdm


0
QoS
6/26/2007 3:15:03 PM
jbl wrote:
> I have a script that deals with time in hours, minures, seconds and
> milliconds. Initially I was using the round function from Math::Round

sprintf() should have been sufficient.

> It was working fine then I noticed I was getting an error occasionaly.
> 
> Turned out it was when the seconds were 59 and the milliseconds were
> above 500, I was rounding the minutes from 59 to 60 yeilding invalid
> times like 13 minutes and 60 seconds instead of 14 minutes and 00
> seconds.
> 
> Example:
> HH:MM:SS,miliseconds
> 
> $lines[0] = 00:13:59,934
> 
> my $roundedSeconds = sprintf("%02d",(round(substr($lines[0],-6)))); 
> 
> Using the round function from Math::Round
> 
> I get $roundedSeconds = 13:60 which is 13 minutes and 60 seconds.
> Obviously an invalid time.
> 
> I believe that a workaround is to just use the int function and
> truncate the milliseconds
> 
> $lines[0] = 00:13:59,934
> 
> my $roundedSeconds = sprintf("%02d",(int(substr($lines[0],-6)))); 
> 
> Using the int function I get  $roundedSeconds = 13:59 which is 13
> minutes and 59 seconds.
> 
> The files have thousands of times in them and I am trying to avoid
> building in another error that will take hours to check.
> 
> Does anybody see any errors with me using this method (int)

How about incorrectly rounded time?

This is one approach:

     my $t = '00:13:59,934';
     print t_round( $t ), "\n";

     sub t_round {
         my ($h, $m, $s) = split /:/, shift;
         $s =~ tr/,/./;
         my $sec = 60*60*$h + 60*$m + sprintf '%.0f', $s;
         $s = $sec % 60;
         $m = ( $sec - $s ) / 60 % 60;
         $h = ( $sec - $s - 60*$m ) / ( 60*60 ) % 24;
         sprintf '%02d:%02d:%02d', $h, $m, $s
     }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Gunnar
6/26/2007 4:50:53 PM
Reply: