Skip to content

Instantly share code, notes, and snippets.

@cfeduke
Created October 9, 2009 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfeduke/5b371226faf83af50d7e to your computer and use it in GitHub Desktop.
Save cfeduke/5b371226faf83af50d7e to your computer and use it in GitHub Desktop.
# 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