Hi chrisjej,
See if the following helps. http://mathforum.org/library/drmath/view/63173.html
Doing a simple "floating point hours since midnight" conversion like he shows there may be a simple way to accomplish your goal.
EDIT:
As hippo pointed out, a better defined spec may help the Monks help you out. I tried the following that seems to work (though I haven't done extensive testing by any means), but it presumes that you are always only averaging two times (like you show), and that they are always in chronological order with the earliest time first. Those are some pretty big assumptions.
use strict; use warnings; my @times1 = qw( 11:00 13:00 ); my @times2 = qw( 23:00 1:00 ); my @times3 = qw( 10:30 13:00 ); my @times4 = qw( 19:40 1:20 ); print "times1 average: " . average_start_time(@times1) . "\n"; print "times2 average: " . average_start_time(@times2) . "\n"; print "times3 average: " . average_start_time(@times3) . "\n"; print "times4 average: " . average_start_time(@times4) . "\n"; sub average_start_time { my ($hours1, $minutes1) = split /:/, $_[0]; my ($hours2, $minutes2) = split /:/, $_[1]; my $decimal_hours1 = $hours1 + $minutes1/60; my $decimal_hours2 = $hours2 + $minutes2/60; my $average_decimal_hours = ($decimal_hours1+$decimal_hours2)/2; $average_decimal_hours -= 12 if ($decimal_hours1 > $decimal_hours2 +); $average_decimal_hours += 24 if ($average_decimal_hours < 0); return int($average_decimal_hours) . ":" . sprintf("%02d", ($avera +ge_decimal_hours - int($average_decimal_hours))*60); }
In reply to Re: Average start time handling midnight
by perldigious
in thread Average start time handling midnight
by chrisjej
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |