scubaelmo has asked for the wisdom of the Perl Monks concerning the following question:

Is there a simple way to add time variables together? For example if i have a time of 1:46 that is in a variable such as: $time1 = 146; Is there a way to add another variable, $time2 = 547; And have it adjusted to equal the right time? Any suggestions would be greatly appreciated... Scubaelmo

Replies are listed 'Best First'.
Re: adding time variables together
by Masem (Monsignor) on Nov 03, 2001 at 01:28 UTC
    It's a weird way to store time, in the first place...:-)

    However, to add times with adjustments in hours, you can do the following:

    sub addtime { my ( $time1, $time2 ) = @_; my $min1 = $time1 % 100; my $hour1 = $time1 - $min1; my $min2 = $time2 % 100; my $hour2 = $time2 - $min2; my $hour3 = $hour1+$hour2; my $min3 = $min1+$min2; if ( $min3 > 59 ) { $min3 -= 60; $hour3++; } return $hour3*100 + $min3; }

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: adding time variables together
by lachoy (Parson) on Nov 03, 2001 at 03:32 UTC

    Check out Class::Date, a nifty module that lets you add and subtract dates from one another using normal arithmetic. It also includes formatting and some simple date parsing, and can rely on other modules for more complicated date parsing. (The latest version --1.07 -- has some issues compiling on 5.005_03, but the earlier ones work fine.)

    Chris
    M-x auto-bs-mode

      Also check out Matt Sergeant's Time::Piece which does the same thing and has the added advantege that it will be part of the standard Perl distribution from 5.8.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

        This looks excellent, and I had no idea that it was around, much less that it would be included with 5.8. Guess I'll have to update those Makefile.PL files again...

        Chris
        M-x auto-bs-mode

Re: adding time variables together
by Rich36 (Chaplain) on Nov 03, 2001 at 01:41 UTC
    Probably the best way to do this is to deal with the time in seconds -- the UNIX standard of seconds since the UNIX Epoch. If you can get away with storing your time that way, it's probably a lot easier to deal with.
    Using functions like time and localtime, you can get the current time in seconds and change it over to a useful format. Using the POSIX module, you can easily format the time. I would suggest running SuperSearch for additional information - the time issue comes up a lot.
    An example...
    #!/usr/bin/perl -w use strict; use POSIX qw(strftime); my $now = time(); #Gets current time # Subtract 3 hours, 15 minutes #(seconds/per minute * minutes/per hour * 3) = 3 hours # plus # (seconds/per minute * 15) my $earlier = $now - ((60 * 60 * 3) + (60 * 15)); # Format the times using POSIX module $now = strftime "%H:%M", localtime($now); $earlier = strftime "%H:%M", localtime($earlier); print "The current time is $now\nThe earlier time is $earlier\n";
    =Output The current time is 15:40 The earlier time is 12:25

    If you have to store your time in the HH:MM format, just follow Masem's excellent advice...
    Rich36


    There's more than one way to screw it up...