Created
January 12, 2018 13:53
-
-
Save matugm/9683a5dd81bb955b3be3dbfb6f20ef18 to your computer and use it in GitHub Desktop.
Time Range
This file contains 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
require 'time' | |
require 'pp' | |
objects = [Time.strptime("20", "%M"), Time.strptime("21", "%M"), Time.strptime("25", "%M"),Time.strptime("26", "%M")] | |
@range = Hash.new { |hash, key| hash[key] = [] } | |
def add_into_time_range(time) | |
base = time.min - (time.min % 5) | |
key = "#{time.hour}:#{base}" | |
@range[key] << time | |
end | |
objects.each { |t| add_into_time_range(t) } | |
pp @range | |
# { | |
# "14:20"=>[2018-01-12 14:20:00 +0100, 2018-01-12 14:21:00 +0100], | |
# "14:25"=>[2018-01-12 14:25:00 +0100, 2018-01-12 14:26:00 +0100] | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment