secret
anonymous / benmiller.rb
Created

  • Download Gist
benmiller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#! /usr/bin/env ruby -w
require 'time'
 
HALF_A_DAY = 12 * 60
A_WHOLE_DAY = 24 * 60
 
def average_time_of_day the_times
 
raise "You must pass me some values!" unless the_times && the_times.length > 0
 
the_times.collect! { |s| t = Time.parse(s); t.hour*60 + t.min }
 
min_time = the_times.min
the_times.collect! { |t| (t - min_time) > HALF_A_DAY ? t : t + A_WHOLE_DAY } unless the_times.max - the_times.min <= HALF_A_DAY
 
total = the_times.inject { |total,t| total + t }
avg = total / the_times.length
Time.at( avg * 60 ).gmtime.strftime('%I:%M%p').downcase
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.