2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-22 03:32:06 +00:00
|
|
|
module Rex
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Extended time related functions.
|
|
|
|
#
|
|
|
|
###
|
2005-07-22 03:32:06 +00:00
|
|
|
module ExtTime
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Convert seconds to a string that is broken down into years, days, hours,
|
|
|
|
# minutes, and second.
|
|
|
|
#
|
|
|
|
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
|
2005-07-22 03:32:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
str = ''
|
2005-07-22 03:32:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
[ "sec", "min", "hour", "day", "year" ].each_with_index { |name, idx|
|
|
|
|
next if (!parts[idx] or parts[idx] == 0)
|
2005-07-22 03:32:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
str = "#{parts[idx]} #{name + ((parts[idx] != 1) ? 's' :'')} " + str
|
|
|
|
}
|
2005-07-22 03:32:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
str.empty? ? "0 secs" : str.strip
|
|
|
|
end
|
2005-07-22 03:32:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Converts a string in the form n years g days x hours y mins z secs.
|
|
|
|
#
|
|
|
|
def self.str_to_sec(str)
|
|
|
|
fields = str.split(/ /)
|
|
|
|
secs = 0
|
2006-01-03 04:07:20 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
fields.each_with_index { |f, idx|
|
|
|
|
val = 0
|
|
|
|
case f
|
|
|
|
when /^year/
|
|
|
|
val = 31536000
|
|
|
|
when /^day/
|
|
|
|
val = 86400
|
|
|
|
when /^hour/
|
|
|
|
val = 3600
|
|
|
|
when /^min/
|
|
|
|
val = 60
|
|
|
|
when /^sec/
|
|
|
|
val = 1
|
|
|
|
end
|
2006-01-03 04:07:20 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
secs += val * fields[idx-1].to_i
|
|
|
|
}
|
2006-01-03 04:07:20 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
secs
|
|
|
|
end
|
2006-01-03 04:07:20 +00:00
|
|
|
|
2005-07-22 03:32:06 +00:00
|
|
|
end
|
|
|
|
|
2008-11-08 22:35:37 +00:00
|
|
|
end
|