Skip to content

Instantly share code, notes, and snippets.

@flippingbits
Created October 9, 2009 08:33
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 flippingbits/71001d595386112ed45c to your computer and use it in GitHub Desktop.
Save flippingbits/71001d595386112ed45c to your computer and use it in GitHub Desktop.
require 'time'
# define the regular arrival time, that is used to detect a cycle
REGULAR_ARRIVAL_TIME = "10:00pm"
# maximum delay can be 23 hours 59 minutes (because of cycling times)
def average_time_of_day(arrivals)
# check for a cycle and convert arrival times into UNIX timestamps
arrivals.collect!{ |a|
a = Time.parse(a)
a += 60*60*24 if a < Time.parse(REGULAR_ARRIVAL_TIME)
a.to_i
}
# get the average timestamp
sum = arrivals.inject{ |sum,x| sum + x }
average_time = sum/arrivals.length
# convert the timestamp back and return it
average_time = Time.at(average_time)
average_time.strftime("%I:%M%p")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment