class Minutely::Time

A class that represents a day time by using only hours and minutes.

@!attribute [r] hour

@return [Integer]

@!attribute [r] minute

@return [Integer]

Attributes

hour[R]
min[R]
minute[R]

Public Class Methods

beginning_of_day() click to toggle source

Returns the begining of day.

@return [Minutely::Time]

# File lib/minutely/time.rb, line 41
def self.beginning_of_day
  new(0, 0)
end
end_of_day() click to toggle source

Returns the end of day.

@return [Minutely::Time]

# File lib/minutely/time.rb, line 49
def self.end_of_day
  new(23, 59)
end
new(hour, minute) click to toggle source

Builds a new `Minutely::Time`.

@param hour [Integer] a number between 0 and 23

@param minute [Integer] a number between 0 and 59

@raise [ArgumentError] when arguments are not within the specified ranges

# File lib/minutely/time.rb, line 29
def initialize(hour, minute)
  raise ArgumentError, 'invalid hour' unless (0..24).include?(hour)
  raise ArgumentError, 'invalid minute' unless (0..59).include?(minute)

  @hour = hour == 24 ? 0 : hour
  @minute = minute
end
parse(value) click to toggle source

Parses the given input and returns a `Minutely::Time` or `nil`, respectively.

@param value [Minutely::Time, hour, min, Integer, String, nil]

@return [Minutely::Time, nil]

@raise [ArgumentError] when the object does not represent a valid time

# File lib/minutely/time.rb, line 62
def self.parse(value)
  Time::Parser.parse(value)
end

Public Instance Methods

<=>(other) click to toggle source

Compares the time to another one.

@return [Integer]

# File lib/minutely/time.rb, line 70
def <=>(other)
  return nil unless other.is_a?(self.class)

  [hour, minute] <=> [other.hour, other.minute]
end
succ() click to toggle source

Gets the next minute as new `Minutely::Time`.

@return [Minutely::Time]

# File lib/minutely/time.rb, line 80
def succ
  return self.class.new((hour + 1) % 24, 0) if minute == 59

  self.class.new(hour, minute + 1)
end
to_i() click to toggle source

Converts the time to an integer representation of the value.

@return [Integer]

# File lib/minutely/time.rb, line 90
def to_i
  100 * hour + minute
end
to_s() click to toggle source

Converts the time to an string representation of the value.

@return [String]

# File lib/minutely/time.rb, line 98
def to_s
  [hour, minute].map { |v| v.to_s.rjust(2, '0') }.join(':')
end