summaryrefslogtreecommitdiffstats
path: root/src/etc/inc/pfsense-utils.inc
diff options
context:
space:
mode:
authorstilez <stilez@users.noreply.github.com>2016-05-31 07:12:43 +0100
committerStephen Beaver <sbeaver@netgate.com>2016-06-22 11:22:39 -0400
commit8c91c89f172234c616729b06fa84630d4f84bba9 (patch)
tree413122585e157867cc97b1709b8d6b68adad3a7e /src/etc/inc/pfsense-utils.inc
parent17e3a05af3428372c20a83cbc36b2ce884d3f5e7 (diff)
downloadpfsense-8c91c89f172234c616729b06fa84630d4f84bba9.zip
pfsense-8c91c89f172234c616729b06fa84630d4f84bba9.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) (cherry picked from commit 0bde6d1057ed39c8ef650a5a505cf9ae5eb7199e)
Diffstat (limited to 'src/etc/inc/pfsense-utils.inc')
-rw-r--r--src/etc/inc/pfsense-utils.inc34
1 files changed, 12 insertions, 22 deletions
diff --git a/src/etc/inc/pfsense-utils.inc b/src/etc/inc/pfsense-utils.inc
index 98447f0..e370fb2 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") {
OpenPOWER on IntegriCloud