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

Background
I have a simple question and i am sure this problem has been asked before. I have a bunch of times (in 24 hunder hour format e.g., 0930, 2300) that have a toTime and fromTime. All i have to do is take the time passed between each toTime and fromTime and add these resulting time(s) to form a grand total.

Example
add the following times:

toTime -- fromTime 0715 0945 = 2 hrs and 30 mins 1107 1256 = 1 hr and 49 mins 0845 0915 = 0 hr and 30 mins

Giving us a total of Total Time in Hrs and Mins = 4 hours and 49 mins
This is what i am trying to do.

Here is what i have for now, one of the main problems i face is how to convert a number of minutes to hr/min format.

#!perl use Data::Dumper; print "this is a test\n"; my %HoH = ( "toTime" => "1220", "fromTime" => "0930" ); #print Dumper (\%HoH); my $fromTime = $HoH{"fromTime"}; my $toTime = $HoH{"toTime"}; print "$fromTime\n"; my ($fromTimeHr, $fromTimeMin, $toTimeHr, $toTimeMin); if ($fromTime =~ /(\d{2})(\d{2})/) { print "FOMRTIME Hr = $1 / Min = $2\n"; $fromTimeHr = $1; $fromTimeMin = $2; } if ($toTime =~ /(\d{2})(\d{2})/) { print "TOTIME Hr = $1 / Min = $2\n"; $toTimeHr = $1; $toTimeMin = $2; } # Now convert Hrs to Mins and and all Mins my $totalFromTime = ($fromTimeHr * 60) + $fromTimeMin; my $totalToTime = ($toTimeHr * 60) + $toTimeMin; print "Total From Time Mins = $totalFromTime\n"; print "Total To Time Mins = $totalToTime\n";

As always, thanks in advance.

Replies are listed 'Best First'.
Re: calculate hours of day
by Zaxo (Archbishop) on Sep 07, 2003 at 21:41 UTC
      The CPAN is one of the greatest advantages Perl has over most other programming languages. It is searchable. You can configure it so that it will download and test packages prior to installation. It will also offer to download dependencies.

      The CPAN is really a timesaver! It is easy to use, and you will find common problems solved so that you can concentrate on solving the unique parts of your problem.

      One word of advice, though. Test upgrades on a test system first. I've encountered undocumented or poorly documented changes to Perl modules. They don't occur often, but they do occur.

Re: calculate hours of day
by Not_a_Number (Prior) on Sep 07, 2003 at 22:33 UTC

    Two problems that might arise (and two reasons you might consider following Zaxo's advice):

    1) What happens if your $fromTime is say 2359 and your $toTime is 0009? It would be fairly easy to adjust eg CombatSquirrel's solution for this, but only if you can be sure that the $toTime is on the following day, ie that the elapsed time is 10 minutes, and not 24 (or 48, or 72...) hours and 10 minutes.

    2) Twice a year, if you live in a country that has daylight saving time, you will have wrong results in the early hours of the morning :-)

    dave

Re: calculate hours of day
by PodMaster (Abbot) on Sep 08, 2003 at 01:07 UTC
    Just for good measure, you could always use the DateTime distribution, something like
    use DateTime::Duration; my $toTime = "1220"; my $fromTime = "0930"; my $to = DateTime::Duration->new( hours => substr($toTime,0,2), minutes => substr($toTime,2,2), ); printf "%02d%02d\n", $to->hours,$to->minutes; my $from = DateTime::Duration->new( hours => substr($fromTime,0,2), minutes => substr($fromTime,2,2), ); printf "%02d%02d\n", $from->hours,$from->minutes; $to->subtract_duration( $from ); # $to = $to - $from ; printf "%02d%02d\n", $to->hours,$to->minutes; print $to->hours, " hours and ", $to->minutes, " minutes\n"; use DateTime; $to = DateTime->now; $to->set(hour => substr($toTime,0,2) ); $to->set(minute => substr($toTime,2,2) ); print $to->strftime('%A, %B %d, %Y @ %H%M'),"\n"; print $to->subtract(hours => substr($fromTime,0,2) ) ->subtract(minutes => substr($fromTime,2,2) ) ->strftime('%A, %B %d, %Y @ %H%M'),"\n"; __END__ 1220 0930 0250 2 hours and 50 minutes Monday, September 08, 2003 @ 1220 Monday, September 08, 2003 @ 0250

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: calculate hours of day
by CombatSquirrel (Hermit) on Sep 07, 2003 at 21:43 UTC
    Perhaps something like this:
    sub delta_time { my ($start, $end) = @_; $start = substr($start, 0, 2) * 60 + substr($start, 2, 2); $end = substr($end, 0, 2) * 60 + substr($end, 2, 2); return $end - $start; } my ($minutes, $hours); $minutes = delta_time '0930', '1145'; $hours = int($minutes / 60); $minutes %= 60; print "Delta: $hours:$minutes\n";
    Cheers,
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: calculate hours of day
by gwadej (Chaplain) on Sep 07, 2003 at 21:49 UTC

    Unless I'm really misreading your question, converting a number of minutes to hours and minutes is trivial. Divide/mod by 60.

    my $timeInMinutes = 12345; my $timeHrs = int($timeInMinutes/60); my $timeMin = $timeInMinutes % 60;

    It's the inverse of what you did to convert hours/minutes to total minutes.

    G. Wade