Class: TimeSecond
- Inherits:
-
Numeric
- Object
- Numeric
- TimeSecond
- Includes:
- Comparable
- Defined in:
- lib/time_second.rb
Overview
Make it easy to handle numeric value as seconds.
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
-
.parse(str) ⇒ TimeSecond
Parse ‘HH:MM:SS’ format string and return its object.
Instance Method Summary collapse
-
#%(other) ⇒ Object
Returns the modulo of this by another TimeSecond or Numeric.
-
#*(other) ⇒ Object
Multiple self by a Numeric and returns a new TimeSecond.
-
#+(other) ⇒ Object
Adds another TimeSecond or Numeric to this TimeSecond.
-
#-(other) ⇒ Object
Subtracts another TimeSecond or Numeric from this TimeSecond.
-
#/(other) ⇒ Object
Divides self by a Numeric and returns a new TimeSecond.
-
#<=>(other) ⇒ Object
Compares one TimeSecond and another or a Numeric to this TimeSecond.
-
#==(other) ⇒ Object
Returns true if other is with the same time.
-
#hm(sep = ':') ⇒ String
Return ‘HH:MM’ format string.
-
#hms(sep = ':') ⇒ String
Return ‘HH:MM:SS’ format string.
-
#hour ⇒ Integer
Return hour.
-
#initialize(time) ⇒ TimeSecond
constructor
:nodoc:.
-
#minute ⇒ Integer
Return minute.
-
#second ⇒ Integer
Return second.
-
#to_f ⇒ Float
Returns float value of seconds.
-
#to_i ⇒ Integer
Returns integer value of seconds.
Constructor Details
#initialize(time) ⇒ TimeSecond
:nodoc:
23 24 25 |
# File 'lib/time_second.rb', line 23 def initialize(time) # :nodoc: @time = time end |
Class Method Details
.parse(str) ⇒ TimeSecond
Parse ‘HH:MM:SS’ format string and return its object
14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#%(other) ⇒ Object
Returns the modulo of this by another TimeSecond or Numeric.
115 116 117 118 |
# File 'lib/time_second.rb', line 115 def %(other) r, l = @time.coerce(other) self.class.new(l % r) end |
#*(other) ⇒ Object
Multiple self by a Numeric and returns a new TimeSecond.
103 104 105 106 |
# File 'lib/time_second.rb', line 103 def *(other) r, l = @time.coerce(other) self.class.new(l * r) end |
#+(other) ⇒ Object
Adds another TimeSecond or Numeric to this TimeSecond.
91 92 93 94 |
# File 'lib/time_second.rb', line 91 def +(other) r, l = @time.coerce(other) self.class.new(l + r) end |
#-(other) ⇒ Object
Subtracts another TimeSecond or Numeric from this TimeSecond.
97 98 99 100 |
# File 'lib/time_second.rb', line 97 def -(other) r, l = @time.coerce(other) self.class.new(l - r) end |
#/(other) ⇒ Object
Divides self by a Numeric and returns a new TimeSecond.
109 110 111 112 |
# File 'lib/time_second.rb', line 109 def /(other) r, l = @time.coerce(other) self.class.new(l / r) end |
#<=>(other) ⇒ Object
Compares one TimeSecond and another or a Numeric to this TimeSecond.
81 82 83 |
# File 'lib/time_second.rb', line 81 def <=>(other) @time <=> other end |
#==(other) ⇒ Object
Returns true if other is with the same time.
86 87 88 |
# File 'lib/time_second.rb', line 86 def ==(other) @time.to_f == other.to_f end |
#hm(sep = ':') ⇒ String
Return ‘HH:MM’ format string
76 77 78 |
# File 'lib/time_second.rb', line 76 def hm(sep = ':') "%02d#{sep}%02d" % [hour, minute] end |
#hms(sep = ':') ⇒ String
Return ‘HH:MM:SS’ format string
67 68 69 |
# File 'lib/time_second.rb', line 67 def hms(sep = ':') "%02d#{sep}%02d#{sep}%02d" % [hour, minute, second] end |
#hour ⇒ Integer
Return hour
44 45 46 |
# File 'lib/time_second.rb', line 44 def hour @time.to_i / 60 / 60 end |
#minute ⇒ Integer
Return minute
51 52 53 |
# File 'lib/time_second.rb', line 51 def minute @time.to_i / 60 % 60 end |
#second ⇒ Integer
Return second
58 59 60 |
# File 'lib/time_second.rb', line 58 def second @time.to_i % 60 end |
#to_f ⇒ Float
Returns float value of seconds
37 38 39 |
# File 'lib/time_second.rb', line 37 def to_f @time.to_f end |
#to_i ⇒ Integer
Returns integer value of seconds
30 31 32 |
# File 'lib/time_second.rb', line 30 def to_i @time.to_int end |