Module: Bosh::Director::Duration

Defined in:
lib/bosh/director/duration.rb

Class Method Summary collapse

Class Method Details

.duration(delta) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bosh/director/duration.rb', line 6

def duration(delta)
  seconds = delta % 60
  delta = (delta / 60).floor
  minutes = delta % 60
  delta = (delta / 60).floor
  hours = delta % 24
  delta = (delta / 24).floor
  days = delta

  result = []

  duration_helper(days, result, "day")
  duration_helper(hours, result, "hour")
  duration_helper(minutes, result, "minute")
  duration_helper(seconds, result, "second")

  result << "0 seconds" if result.empty?

  result.join(" ")
end

.duration_helper(value, result, unit) ⇒ Object



27
28
29
30
31
# File 'lib/bosh/director/duration.rb', line 27

def duration_helper(value, result, unit)
  if value > 0
    result << "#{value} #{value == 1 ? unit : "#{unit}s"}"
  end
end