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

I have posted before....but i'm still working on this.

$row3 is start minute
$row5 is end minute

All i need to do is round the minutes for each
Join the start hour to the start minute and
the end hour to the end minute. then subtract the end time
from the start time to get a total elapsed time (rounded of course)
I just can't seem to get a total.
################ BEGIN TIME TOTALING SUBROUTINE sub calc_total{ # HAVE TO CONVERT TIME INPUT if ($row[3] >= 52.5) {$start_minute = 1.0 ;} elsif ($row[3] >= 37.5) {$start_minute = .75 ;} elsif ($row[3] >= 22.5) {$start_minute = .5 ;} elsif ($row[3] >= 7.5) {$start_minute = .25 ;} else {$row[3] = .0 ;} if ($row[5] >= 52.5) {$end_minute = 1.0 ;} elsif ($row[5] >= 37.5) {$end_minute = .75 ;} elsif ($row[5] >= 22.5) {$end_minute = .5 ;} elsif ($row[5] >= 7.5) {$end_minute = .25 ;} else {$row[5] = .0 ;} $start_total = $row[2] + $start_minute; $end_total = $row[4] + end_minute; $total_time = $end_total - $start_total; } ################ END TIME TOTALING SUBROUTINE

Replies are listed 'Best First'.
Re: Time Totaling
by Zaxo (Archbishop) on Aug 02, 2001 at 01:43 UTC

    What is in @row[2,4]? If it is start hour and end hour, you are adding hours to minutes in your totals as if they were the same unit of time.

    To round floating point minutes to quarter-minutes, something like ((4*$minutes+0.5)|0)/4 replaces all that iffing and elsing.

    Update Ahh, wasn't aware of the history cited by lestrrat. If you are tracking phone calls or web sessions you need to consider those which start before midnight and end after. Your rounding to 15 sec intervals suggest you won't see sessions of a day or more, but I don't know that's true. Adding hours to minutes suggests that you haven't yet understood the problem.

    After Compline,
    Zaxo

Re: Time Totaling
by VSarkiss (Monsignor) on Aug 02, 2001 at 02:33 UTC

    I'm not sure I've understood, but I guess @row[2,3] are the starting hour and minute, and @row[4,5] are the ending hour and minute. You want to get the total elapsed time, presumably in hours and minutes.

    If so, don't round the minutes up to hours; multiply the hour by 60 to get minutes, add it to the minutes, then substract start from end (everything in the smallest units):

    $start_total = 60*$row[2] + $row[3]; $end_total = 60*$row[4] + $row[5]; $total_time = $end_total - $start_total; # in minutes
    If you want to render the $total_time as hours and minutes, use integer division and remainder by 60:
    $total_hr = int($total_time/60) # $total_time >= 0 $total_min = $total_time%60;
    You can then use sprintf or something to render it in a nice "hh:mm" format.

    HTH

Re: Time Totaling
by nlafferty (Scribe) on Aug 02, 2001 at 01:51 UTC
    Row2 and Row 4 are the start and end hour. However. I want to convert the minute to base 100 from base 60. That is why i have the elsif statments. Could i do
    $start_total = $row[2].$start_minute
    But this creates a problem if the minute is rounded to 1.0

      Dude! Read carefully the advices that we gave you !

      I guess boo_radley was correct in pointing out "what exactly do you mean by decimal?" , cause judging by the way you are doing this, you somehow got our advices wrong

      Everybody was telling you to either:

      • use epoch time, or
      • use the database's native time format

      We were trying to tell you that you can't reliably compute the elapsed time by just having the starting hour, starting minute, end hour and end minute. If you want to do that, you *must* assume that those times were recorded within the same day, between 00:00am to 23:59pm. Otherwise, you *always* need to know the date to compute the time elapsed

      Is there some reason you can't change those entries?

Re: Time Totaling
by nlafferty (Scribe) on Aug 02, 2001 at 22:54 UTC
    Well. I finally finished this script and it's working nicely. Actually I would like to have it work past the current day. However it is not nessecery because my use only uses same day entries. I do plan on release the code as my first useful program (which i will add this feature to eventually) But for now. I have many other things to work on. Thanks you for all the Help