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); }

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

In reply to Re: Average start time handling midnight by perldigious
in thread Average start time handling midnight by chrisjej

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.