class TimeSecond

Make it easy to handle numeric value as seconds.

Constants

VERSION

Public Class Methods

parse(str) click to toggle source

Parse 'HH:MM:SS' format string and return its object

@param [String] str 'HH:MM:SS' or 'HH:MM'

@return [TimeSecond]

# File lib/time_second.rb, line 14
def self.parse(str)
  unless str.match(/\A\d{1,2}:\d{2}(?:\:\d{2})?\z/)
    raise ArgumentError, 'Invalid string format'
  end

  h, m, s = str.split(':')
  new(h.to_i * 60 * 60 + m.to_i * 60 + s.to_i)
end

Public Instance Methods

%(other) click to toggle source

Returns the modulo of this by another TimeSecond or Numeric.

# File lib/time_second.rb, line 115
def %(other)
  r, l = @time.coerce(other)
  self.class.new(l % r)
end
*(other) click to toggle source

Multiple self by a Numeric and returns a new TimeSecond.

# File lib/time_second.rb, line 103
def *(other)
  r, l = @time.coerce(other)
  self.class.new(l * r)
end
+(other) click to toggle source

Adds another TimeSecond or Numeric to this TimeSecond.

# File lib/time_second.rb, line 91
def +(other)
  r, l = @time.coerce(other)
  self.class.new(l + r)
end
-(other) click to toggle source

Subtracts another TimeSecond or Numeric from this TimeSecond.

# File lib/time_second.rb, line 97
def -(other)
  r, l = @time.coerce(other)
  self.class.new(l - r)
end
/(other) click to toggle source

Divides self by a Numeric and returns a new TimeSecond.

# File lib/time_second.rb, line 109
def /(other)
  r, l = @time.coerce(other)
  self.class.new(l / r)
end
<=>(other) click to toggle source

Compares one TimeSecond and another or a Numeric to this TimeSecond.

# File lib/time_second.rb, line 81
def <=>(other)
  @time <=> other
end
==(other) click to toggle source

Returns true if other is with the same time.

# File lib/time_second.rb, line 86
def ==(other)
  @time.to_f == other.to_f
end
hm(sep = ':') click to toggle source

Return 'HH:MM' format string

@param [String] sep Seperator string. Default is ':'.

@return [String] 'HH:MM'

# File lib/time_second.rb, line 76
def hm(sep = ':')
  "%02d#{sep}%02d" % [hour, minute]
end
hms(sep = ':') click to toggle source

Return 'HH:MM:SS' format string

@param [String] sep Seperator string. Default is ':'.

@return [String] 'HH:MM:SS'

# File lib/time_second.rb, line 67
def hms(sep = ':')
  "%02d#{sep}%02d#{sep}%02d" % [hour, minute, second]
end
hour() click to toggle source

Return hour

@return [Integer] Hour (0 ~ )

# File lib/time_second.rb, line 44
def hour
  @time.to_i / 60 / 60
end
minute() click to toggle source

Return minute

@return [Integer] Minute (0 ~ 59)

# File lib/time_second.rb, line 51
def minute
  @time.to_i / 60 % 60
end
second() click to toggle source

Return second

@return [Integer] TimeSecond (0 ~ 59)

# File lib/time_second.rb, line 58
def second
  @time.to_i % 60
end
to_f() click to toggle source

Returns float value of seconds

@return [Float] seconds

# File lib/time_second.rb, line 37
def to_f
  @time.to_f
end
to_i() click to toggle source

Returns integer value of seconds

@return [Integer] seconds

# File lib/time_second.rb, line 30
def to_i
  @time.to_int
end