Sha256: eae2d2fae79d12b2101f2bad1d949615b82bf91391d984010890759d5435a91b

Contents?: true

Size: 864 Bytes

Versions: 6

Compression:

Stored size: 864 Bytes

Contents

module Montrose
  class TimeOfDay
    include Comparable

    attr_reader :parts, :hour, :min, :sec

    def self.parse(arg)
      return new(arg) if arg.is_a?(Array)

      from_time(::Montrose::Utils.as_time(arg))
    end

    def self.from_time(time)
      new(to_parts(time))
    end

    def self.to_parts(time)
      [time.hour, time.min, time.sec]
    end

    def initialize(parts)
      @parts = parts
      @hour, @min, @sec = *parts
    end

    def seconds_since_midnight
      @seconds_since_midnight ||= (@hour * 60 * 60) + (@min * 60) + @sec
    end

    def to_a
      @parts
    end

    # def inspect
    #   "#<Montrose::TimeOfDay #{format_time(@hour)}:#{format_time(@min)}:#{format_time(@sec)}"
    # end

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

    private

    def format_time(part)
      format("%02d", part)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
montrose-0.18.0 lib/montrose/time_of_day.rb
montrose-0.17.0 lib/montrose/time_of_day.rb
montrose-0.16.0 lib/montrose/time_of_day.rb
montrose-0.15.0 lib/montrose/time_of_day.rb
montrose-0.14.0 lib/montrose/time_of_day.rb
montrose-0.13.0 lib/montrose/time_of_day.rb