From d13929d49de9298b74940d254915adc637dab63b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 14 Sep 2016 10:35:42 +0200 Subject: [PATCH] DHCP: Format lease time in human-readable format Signed-off-by: Michael Tremer --- src/functions/functions.util | 20 ++++++++++++++++++++ src/network | 10 +++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/functions/functions.util b/src/functions/functions.util index 91b5148c..9c3601b1 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -113,6 +113,26 @@ format() { printf -v "${key}" "${format}" "$@" } +format_time() { + local s=${1} + local ret m + + local units="s m h" + + local unit + for unit in ${units}; do + m=$(( ${s} % 60 )) + s=$(( ${s} / 60 )) + + if [ ${m} -gt 0 ]; then + ret="${m}${unit} ${ret}" + fi + done + + # Remove whitespace + echo ${ret} +} + assign() { local key=${1} assert isset key diff --git a/src/network b/src/network index e45e8ada..719664e5 100644 --- a/src/network +++ b/src/network @@ -840,11 +840,11 @@ cli_dhcpd_show() { ipv6) cli_headline 2 "Lease times" if isinteger VALID_LIFETIME; then - cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s" + cli_print_fmt1 2 "Valid lifetime" "$(format_time ${VALID_LIFETIME})" fi if isinteger PREFERRED_LIFETIME; then - cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s" + cli_print_fmt1 2 "Preferred lifetime" "$(format_time ${PREFERRED_LIFETIME})" fi cli_space @@ -854,11 +854,11 @@ cli_dhcpd_show() { cli_space cli_headline 2 "Lease times" - cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s" - cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s" + cli_print_fmt1 2 "Default lease time" "$(format_time ${DEFAULT_LEASE_TIME})" + cli_print_fmt1 2 "Max. lease time" "$(format_time ${MAX_LEASE_TIME})" if isset MIN_LEASE_TIME; then - cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s" + cli_print_fmt1 2 "Min. lease time" "$(format_time ${MIN_LEASE_TIME})" fi cli_space -- 2.39.2