Skip to content

Instantly share code, notes, and snippets.

/benmiller.rb Secret

Created October 17, 2009 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/32751c9971386f6c7d8f to your computer and use it in GitHub Desktop.
Save anonymous/32751c9971386f6c7d8f to your computer and use it in GitHub Desktop.
#! /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