Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/java-buildpack-utils/format_duration.rb

Overview

A mixin that adds the ability to format a Numeric as a user-readable duration

Instance Method Summary collapse

Instance Method Details

#durationString

Formats a number as a user-readable duration

Returns:

  • (String)

    a user-readable duration. Follows the following algorithm

    1. If more than an hour, print hours and minutes

    2. If less than an hour and more than a minute, print minutes and seconds

    3. If less than a minute and more than a second, print seconds.milliseconds

    4. If less than a second, print milliseconds



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/java-buildpack-utils/format_duration.rb', line 26

def duration
  remainder = self

  hours = (remainder / HOUR).to_int
  remainder -= HOUR * hours

  minutes = (remainder / MINUTE).to_int
  remainder -= MINUTE * minutes

  if hours > 0
    return "#{hours}h #{minutes}m"
  end

  seconds = (remainder / SECOND).to_int
  remainder -= SECOND * seconds

  if minutes > 0
    return "#{minutes}m #{seconds}s"
  end

  tenths = (remainder / TENTH).to_int
  "#{seconds}.#{tenths}s"
end