summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-07-20 05:50:16 +0200
committerDaniil Baturin <daniil@baturin.org>2018-07-20 05:50:16 +0200
commit2a7f374f8a3ddf4a31ab88087912f19889c75a94 (patch)
treea0de44d28ba16960bd74360787de0e0e5bf94aff /python
parent145550408bbe53f5bfb8b0211bacc5129d7b3117 (diff)
downloadvyos-1x-2a7f374f8a3ddf4a31ab88087912f19889c75a94.tar.gz
vyos-1x-2a7f374f8a3ddf4a31ab88087912f19889c75a94.zip
Add a function for converting seconds to a human readable elapsed time descriptions such as 1w3d18h42m12s.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/util.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 11a75c14b..531cd8182 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -70,3 +70,43 @@ def process_running(pid_file):
with open(pid_file, 'r') as f:
pid = f.read().strip()
return psutil.pid_exists(int(pid))
+
+def seconds_to_human(s, separator=""):
+ """ Converts number of seconds passed to a human-readable
+ interval such as 1w4d18h35m59s
+ """
+ s = int(s)
+
+ week = 60 * 60 * 24 * 7
+ day = 60 * 60 * 24
+ hour = 60 * 60
+
+ remainder = 0
+ result = ""
+
+ weeks = s // week
+ if weeks > 0:
+ result = "{0}w".format(weeks)
+ s = s % week
+
+ days = s // day
+ if days > 0:
+ result = "{0}{1}{2}d".format(result, separator, days)
+ s = s % day
+
+ hours = s // hour
+ if hours > 0:
+ result = "{0}{1}{2}h".format(result, separator, hours)
+ s = s % hour
+
+ minutes = s // 60
+ if minutes > 0:
+ result = "{0}{1}{2}m".format(result, separator, minutes)
+ s = s % 60
+ print(s)
+
+ seconds = s
+ if seconds > 0:
+ result = "{0}{1}{2}s".format(result, separator, seconds)
+
+ return result