Class: TimeDuration::Duration
- Inherits:
-
Object
- Object
- TimeDuration::Duration
- Includes:
- Comparable
- Defined in:
- lib/time_duration.rb
Instance Attribute Summary collapse
-
#second ⇒ Object
Returns the value of attribute second.
Class Method Summary collapse
-
.parse(time_as_string, format: '%H:%M') ⇒ Object
TODO: format指定できるようにする.
Instance Method Summary collapse
- #+(time_duration) ⇒ Object
- #-(time_duration) ⇒ Object
- #<=>(time_duration) ⇒ Object
- #hour ⇒ Object
-
#initialize(hour: 0, minute: 0, second: 0) ⇒ Duration
constructor
A new instance of Duration.
-
#inspect ⇒ Object
override.
- #minute ⇒ Object
-
#to_s ⇒ Object
TODO: format指定できるようにする.
Constructor Details
#initialize(hour: 0, minute: 0, second: 0) ⇒ Duration
Returns a new instance of Duration.
19 20 21 22 23 24 |
# File 'lib/time_duration.rb', line 19 def initialize(hour: 0, minute: 0, second: 0) hour = hour.to_i minute = minute.to_i second = second.to_i @second = hour * 3600 + minute * 60 + second end |
Instance Attribute Details
#second ⇒ Object
Returns the value of attribute second.
11 12 13 |
# File 'lib/time_duration.rb', line 11 def second @second end |
Class Method Details
.parse(time_as_string, format: '%H:%M') ⇒ Object
TODO: format指定できるようにする
14 15 16 17 |
# File 'lib/time_duration.rb', line 14 def self.parse(time_as_string, format: '%H:%M') hour, minute = time_as_string.split(':').map(&:to_i) new(hour: hour, minute: minute) end |
Instance Method Details
#+(time_duration) ⇒ Object
39 40 41 |
# File 'lib/time_duration.rb', line 39 def +(time_duration) self.class.new(second: second + time_duration.second) end |
#-(time_duration) ⇒ Object
43 44 45 |
# File 'lib/time_duration.rb', line 43 def -(time_duration) self.class.new(second: second - time_duration.second) end |
#<=>(time_duration) ⇒ Object
47 48 49 |
# File 'lib/time_duration.rb', line 47 def <=>(time_duration) self.second <=> time_duration.second end |
#hour ⇒ Object
26 27 28 |
# File 'lib/time_duration.rb', line 26 def hour minute / 60 + second / 3600 end |
#inspect ⇒ Object
override
52 53 54 |
# File 'lib/time_duration.rb', line 52 def inspect to_s end |
#minute ⇒ Object
30 31 32 |
# File 'lib/time_duration.rb', line 30 def minute (second / 60) % 60 end |
#to_s ⇒ Object
TODO: format指定できるようにする
35 36 37 |
# File 'lib/time_duration.rb', line 35 def to_s "%d:%02d" % [hour, minute] end |