Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'date'
require 'time'
def average_time_of_day(times)
base_time = Time.parse(DateTime.parse("2009-01-01T10:00:00+00:00").strftime("%c")).to_i
total_time = 0
times.each do |time|
date = [2009,1,1]
t = DateTime.strptime(time, fmt='%I:%M%p')
date[2] = t.hour < 22 ? 1+date[2] : date[2]
time_string = sprintf("%02d:%02d:00+00:00", t.hour,t.minute)
time_string = sprintf("%04d-%02d-%02dT#{time_string}", date[0], date[1], date[2])
new_time = DateTime.parse(time_string)
new_time = Time.parse(new_time.strftime("%c")).to_i
total_time += new_time-base_time
end + total_time/times.length).strftime("%I:%M%p")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment