G'day chrisjej,

Here's another way to do it using the builtin modules Time::Piece and Time::Seconds.

I've shown averages truncated to whole minutes (as per your OP) and also averages that haven't been truncated (i.e. shows seconds in the output).

#!/usr/bin/env perl use strict; use warnings; use Time::Piece; use Time::Seconds; use Test::More; my @ranges = ( ['11:00', '13:00', '12:00', '12:00:00'], ['23:00', '01:00', '00:00', '00:00:00'], ['23:30', '00:30', '00:00', '00:00:00'], ['12:00', '12:00', '12:00', '12:00:00'], ['00:00', '00:00', '00:00', '00:00:00'], ['00:00', '00:02', '00:01', '00:01:00'], ['00:00', '00:01', '00:00', '00:00:30'], ['23:58', '00:00', '23:59', '23:59:00'], ['23:59', '00:00', '23:59', '23:59:30'], ); plan tests => @ranges * 2; test_average($_) for @ranges; sub test_average { my ($range) = @_; my $t0 = get_tp($range->[0]); my $t1 = get_tp($range->[1]); $t1 += ONE_DAY if $t1 < $t0; my $avg = ($t1->epoch + $t0->epoch) / 2; ok( Time::Piece->strptime($avg, '%s')->strftime('%H:%M') eq $range +->[2], "$range->[0] -> $range->[1]: Avg = $range->[2] [HH:MM]" ); ok( Time::Piece->strptime($avg, '%s')->strftime('%H:%M:%S') eq $ra +nge->[3], "$range->[0] -> $range->[1]: Avg = $range->[3] [HH:MM:SS]" ); } sub get_tp { Time::Piece->strptime(shift, '%H:%M') }

Output:

1..18 ok 1 - 11:00 -> 13:00: Avg = 12:00 [HH:MM] ok 2 - 11:00 -> 13:00: Avg = 12:00:00 [HH:MM:SS] ok 3 - 23:00 -> 01:00: Avg = 00:00 [HH:MM] ok 4 - 23:00 -> 01:00: Avg = 00:00:00 [HH:MM:SS] ok 5 - 23:30 -> 00:30: Avg = 00:00 [HH:MM] ok 6 - 23:30 -> 00:30: Avg = 00:00:00 [HH:MM:SS] ok 7 - 12:00 -> 12:00: Avg = 12:00 [HH:MM] ok 8 - 12:00 -> 12:00: Avg = 12:00:00 [HH:MM:SS] ok 9 - 00:00 -> 00:00: Avg = 00:00 [HH:MM] ok 10 - 00:00 -> 00:00: Avg = 00:00:00 [HH:MM:SS] ok 11 - 00:00 -> 00:02: Avg = 00:01 [HH:MM] ok 12 - 00:00 -> 00:02: Avg = 00:01:00 [HH:MM:SS] ok 13 - 00:00 -> 00:01: Avg = 00:00 [HH:MM] ok 14 - 00:00 -> 00:01: Avg = 00:00:30 [HH:MM:SS] ok 15 - 23:58 -> 00:00: Avg = 23:59 [HH:MM] ok 16 - 23:58 -> 00:00: Avg = 23:59:00 [HH:MM:SS] ok 17 - 23:59 -> 00:00: Avg = 23:59 [HH:MM] ok 18 - 23:59 -> 00:00: Avg = 23:59:30 [HH:MM:SS]

— Ken


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