-
-
Save chuckha/2ca8054d76cfddd58795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment