public
Last active

  • Download Gist
arrival_calculator.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
require 'time'
 
module ArrivalCalculator
TWELVE_HOURS = 12 * 60 * 60
TWENTY_FOUR_HOURS = 24 * 60 * 60
 
# Average time of day expects array such as ['11:00am', '1:00pm'] => '12:00pm'
def average_time_of_day(times)
times_in_seconds = times.map do | time |
raise ArgumentError, "Unexpected format " + time unless time.strip =~ /^[01]?\d:\d\d(am|pm)$/
Time.parse(time + " UTC", Time.at(0).utc).to_i # "12:00am" => 0 seconds
end
avg = average(coerce_split_day_times(times_in_seconds))
Time.at(avg).utc.strftime('%l:%M%p').downcase.strip
end
 
# Coerce AM times to next day for split day arrivals
def coerce_split_day_times(times_in_seconds)
am_times, pm_times = [], []
times_in_seconds.each { | time | ((time < TWELVE_HOURS)? am_times : pm_times) << time }
if am_times.any? && pm_times.any? && average(pm_times) - average(am_times) > TWELVE_HOURS
am_times.map! { | time | time + TWENTY_FOUR_HOURS }
end
am_times + pm_times
end
 
# Returns the arithmetic mean of the contents of the array
def average(numbers)
numbers.inject(0.0) { | sum, seconds | sum + seconds } / numbers.size
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.