Velocity Reviews > Perl > perl and time

# perl and time

Guest
Posts: n/a

 10-16-2004
Ok this may be a noob question but it almost 6am and the coffiee is no
longer working ......

time A = 00:01:04
time b = 0:0:58

how can i add the two times to be 00:02:02?
I know the format is not the same but that is what im stuck with....

Jerry Preston
Guest
Posts: n/a

 10-16-2004

Look at http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod

check out the following:

# \$time = time a + time b

\$time = ( Date_to_Time(\$year,\$month,\$day, \$hour,\$min,\$sec) +
Date_to_Time(\$year,\$month,\$day, \$hour,\$min,\$sec));

(\$year,\$month,\$day, \$hour,\$min,\$sec) = Time_to_Date( \$time);

Jerry

"Drunken Canadian" <(E-Mail Removed)> wrote in message
news:gn5cd.7609\$_u6.4468@edtnps89...
> Ok this may be a noob question but it almost 6am and the coffiee is no
> longer working ......
>
> time A = 00:01:04
> time b = 0:0:58
>
> how can i add the two times to be 00:02:02?
> I know the format is not the same but that is what im stuck with....
>
>
>
>

Guest
Posts: n/a

 10-16-2004
>
> time A = 00:01:04
> time b = 0:0:58
>
> how can i add the two times to be 00:02:02?

my @t1 = split /:/, shift;
my @t2 = split /:/, shift;
my \$sec = (\$t1[0]+\$t2[0])*3600 +
(\$t1[1]+\$t2[1])*60 + \$t1[2]+\$t2[2];
sprintf '%02d:%02d:%02d', (gmtime \$sec)[2,1,0]
}

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Guest
Posts: n/a

 10-16-2004

> time A = 00:01:04
> time b = 0:0:58

> I know the format is not the same but that is what im stuck with....

You can normalize to a more convenient format:

\$time = sprintf '%02d:%02d:%02d', split /:/, \$time;

--
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

John W. Krahn
Guest
Posts: n/a

 10-16-2004
> Ok this may be a noob question but it almost 6am and the coffiee is no
> longer working ......
>
> time A = 00:01:04
> time b = 0:0:58
>
> how can i add the two times to be 00:02:02?
> I know the format is not the same but that is what im stuck with....

\$ perl -e'
\$a = "00:01:04";
\$b = "0:0:58";
printf "%10s%10s\n", \$a, \$b;

\$x = 2; \$d += \$_ * 60 ** \$x-- for \$a =~ /\d+/g;
\$x = 2; \$e += \$_ * 60 ** \$x-- for \$b =~ /\d+/g;
printf "%10s%10s\n", \$d, \$e;

\$c = \$d + \$e;
printf "%02d:%02d:%02d\n", reverse \$c % 60, int(\$c /= 60) % 60, int(\$c /= 60)
% 60;
'
00:01:04 0:0:58
64 58
00:02:02

John
--
use Perl;
program
fulfillment

Guest
Posts: n/a

 10-17-2004
>>
>> time A = 00:01:04
>> time b = 0:0:58
>>
>> how can i add the two times to be 00:02:02?

>
> my @t1 = split /:/, shift;
> my @t2 = split /:/, shift;
> my \$sec = (\$t1[0]+\$t2[0])*3600 +
> (\$t1[1]+\$t2[1])*60 + \$t1[2]+\$t2[2];
> sprintf '%02d:%02d:%02d', (gmtime \$sec)[2,1,0]
> }
>

Or why not shorten the sub to:

my \$sec;
while (@_) {
my @t = reverse split /:/, shift;
\$sec += \$t[\$_]*60**\$_ for 0..2;
}
sprintf '%02d:%02d:%02d', (gmtime \$sec)[2,1,0]
}

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl