Skip to content

Instantly share code, notes, and snippets.

@rhyhann
Created October 8, 2009 13:05
Show Gist options
  • Save rhyhann/204998 to your computer and use it in GitHub Desktop.
Save rhyhann/204998 to your computer and use it in GitHub Desktop.
def average_time_of_day(times)
times.map! {|time| time.scan(/(\d+):(\d{2})(\w{2})/)}.
map! {|l| l[0].first(2).map(&:to_i) << (l[0].last == 'pm')}
seconds = 0
times.each { |t| seconds += t[0]*3600 + (12 if t[2]).to_i + t[1]*60 }
average = seconds/times.size
hours = average/3600
hours, minutes, ampm = hours-(12 if hours>12).to_i, (average- hours*3600)/60,
hours>12 ? 'pm' : 'am'
"#{hours}:#{minutes}#{ampm}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment