I'm not sure I fully understood your requirements, and whether I covered all the possibilities, but the following seems to give reasonable results to me:

#!/usr/bin/perl use warnings; use strict; sub avg_time { my ($s1, $s2) = @_; my ($t1, $t2) = map 60 * (split /:/)[0] + (split /:/)[1], $s1, $s2 +; my $avg = ($t1 + $t2) / 2; # The times are closer to each other via midnight than via noon. $avg += 12 * 60 if abs($t1 - $t2) > 12 * 60; # Don't report hours > 23. $avg %= 24 * 60; return join ':', map sprintf('%02d', $_), int $avg / 60, $avg % 60 } use Test::More; is avg_time('11:00', '13:00'), '12:00'; is avg_time('23:00', '01:00'), '00:00'; is avg_time('10:59', '13:01'), '12:00'; is avg_time('22:59', '01:01'), '00:00'; is avg_time('03:00', '21:00'), '00:00'; is avg_time('10:20', '22:10'), '16:15'; is avg_time('10:10', '22:20'), '04:15'; done_testing();

Unlike GotToBTru, I don't think 7:00 - 9:00 should give different results to 9:00 - 7:00 (the "next day" tests with both times AM or PM). The other tests mentioned there pass.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re: Average start time handling midnight by choroba
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.