Project

General

Profile

« Previous | Next » 

Revision 0bde6d10

Added by Stilez y about 9 years ago

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)

View differences:

src/etc/inc/pfsense-utils.inc
1144 1144
	return $pppoeenable;
1145 1145
}
1146 1146

  
1147
function convert_seconds_to_hms($sec) {
1148
	$min = $hrs = 0;
1149
	if ($sec != 0) {
1150
		$min = floor($sec/60);
1151
		$sec %= 60;
1147
function convert_seconds_to_dhms($sec) {
1148
	if (!is_numericint($sec)) {
1149
		return '-';
1152 1150
	}
1153
	if ($min != 0) {
1154
		$hrs = floor($min/60);
1155
		$min %= 60;
1156
	}
1157
	if ($sec < 10) {
1158
		$sec = "0".$sec;
1159
	}
1160
	if ($min < 10) {
1161
		$min = "0".$min;
1162
	}
1163
	if ($hrs < 10) {
1164
		$hrs = "0".$hrs;
1165
	}
1166
	$result = $hrs.":".$min.":".$sec;
1167
	return $result;
1151
	// FIXME: When we move to PHP 7 we can use "intdiv($sec % X, Y)" etc
1152
	list($d, $h, $m, $s) = array(	(int)($sec/86400),
1153
					(int)(($sec % 86400)/3600),
1154
					(int)(($sec % 3600)/60),
1155
					$sec % 60
1156
				);
1157
	return ($d > 0 ? $d . 'd ' : '') . sprintf('%02d:%02d:%02d', $h, $m, $s);
1168 1158
}
1169 1159

  
1170 1160
/* Compute the total uptime from the ppp uptime log file in the conf directory */
......
1177 1167
		foreach ($uptime_data as $upt) {
1178 1168
			$sec += substr($upt, 1 + strpos($upt, " "));
1179 1169
		}
1180
		return convert_seconds_to_hms($sec);
1170
		return convert_seconds_to_dhms($sec);
1181 1171
	} else {
1182 1172
		$total_time = gettext("No history data found!");
1183 1173
		return $total_time;
......
1351 1341

  
1352 1342
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1353 1343
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1354
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1344
		$ifinfo['ppp_uptime'] = convert_seconds_to_dhms($sec);
1355 1345
	}
1356 1346

  
1357 1347
	if ($ifinfo['status'] == "up") {

Also available in: Unified diff