summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2024-04-21 10:33:15 +0200
committerMergify <37929162+mergify[bot]@users.noreply.github.com>2024-04-22 16:15:28 +0000
commit2ccc0e105223e2cb5558c52baba8ddf356bd34a8 (patch)
treea9c92368c9adf3fce783c5013e0123a8d3c26bc8 /python
parent428d793ef162cc3e61af0fbc89963898e6b3988d (diff)
downloadvyos-1x-2ccc0e105223e2cb5558c52baba8ddf356bd34a8.tar.gz
vyos-1x-2ccc0e105223e2cb5558c52baba8ddf356bd34a8.zip
vyos.utils: T6244: use list to build up result string
When handling optional separators rather build up a list and join the list with the requested delimiter to form the resulting human readable time string. (cherry picked from commit 6e9cd8821ca028b5bc05c14b0b4e3454036da6da)
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/convert.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/python/vyos/utils/convert.py b/python/vyos/utils/convert.py
index c02f0071e..da207b9fb 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
@@ -23,34 +23,34 @@ def seconds_to_human(s, separator=""):
day = 60 * 60 * 24
hour = 60 * 60
- remainder = 0
- result = ""
+ result = []
+
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):