Skip to content

Instantly share code, notes, and snippets.

@matugm
Created January 12, 2018 13:53
Show Gist options
  • Save matugm/9683a5dd81bb955b3be3dbfb6f20ef18 to your computer and use it in GitHub Desktop.
Save matugm/9683a5dd81bb955b3be3dbfb6f20ef18 to your computer and use it in GitHub Desktop.
Time Range
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