summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2024-04-21 10:34:43 +0200
committerMergify <37929162+mergify[bot]@users.noreply.github.com>2024-04-22 16:15:28 +0000
commit94780c9b5a684c4e0c0b649d6e07081b0a52274e (patch)
tree4981cbca30539a6e0eac66586c4bc2a0dac13c51 /python
parent2ccc0e105223e2cb5558c52baba8ddf356bd34a8 (diff)
downloadvyos-1x-94780c9b5a684c4e0c0b649d6e07081b0a52274e.tar.gz
vyos-1x-94780c9b5a684c4e0c0b649d6e07081b0a52274e.zip
vyos.utils: T6244: add support for year timebase in seconds_to_human()
We only supported calculating seconds to weeks but not seconds to years. This has been added. Testcase: from vyos.utils.convert import seconds_to_human minute = 60 hour = minute * 60 day = hour * 24 week = day * 7 year = day * 365.25 for separator in ['', ' ', '-', '/']: print(f'----- Using separator "{separator}" -----') print(seconds_to_human(10, separator)) print(seconds_to_human(5* minute, separator)) print(seconds_to_human(3* hour, separator)) print(seconds_to_human(4* day, separator)) print(seconds_to_human(7 * week, separator)) print(seconds_to_human(10 * year, separator)) print(seconds_to_human(5*year + 4*week + 3*day + 2*hour + minute + 5, separator)) print() cpo@LR1.wue3:~$ ./foo.py ----- Using separator "" ----- 10s 5m 3h 4d 7w 10y 5y4w3d2h1m5s ----- Using separator " " ----- 10s 5m 3h 4d 7w 10y 5y 4w 3d 2h 1m 5s ----- Using separator "-" ----- 10s 5m 3h 4d 7w 10y 5y-4w-3d-2h-1m-5s ----- Using separator "/" ----- 10s 5m 3h 4d 7w 10y 5y/4w/3d/2h/1m/5s (cherry picked from commit 8d8f3137d174a43a259cbe50dd12730805f0200c)
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/convert.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/python/vyos/utils/convert.py b/python/vyos/utils/convert.py
index da207b9fb..41e65081f 100644
--- a/python/vyos/utils/convert.py
+++ b/python/vyos/utils/convert.py
@@ -19,12 +19,17 @@ 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
result = []
+ years = s // year
+ if years > 0:
+ result.append(f'{int(years)}y')
+ s = int(s % year)
weeks = s // week
if weeks > 0: