Skip to content

@cfeduke /average_time.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# average_time.rb
# makes assumptions about times fed between midday and midnight
# if the lowest time is closer to midnight than the latest time is to midday, assumes we are crossing
# midnight while averaging
require 'time'
SECONDS_IN_DAY = 86400
MIDNIGHT = Time.parse("12:00AM").to_i
MIDDAY = Time.parse("12:00PM").to_i
def average_time_of_day(times)
seconds = []
times.each {|time| seconds << Time.parse(time).to_i}
seconds.sort!
if (seconds.first - MIDNIGHT) < (seconds.last - MIDDAY)
seconds.map! {|s| s < MIDDAY ? s += SECONDS_IN_DAY : s }
end
Time.at(seconds.inject { |sum,n| sum += n }.to_f / seconds.length).strftime("%I:%M%p").downcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.