Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Average start time handling midnight

by choroba (Cardinal)
on Jul 21, 2016 at 14:11 UTC ( [id://1168232]=note: print w/replies, xml ) Need Help??


in reply to Average start time handling midnight

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,

Replies are listed 'Best First'.
Re^2: Average start time handling midnight
by GotToBTru (Prior) on Jul 21, 2016 at 19:24 UTC

    I'm curious about the rationale for choosing to average the shorter of the two possible sequences? Being events in time, there is definitely an order to them.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168232]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found