-
-
Save anonymous/32751c9971386f6c7d8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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