Method: Rex::ExtTime.sec_to_s

Defined in:
lib/rex/time.rb

.sec_to_s(seconds) ⇒ Object

Convert seconds to a string that is broken down into years, days, hours, minutes, and second.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rex/time.rb', line 15

def self.sec_to_s(seconds)
  parts = [ 31536000, 86400, 3600, 60, 1 ].map { |d|
    if ((c = seconds / d) > 0)
      seconds -= c.truncate * d
      c.truncate
    else
      0
    end
  }.reverse

  str = ''

  [ "sec", "min", "hour", "day", "year" ].each_with_index { |name, idx|
    next if (!parts[idx] or parts[idx] == 0)

    str = "#{parts[idx]} #{name + ((parts[idx] != 1) ? 's' :'')} " + str
  }

  str.empty? ? "0 secs" : str.strip
end