-
-
Save anonymous/b7165b186f6b02464463 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
MIN = 60 | |
HOUR = MIN*60 | |
def average_time_of_day(args) | |
times = [] | |
r = /(\d+?):(\d+?)(am|pm)/ | |
args.each do |e| | |
times << r.match(e).to_a[1..-1] | |
end | |
mixed = !(times.all?{|h,m,meridiam| meridiam =~ /am/} || times.all?{|h,m,meridiam| meridiam =~ /pm/}) | |
full = mixed ? HOUR*24 : 0 | |
total = times.map! do |h,m,meridiam| | |
(meridiam =~ /am/ ? (h =~/12/ ? 0 + m.to_i*MIN : h.to_i*HOUR + m.to_i*MIN) : ((h.to_i*HOUR+HOUR*12 + m.to_i*MIN) - full)) | |
end.inject(0) do |sum,t| | |
sum + t | |
end | |
average = total/args.size return "#{average/HOUR == 0 ? 12 : average/HOUR}:"+"%02d" % "#{(average % HOUR)/MIN}"+(average <= HOUR*12 ? "am":"pm") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment