Skip to content

anonymous /honconn.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.