Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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'
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}
if (seconds.first - MIDNIGHT) < (seconds.last - MIDDAY)! {|s| s < MIDDAY ? s += SECONDS_IN_DAY : s }
end { |sum,n| sum += n }.to_f / seconds.length).strftime("%I:%M%p").downcase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment