From f2f86310b58b0329cd4d4b417c0aee811b8da999 Mon Sep 17 00:00:00 2001 From: stilez Date: Tue, 31 May 2016 07:12:43 +0100 Subject: Simplify convert_seconds_to_hms() and show days for large numbers of hours 1) Function can be simplified and all "if" statements removed, using intdiv (or casting result as int for PHP < 7) and % for calcs and sprintf for padding. 2) Input validity check before trying to convert format 3) If time represented is large (eg uptime might be several months) then hours becomes unhelpful, it's clearer to show "4921:02:06" as "205d 01:02:06". (Leading "days" value not shown unless >=1 for simplicity) (cherry picked from commit 0bde6d1057ed39c8ef650a5a505cf9ae5eb7199e) --- src/etc/inc/pfsense-utils.inc | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/etc/inc/pfsense-utils.inc b/src/etc/inc/pfsense-utils.inc index 066d9d7..fb9f43d 100644 --- a/src/etc/inc/pfsense-utils.inc +++ b/src/etc/inc/pfsense-utils.inc @@ -1144,27 +1144,17 @@ function is_pppoe_server_enabled() { return $pppoeenable; } -function convert_seconds_to_hms($sec) { - $min = $hrs = 0; - if ($sec != 0) { - $min = floor($sec/60); - $sec %= 60; +function convert_seconds_to_dhms($sec) { + if (!is_numericint($sec)) { + return '-'; } - if ($min != 0) { - $hrs = floor($min/60); - $min %= 60; - } - if ($sec < 10) { - $sec = "0".$sec; - } - if ($min < 10) { - $min = "0".$min; - } - if ($hrs < 10) { - $hrs = "0".$hrs; - } - $result = $hrs.":".$min.":".$sec; - return $result; + // FIXME: When we move to PHP 7 we can use "intdiv($sec % X, Y)" etc + list($d, $h, $m, $s) = array( (int)($sec/86400), + (int)(($sec % 86400)/3600), + (int)(($sec % 3600)/60), + $sec % 60 + ); + return ($d > 0 ? $d . 'd ' : '') . sprintf('%02d:%02d:%02d', $h, $m, $s); } /* Compute the total uptime from the ppp uptime log file in the conf directory */ @@ -1177,7 +1167,7 @@ function get_ppp_uptime($port) { foreach ($uptime_data as $upt) { $sec += substr($upt, 1 + strpos($upt, " ")); } - return convert_seconds_to_hms($sec); + return convert_seconds_to_dhms($sec); } else { $total_time = gettext("No history data found!"); return $total_time; @@ -1351,7 +1341,7 @@ function get_interface_info($ifdescr) { if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) { $sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`); - $ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec); + $ifinfo['ppp_uptime'] = convert_seconds_to_dhms($sec); } if ($ifinfo['status'] == "up") { -- cgit v1.1