secret
anonymous / honconn.rb
Last active

  • Download Gist
honconn.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.