Skip to content

anonymous /benmiller.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#! /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
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.