# perl and time

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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....

## Re: perl and time

Look at http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod">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

## Re: perl and time

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

## Re: perl and time

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

## Re: perl and time

\$ 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