-
-
Save rwjblue/3458397 to your computer and use it in GitHub Desktop.
Calculate percentage of interval covered within a given hour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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