Skip to content

Commit db1ebe1

Browse files
authored
Merge pull request #658 from PHPCSStandards/feature/util-timer-gethumanreadableduration-improve
Util\Timing::getHumanReadableDuration: improve time display
2 parents f145bb4 + 7ee4cf5 commit db1ebe1

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Util/Timing.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
class Timing
1313
{
1414

15+
/**
16+
* Number of milliseconds in a minute.
17+
*
18+
* @var int
19+
*/
20+
const MINUTE_IN_MS = 60000;
21+
22+
/**
23+
* Number of milliseconds in a second.
24+
*
25+
* @var int
26+
*/
27+
const SECOND_IN_MS = 1000;
28+
1529
/**
1630
* The start time of the run in microseconds.
1731
*
@@ -67,15 +81,15 @@ public static function getDuration()
6781
public static function getHumanReadableDuration($duration)
6882
{
6983
$timeString = '';
70-
if ($duration > 60000) {
71-
$mins = floor($duration / 60000);
72-
$secs = round((fmod($duration, 60000) / 1000), 2);
84+
if ($duration >= self::MINUTE_IN_MS) {
85+
$mins = floor($duration / self::MINUTE_IN_MS);
86+
$secs = round((fmod($duration, self::MINUTE_IN_MS) / self::SECOND_IN_MS), 2);
7387
$timeString = $mins.' mins';
74-
if ($secs !== 0) {
88+
if ($secs >= 0.01) {
7589
$timeString .= ", $secs secs";
7690
}
77-
} else if ($duration > 1000) {
78-
$timeString = round(($duration / 1000), 2).' secs';
91+
} else if ($duration >= self::SECOND_IN_MS) {
92+
$timeString = round(($duration / self::SECOND_IN_MS), 2).' secs';
7993
} else {
8094
$timeString = round($duration).'ms';
8195
}

tests/Core/Util/Timing/GetHumanReadableDurationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function dataGetHumanReadableDuration()
6868
],
6969
'Duration: 1 second' => [
7070
'duration' => 1000,
71-
'expected' => '1000ms',
71+
'expected' => '1 secs',
7272
],
7373
'Duration: slightly more than 1 second' => [
7474
'duration' => 1001.178215,
@@ -80,11 +80,11 @@ public static function dataGetHumanReadableDuration()
8080
],
8181
'Duration: exactly 1 minute' => [
8282
'duration' => 60000,
83-
'expected' => '60 secs',
83+
'expected' => '1 mins',
8484
],
8585
'Duration: slightly more than 1 minute' => [
8686
'duration' => 60001.7581235,
87-
'expected' => '1 mins, 0 secs',
87+
'expected' => '1 mins',
8888
],
8989
'Duration: 1 minute, just under half a second' => [
9090
'duration' => 60499.83639,
@@ -100,7 +100,7 @@ public static function dataGetHumanReadableDuration()
100100
],
101101
'Duration: exactly 1 hour' => [
102102
'duration' => 3600000,
103-
'expected' => '60 mins, 0 secs',
103+
'expected' => '60 mins',
104104
],
105105
'Duration: 89.4 mins' => [
106106
'duration' => 5364000,

0 commit comments

Comments
 (0)