Create a gist now

Instantly share code, notes, and snippets.

@conner /connerpeirce.rb Secret
Created Oct 18, 2009

# @author = Conner Peirce
require 'Time'
# assumes that times are within 13 hours of each other
# uses naive compare between current time and last time
def average_time_of_day( times, hourDiff = 13 )
day = 60*60*24 # in seconds
sum = 0
prevt= Time.parse(times[0]).to_f
for t in times do
parsedt = Time.parse(t).to_f
# if the new time is more than hourDiff from the last time
# then we assume that the day is off and add or subtract a day
if (parsedt-prevt).abs > (60*60*hourDiff)
if parsedt > prevt then nt = parsedt - day
else nt = parsedt + day
else nt = parsedt
sum+= nt
prevt = parsedt
p 'average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])'
average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
p 'average_time_of_day(["6:41am", "6:51am", "7:01am"])'
average_time_of_day(["6:41am", "6:51am", "7:01am"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment