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.

Revisions

  1. matugm created this gist Jan 12, 2018.
    22 changes: 22 additions & 0 deletions time-range.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@

    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]
    # }