For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2007 > time manipulation getting invalid times









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author time manipulation getting invalid times
jbl

2007-06-26, 10:03 pm




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
QoS@domain.invalid

2007-06-26, 10:03 pm


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


Gunnar Hjalmarsson

2007-06-26, 10: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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com