Sha256: b137c896b00a7646c2618f88f9cfcb6c9e52264cba043d5699f5faca39523a7d

Contents?: true

Size: 699 Bytes

Versions: 9

Compression:

Stored size: 699 Bytes

Contents

module Crono
  # TimeOfDay describes a certain hour and minute (on any day)
  class TimeOfDay
    include Comparable

    attr_accessor :hour, :min

    def self.parse(value)
      time =
        case value
        when String then Time.parse(value).utc
        when Hash   then Time.now.change(value).utc
        when Time   then value.utc
        else
          fail "Unknown TimeOfDay format: #{value.inspect}"
        end
      new time.hour, time.min
    end

    def initialize(hour, min)
      @hour, @min = hour, min
    end

    def to_i
      @hour * 60 + @min
    end

    def to_s
      '%02d:%02d' % [@hour, @min]
    end

    def <=>(other)
      to_i <=> other.to_i
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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