Class: TimeSecond

Inherits:
Numeric
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • str (String)

    ‘HH:MM:SS’ or ‘HH:MM’

Returns:



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

Parameters:

  • sep (String) (defaults to: ':')

    Seperator string. Default is ‘:’.

Returns:

  • (String)

    ‘HH:MM’



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

Parameters:

  • sep (String) (defaults to: ':')

    Seperator string. Default is ‘:’.

Returns:

  • (String)

    ‘HH:MM:SS’



67
68
69
# File 'lib/time_second.rb', line 67

def hms(sep = ':')
  "%02d#{sep}%02d#{sep}%02d" % [hour, minute, second]
end

#hourInteger

Return hour

Returns:

  • (Integer)

    Hour (0 ~ )



44
45
46
# File 'lib/time_second.rb', line 44

def hour
  @time.to_i / 60 / 60
end

#minuteInteger

Return minute

Returns:

  • (Integer)

    Minute (0 ~ 59)



51
52
53
# File 'lib/time_second.rb', line 51

def minute
  @time.to_i / 60 % 60
end

#secondInteger

Return second

Returns:

  • (Integer)

    TimeSecond (0 ~ 59)



58
59
60
# File 'lib/time_second.rb', line 58

def second
  @time.to_i % 60
end

#to_fFloat

Returns float value of seconds

Returns:

  • (Float)

    seconds



37
38
39
# File 'lib/time_second.rb', line 37

def to_f
  @time.to_f
end

#to_iInteger

Returns integer value of seconds

Returns:

  • (Integer)

    seconds



30
31
32
# File 'lib/time_second.rb', line 30

def to_i
  @time.to_int
end