Sha256: 9268cd31ca6d978e0cfbc60d5861977c770206d6df7ba03c0b2f4c700e0436df

Contents?: true

Size: 962 Bytes

Versions: 11

Compression:

Stored size: 962 Bytes

Contents

module Crono
  # Interval describes a period between two specific times of day
  class Interval
    attr_accessor :from, :to

    def self.parse(value)
      from_to =
        case value
        when Array  then value
        when Hash   then value.values_at(:from, :to)
        when String then value.split('-')
        else
          fail "Unknown interval format: #{value.inspect}"
        end
      from, to = from_to.map { |v| TimeOfDay.parse(v) }
      new from, to
    end

    def initialize(from, to)
      @from, @to = from, to
    end

    def within?(value)
      tod = ((value.is_a? TimeOfDay) ? value : TimeOfDay.parse(value))
      if @from <= @to
        tod >= @from && tod < @to
      else
        tod >= @from || tod < @to
      end
    end

    def next_within(time, period)
      begin
        time = period.since(time)
      end until within? TimeOfDay.parse(time)
      time
    end

    def to_s
      "#{@from}-#{@to}"
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
crono-2.0.1 lib/crono/interval.rb
crono-2.0.0 lib/crono/interval.rb
crono-1.1.2 lib/crono/interval.rb
crono-1.1.1 lib/crono/interval.rb
crono-1.1.0 lib/crono/interval.rb
crono-1.0.3 lib/crono/interval.rb
crono-1.0.2 lib/crono/interval.rb
crono-1.0.1 lib/crono/interval.rb
crono-1.0.0 lib/crono/interval.rb
crono-1.0.0.pre3 lib/crono/interval.rb
crono-1.0.0.pre2 lib/crono/interval.rb