diff options
author | stilez <stilez@users.noreply.github.com> | 2016-05-31 07:12:43 +0100 |
---|---|---|
committer | stilez <stilez@users.noreply.github.com> | 2016-05-31 07:12:43 +0100 |
commit | 0bde6d1057ed39c8ef650a5a505cf9ae5eb7199e (patch) | |
tree | 86321db66f0cb2f9dd280cac68579ec570fc9e0b | |
parent | e5026472d7b24d8ef1d31412f323d90e4949545c (diff) | |
download | pfsense-0bde6d1057ed39c8ef650a5a505cf9ae5eb7199e.zip pfsense-0bde6d1057ed39c8ef650a5a505cf9ae5eb7199e.tar.gz |
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)
-rw-r--r-- | src/etc/inc/pfsense-utils.inc | 34 |
1 files changed, 12 insertions, 22 deletions
diff --git a/src/etc/inc/pfsense-utils.inc b/src/etc/inc/pfsense-utils.inc index e0d5c12..f9b31d1 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") { |