Skip to content

Commit

Permalink
Merge pull request squizlabs#658 from PHPCSStandards/feature/util-tim…
Browse files Browse the repository at this point in the history
…er-gethumanreadableduration-improve

Util\Timing::getHumanReadableDuration: improve time display
  • Loading branch information
jrfnl authored Nov 2, 2024
2 parents f145bb4 + 7ee4cf5 commit db1ebe1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
26 changes: 20 additions & 6 deletions src/Util/Timing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
class Timing
{

/**
* Number of milliseconds in a minute.
*
* @var int
*/
const MINUTE_IN_MS = 60000;

/**
* Number of milliseconds in a second.
*
* @var int
*/
const SECOND_IN_MS = 1000;

/**
* The start time of the run in microseconds.
*
Expand Down Expand Up @@ -67,15 +81,15 @@ public static function getDuration()
public static function getHumanReadableDuration($duration)
{
$timeString = '';
if ($duration > 60000) {
$mins = floor($duration / 60000);
$secs = round((fmod($duration, 60000) / 1000), 2);
if ($duration >= self::MINUTE_IN_MS) {
$mins = floor($duration / self::MINUTE_IN_MS);
$secs = round((fmod($duration, self::MINUTE_IN_MS) / self::SECOND_IN_MS), 2);
$timeString = $mins.' mins';
if ($secs !== 0) {
if ($secs >= 0.01) {
$timeString .= ", $secs secs";
}
} else if ($duration > 1000) {
$timeString = round(($duration / 1000), 2).' secs';
} else if ($duration >= self::SECOND_IN_MS) {
$timeString = round(($duration / self::SECOND_IN_MS), 2).' secs';
} else {
$timeString = round($duration).'ms';
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Core/Util/Timing/GetHumanReadableDurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function dataGetHumanReadableDuration()
],
'Duration: 1 second' => [
'duration' => 1000,
'expected' => '1000ms',
'expected' => '1 secs',
],
'Duration: slightly more than 1 second' => [
'duration' => 1001.178215,
Expand All @@ -80,11 +80,11 @@ public static function dataGetHumanReadableDuration()
],
'Duration: exactly 1 minute' => [
'duration' => 60000,
'expected' => '60 secs',
'expected' => '1 mins',
],
'Duration: slightly more than 1 minute' => [
'duration' => 60001.7581235,
'expected' => '1 mins, 0 secs',
'expected' => '1 mins',
],
'Duration: 1 minute, just under half a second' => [
'duration' => 60499.83639,
Expand All @@ -100,7 +100,7 @@ public static function dataGetHumanReadableDuration()
],
'Duration: exactly 1 hour' => [
'duration' => 3600000,
'expected' => '60 mins, 0 secs',
'expected' => '60 mins',
],
'Duration: 89.4 mins' => [
'duration' => 5364000,
Expand Down

0 comments on commit db1ebe1

Please sign in to comment.