Skip to content

Instantly share code, notes, and snippets.

@mikehodgson
Created October 11, 2009 18:19
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 mikehodgson/5194d2a4659cac5369bd to your computer and use it in GitHub Desktop.
Save mikehodgson/5194d2a4659cac5369bd to your computer and use it in GitHub Desktop.
require 'time'
def average_time_of_day(array_of_times)
total = array_of_times.inject(0) { |total,current| total + Time.parse(current).to_i }
Time.at(total / array_of_times.size).strftime('%I:%M %p').downcase!
end
# Assumes same day
puts average_time_of_day(["12:00 am", "11:59 pm", "11:53 pm"])
# Also works
puts average_time_of_day(["Nov 2 12:00 am", "Nov 1 11:59 pm", "Nov 1 11:53 pm"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment