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

All i need to do is add hours and minutes to get a total time. Here is what i have so far.
while (@row = $sth->fetchrow()){ $total_minutes = $row[5]; $total_hours = $row[4] * 60; #multiply hours by 60 to get minute +s $all_minutes = $total_hours + $total_minutes; $total_time = $total_time + $all_minutes; $total_time_hours += int($total_time / 60); $total_time_minutes = $total_time % 60; $all_time = "$total_time_hours:$total_time_minutes"; ## Make our HTML look better if no data. $row[0] = "&nbsp;" if($row[0] eq ""); $row[1] = "&nbsp;" if($row[1] eq ""); $row[2] = "&nbsp;" if($row[2] eq ""); $row[3] = "&nbsp;" if($row[3] eq ""); $row[4] = "&nbsp;" if($row[4] eq ""); $row[5] = "&nbsp;" if($row[5] eq ""); $row[6] = "&nbsp;" if($row[6] eq ""); @date_array01 = split(/ /,$row[2]); $print_date = $date_array01[0]; $in_time_sec = $date_array01[1]; @in_time_array = split(/:/,$in_time_sec); $in_time = join(":",$in_time_array[0],$in_time_array[1]); @date_array02 = split(/ /,$row[3]); $out_time_sec = $date_array02[1]; @out_time_array = split(/:/,$out_time_sec); $out_time = join(":",$out_time_array[0],$out_time_array[1]); print <<HTML; <TR BGCOLOR="#FFFFFF"> <TD><FONT SIZE=2 FACE=ARIAL>$print_date</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$in_time</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$out_time</FONT></TD> <TD BGCOLOR="#FFFFCC"><FONT SIZE=2 FACE=ARIAL>$row[4]:$row[5]</FON +T></TD> <TD><FONT SIZE=2 FACE=ARIAL>$row[6]</FONT></TD> HTML } # End of while. print<<HTML; </TR><tr> <td BGCOLOR="#eecb27" colspan=2></TD> <td><B>Total Time:</B></TD><td bgcolor=#FFFF99><B>$all_time</B></TD>

Basically this while statement prints a table to display all times in a database. At the bottom a total of the number of minutes and hours are displayed at the bottom. In this particular code i'm testing adding two times.
4:23 and
5:13
funny i get 13:36 as the $all_time

Replies are listed 'Best First'.
(jeffa) Re: Time Totaling re-explained
by jeffa (Bishop) on Oct 25, 2001 at 02:24 UTC
    While there are a number of issues i wish i could sit down and discuss with you - there is a simple solution:

    change this:

    $total_time_hours += int($total_time / 60);
    to this:
    $total_time_hours = int($total_time / 60);
    I will plug DBIx::XHTML_Table, but it does not total hours and minutes for you. Looks like i have some work to do now. :)

    jeffa

Re: Time Totaling re-explained
by thunders (Priest) on Oct 25, 2001 at 03:37 UTC
    A couple of comments. I'm assuming that you are declaring the variable $total_time on line 8. But you are not really using it to do anything besides make a copy of $all_minutes so:
    $all_minutes = $total_hours + $total_minutes; $total_time = $total_time + $all_minutes; $total_time_hours += int($total_time / 60);

    can be optimized like this:
    $all_minutes = $total_hours + $total_minutes; $total_time_hours = int($all_minutes / 60);

    also the lines
    $row[0] = "&nbsp;" if($row[0] eq ""); $row[1] = "&nbsp;" if($row[1] eq ""); $row[2] = "&nbsp;" if($row[2] eq ""); etc..

    could be rewritten as:
    foreach $item(@row){ $item = "&nbsp;" if($item eq ""); }

    BTW: I'm not criticizing the way you code, just offering tips to help you with maintainence and readability. So please don't take offence.
Re: Time Totaling re-explained
by cLive ;-) (Prior) on Oct 25, 2001 at 03:21 UTC
    Hmm, here's how I'd add times (stretched out a bit to make it easier to read) - probably not the most efficient but... TIMTOWTDI :)
    my $time1 = '4:23'; my $time2 = '5:13'; my $total_time = add_times($time1,$time2); print "Total time: $total_time\n"; exit(0); sub add_times { # read in times sent my @times = @_; my ($hours,$mins); for (@times) { # get values for this time my ($this_hour,$this_min) = split /:/; # keep running total $hours += $this_hour; $mins += $this_min; } # no of hours stored in $mins $hours += int($mins/60); # remainder after deviding by 60 $mins %= 60; return "$hours:$mins"; }

    With this sub being able to take as many times as you want to send to it...

    cLive ;-)

      The only comment I would offer is to left pad your minutes with a zero.

      #-- Original return "$hours:$mins"; #-- Suggestion return $hours.':'.substr('00'.$mins,-2);

      To see the difference, run the supplied sample with times of 5:33 and 4:33.

      Update:

      return sprintf("%d:%02d",$hours,$mins);

      Just as good or better. *Smiles*

        Ah yes, good point - TIMTOWTDI, but I think sprintf is more your friend for this:
        return sprintf("%d:%02d",$hours,$mins);
        cLive ;-)