Class: TimeTally::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/time_tally/duration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ Duration

Returns a new instance of Duration.



5
6
7
8
9
10
11
12
# File 'lib/time_tally/duration.rb', line 5

def initialize(str = nil)
  if str
    @h, @m, @s = str.split(":").map(&:to_i)
    normalize
  else
    @h = @m = @s = 0
  end
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



3
4
5
# File 'lib/time_tally/duration.rb', line 3

def h
  @h
end

#mObject

Returns the value of attribute m.



3
4
5
# File 'lib/time_tally/duration.rb', line 3

def m
  @m
end

#sObject

Returns the value of attribute s.



3
4
5
# File 'lib/time_tally/duration.rb', line 3

def s
  @s
end

Instance Method Details

#+(other) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/time_tally/duration.rb', line 26

def +(other)
  result = Duration.new
  result.s = s + other.s
  result.m = m + other.m
  result.h = h + other.h
  result.normalize
  result
end

#normalizeObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/time_tally/duration.rb', line 14

def normalize
  if @s > 59
    @m += @s / 60
    @s %= 60
  end

  if @m > 59
    @h += @m / 60
    @m %= 60
  end
end

#to_sObject



35
36
37
# File 'lib/time_tally/duration.rb', line 35

def to_s
  "%d:%02d:%02d" % [h, m, s]
end