in reply to Average start time handling midnight
An obvious approach is to convert times to seconds, find the midpoint, adjust and return, like so:
use 5.16.2; use POSIX 'strftime'; my @test = (['11:00', '13:00'], ['23:00', '01:00'], ['10:30', '13:00'] +, ['19:40', '01:20']); foreach my $tst (@test) { print "Testing '$tst->[0]', '$tst->[1]': "; say getmid($tst->[0], $tst->[1]); } sub getmid($$) { my ($start, $end) = @_; $start =~ /^(\d{2}):(\d{2})$/ or die "invalid start date provided: + '$start', must be in format hh:mm\n"; my $startseconds = $1 * 3600 + $2 *60; $end =~ /^(\d{2}):(\d{2})$/ or die "invalid end date provided: '$e +nd', must be in format hh:mm\n"; my $endseconds = $1 * 3600 + $2 * 60; my $range; if ($endseconds < $startseconds) { $range = 86400 - $startseconds ++ $endseconds } else { $range = $endseconds - $startse +conds } my $average = $startseconds + int($range / 2); return strftime("%H:%M", gmtime($average)); }
|
---|