From a3f31a90aaabdea26eb05929b5801b3ebc9c8cd2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 31 Oct 2024 19:02:17 +0100 Subject: [PATCH 1/2] Util\Timing::getHumanReadableDuration: improve time display This commit makes some tiny changes to how the run time is displayed: * For "exactly 1 second", display `1 secs` instead of `1000ms`. * For "exactly 1 minute", display `1 mins` instead of `60 secs`. * When minutes are displayed, only display seconds when there are seconds to display (so no `# mins, 0 secs`). Includes updating the tests to match. --- src/Util/Timing.php | 6 +++--- tests/Core/Util/Timing/GetHumanReadableDurationTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Util/Timing.php b/src/Util/Timing.php index f5bfda611b..d3eee7e71f 100644 --- a/src/Util/Timing.php +++ b/src/Util/Timing.php @@ -67,14 +67,14 @@ public static function getDuration() public static function getHumanReadableDuration($duration) { $timeString = ''; - if ($duration > 60000) { + if ($duration >= 60000) { $mins = floor($duration / 60000); $secs = round((fmod($duration, 60000) / 1000), 2); $timeString = $mins.' mins'; - if ($secs !== 0) { + if ($secs >= 0.01) { $timeString .= ", $secs secs"; } - } else if ($duration > 1000) { + } else if ($duration >= 1000) { $timeString = round(($duration / 1000), 2).' secs'; } else { $timeString = round($duration).'ms'; diff --git a/tests/Core/Util/Timing/GetHumanReadableDurationTest.php b/tests/Core/Util/Timing/GetHumanReadableDurationTest.php index 113c93e5fb..93b3d05e1e 100644 --- a/tests/Core/Util/Timing/GetHumanReadableDurationTest.php +++ b/tests/Core/Util/Timing/GetHumanReadableDurationTest.php @@ -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, @@ -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, @@ -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, From 7ee4cf57c0c3f5ebde5924ba1191252821b20678 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 31 Oct 2024 19:33:27 +0100 Subject: [PATCH 2/2] Util\Timing::getHumanReadableDuration: magic numbers to constants Make the code more self-descriptive and use less "magic numbers" by declaring a couple of constants. --- src/Util/Timing.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Util/Timing.php b/src/Util/Timing.php index d3eee7e71f..95f6810b3b 100644 --- a/src/Util/Timing.php +++ b/src/Util/Timing.php @@ -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. * @@ -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.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'; }