From 6e9cd8821ca028b5bc05c14b0b4e3454036da6da Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 21 Apr 2024 10:33:15 +0200 Subject: 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. --- python/vyos/utils/convert.py | 18 +++++++++--------- 1 file 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 +# Copyright 2023-2024 VyOS maintainers and contributors # # 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): -- cgit v1.2.3 From 8d8f3137d174a43a259cbe50dd12730805f0200c Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 21 Apr 2024 10:34:43 +0200 Subject: 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 --- python/vyos/utils/convert.py | 5 +++++ 1 file changed, 5 insertions(+) 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: -- cgit v1.2.3 From 31b21d26751b7db7ab784486da5b8690ddd4a058 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 21 Apr 2024 10:35:08 +0200 Subject: op-mode: T6244: add whitespace after time unit in "show system uptime" --- src/op_mode/uptime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/op_mode/uptime.py b/src/op_mode/uptime.py index d6adf6f4d..059a4c3f6 100755 --- a/src/op_mode/uptime.py +++ b/src/op_mode/uptime.py @@ -49,7 +49,7 @@ def _get_raw_data(): res = {} res["uptime_seconds"] = _get_uptime_seconds() - res["uptime"] = seconds_to_human(_get_uptime_seconds()) + res["uptime"] = seconds_to_human(_get_uptime_seconds(), separator=' ') res["load_average"] = _get_load_averages() return res -- cgit v1.2.3