CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ext_time.rb
Views: 1904
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