#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |