CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ext_time.rb
Views: 11766
1
# -*- coding: binary -*-
2
module Rex
3
###
4
#
5
# Extended time related functions.
6
#
7
###
8
module ExtTime
9
#
10
# Convert seconds to a string that is broken down into years, days, hours,
11
# minutes, and second.
12
#
13
def self.sec_to_s(seconds)
14
return "0 secs" if seconds.to_i <= 0
15
[[31536000, 'year'], [86400, 'day'], [3600, 'hour'], [60, 'min'], [1, 'sec']].map! { |count, name|
16
if (c = seconds / count) > 0
17
c = c.truncate
18
seconds -= c * count
19
if c == 1
20
"#{c} #{name}"
21
elsif c > 1
22
"#{c} #{name}s"
23
end
24
end
25
}.compact.join(' ')
26
end
27
end
28
end
29
30