Sha256: 15a1ea86f1b5ed71d098cf5153f94b70dd4118524635c49ec1141eb4c7930f72

Contents?: true

Size: 709 Bytes

Versions: 3

Compression:

Stored size: 709 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.zone.parse(value).utc
        when Hash   then Time.zone.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

3 entries across 3 versions & 1 rubygems

Version Path
crono-2.1.0 lib/crono/time_of_day.rb
crono-2.0.1 lib/crono/time_of_day.rb
crono-2.0.0 lib/crono/time_of_day.rb