perl and time

Discussion in 'Perl Misc' started by Drunken Canadian, Oct 16, 2004.

  1. 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....
     
    Drunken Canadian, Oct 16, 2004
    #1
    1. Advertising

  2. 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" <> 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....
    >
    >
    >
    >
     
    Jerry Preston, Oct 16, 2004
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Oct 16, 2004
    #3
  4. Drunken Canadian <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 16, 2004
    #4
  5. 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
     
    John W. Krahn, Oct 16, 2004
    #5
  6. 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
     
    Gunnar Hjalmarsson, Oct 17, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engsol
    Replies:
    2
    Views:
    992
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    502
    Magnus Lycka
    Aug 5, 2005
  3. flamesrock
    Replies:
    8
    Views:
    546
    Hendrik van Rooyen
    Nov 24, 2006
  4. David Joseph Bonnici
    Replies:
    1
    Views:
    231
    Gunnar Hjalmarsson
    May 15, 2005
  5. phal

    Perl time and Mysql time

    phal, Sep 27, 2005, in forum: Perl Misc
    Replies:
    13
    Views:
    682
Loading...

Share This Page