Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl and time

Reply
Thread Tools

perl and time

 
 
Drunken Canadian
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....




 
Reply With Quote
 
 
 
 
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....
>
>
>
>



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-16-2004
Drunken Canadian wrote:
>
> time A = 00:01:04
> time b = 0:0:58
>
> how can i add the two times to be 00:02:02?


sub addtime {
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]
}

print addtime('00:01:04', '0:0:58'), "\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-16-2004
Drunken Canadian <(E-Mail Removed)> wrote:



> 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;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-16-2004
Drunken Canadian wrote:
> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-17-2004
Gunnar Hjalmarsson wrote:
> Drunken Canadian wrote:
>>
>> time A = 00:01:04
>> time b = 0:0:58
>>
>> how can i add the two times to be 00:02:02?

>
> sub addtime {
> 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]
> }
>
> print addtime('00:01:04', '0:0:58'), "\n";


Or why not shorten the sub to:

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



--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveState Perl and MinGW [was: Perl 5.14 Windows Strawberry Perl 64 bits] Dilbert Perl Misc 0 11-10-2011 02:20 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Perl time and Mysql time phal Perl Misc 13 09-30-2005 07:28 AM
GMT time to local time, according to timezone and summer/winter time. David Joseph Bonnici Perl Misc 1 05-15-2005 09:15 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments