secret
Last active

  • Download Gist
avg_time_function.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
Time.at(base_time + total_time/times.length).strftime("%I:%M%p")
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.