Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created August 24, 2012 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rondale-sc/3455726 to your computer and use it in GitHub Desktop.
Save rondale-sc/3455726 to your computer and use it in GitHub Desktop.
Calculate percentage of interval covered within a given hour.
def hour_intervals(start_time, stop_time)
results = Hash.new {|h,k| h[k] = Hash.new(0)}
(start_time.to_i..stop_time.to_i).step(60).collect do |minute|
minute_time = Time.at(minute)
interval = case minute_time.min
when (0..14) then 0
when (15..29) then 15
when (30..44) then 30
when (45..59) then 45
end
results[minute_time.hour][interval] += 1
end
results
end
s = Service.last
start_time = Time.strptime("#{s.date_of_service.to_s} #{s.start_time.to_s.rjust(4,'0')}", "%Y-%m-%d %H%M")
end_time = Time.strptime("#{s.date_of_service.to_s} #{s.end_time.to_s.rjust(4,'0')}", "%Y-%m-%d %H%M")
m = hour_intervals(start_time, end_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment