summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2024-04-22 19:01:20 +0200
committerGitHub <noreply@github.com>2024-04-22 19:01:20 +0200
commit626321ad1c3f813040897a7d1d7810af18c24800 (patch)
tree49f78a4d60ab51409a5c34913f0e0e2d47c6e88c /python
parent428d793ef162cc3e61af0fbc89963898e6b3988d (diff)
parent8747c300e521ce252c0f26fab104f97c89d4c292 (diff)
downloadvyos-1x-626321ad1c3f813040897a7d1d7810af18c24800.tar.gz
vyos-1x-626321ad1c3f813040897a7d1d7810af18c24800.zip
Merge pull request #3343 from vyos/mergify/bp/sagitta/pr-3336
vyos.utils: T6244: add whitespace after time unit in uptime (backport #3336)
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/convert.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/python/vyos/utils/convert.py b/python/vyos/utils/convert.py
index c02f0071e..41e65081f 100644
--- a/python/vyos/utils/convert.py
+++ b/python/vyos/utils/convert.py
@@ -1,4 +1,4 @@
-# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 2023-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -19,38 +19,43 @@ def seconds_to_human(s, separator=""):
"""
s = int(s)
+ year = 60 * 60 * 24 * 365.25
week = 60 * 60 * 24 * 7
day = 60 * 60 * 24
hour = 60 * 60
- remainder = 0
- result = ""
+ result = []
+
+ years = s // year
+ if years > 0:
+ result.append(f'{int(years)}y')
+ s = int(s % year)
weeks = s // week
if weeks > 0:
- result = "{0}w".format(weeks)
+ result.append(f'{weeks}w')
s = s % week
days = s // day
if days > 0:
- result = "{0}{1}{2}d".format(result, separator, days)
+ result.append(f'{days}d')
s = s % day
hours = s // hour
if hours > 0:
- result = "{0}{1}{2}h".format(result, separator, hours)
+ result.append(f'{hours}h')
s = s % hour
minutes = s // 60
if minutes > 0:
- result = "{0}{1}{2}m".format(result, separator, minutes)
+ result.append(f'{minutes}m')
s = s % 60
seconds = s
if seconds > 0:
- result = "{0}{1}{2}s".format(result, separator, seconds)
+ result.append(f'{seconds}s')
- return result
+ return separator.join(result)
def bytes_to_human(bytes, initial_exponent=0, precision=2,
int_below_exponent=0):