secret
Last active

  • Download Gist
average_time.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.