class TimeSecond
Make it easy to handle numeric value as seconds.
Constants
- VERSION
Public Class Methods
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
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
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
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
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
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
Compares one TimeSecond
and another or a Numeric to this TimeSecond
.
# File lib/time_second.rb, line 81 def <=>(other) @time <=> other end
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
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
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
Return hour
@return [Integer] Hour (0 ~ )
# File lib/time_second.rb, line 44 def hour @time.to_i / 60 / 60 end
Return minute
@return [Integer] Minute (0 ~ 59)
# File lib/time_second.rb, line 51 def minute @time.to_i / 60 % 60 end
Return second
@return [Integer] TimeSecond
(0 ~ 59)
# File lib/time_second.rb, line 58 def second @time.to_i % 60 end
Returns float value of seconds
@return [Float] seconds
# File lib/time_second.rb, line 37 def to_f @time.to_f end
Returns integer value of seconds
@return [Integer] seconds
# File lib/time_second.rb, line 30 def to_i @time.to_int end