Last active
February 8, 2019 10:27
-
-
Save TRex22/4ed5b18d883e41d8c6be1dc3a5a0a924 to your computer and use it in GitHub Desktop.
Chunk Time in ruby
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
TIME_CHUNK_INTERVAL = 10.hours | |
# [[start,end], [start,end]...] | |
def fetch_time_chunks(start_time, end_time, time_interval = nil) | |
interval = time_interval || TIME_CHUNK_INTERVAL | |
time_delta = end_time - start_time | |
number_of_chunks = (time_delta / interval).to_i | |
return [[start_time, end_time]] if number_of_chunks == 0 | |
chunks = (number_of_chunks.times).map.with_index { |chunk, idex| | |
interval_start_time = (start_time.to_i + (interval.to_i * (idex))) | |
interval_end_time = interval_start_time + interval | |
[interval_start_time, interval_end_time] | |
} | |
chunks[-1] = [chunks.last.first, end_time] | |
chunks.map { |chunk| | |
[Time.at(chunk.first), Time.at(chunk.last)] | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment