8000 feature #58946 [Console] Add support of millisecondes for `formatTime… · symfony/symfony@d1a3375 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1a3375

Browse files
committed
feature #58946 [Console] Add support of millisecondes for formatTime (SebLevDev)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Console] Add support of millisecondes for `formatTime` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT ## Description This pull request introduces support for formatting time durations down to milliseconds in the `Helper::formatTime()` method. Previously, the method only supported seconds and larger units (minutes, hours, days). The new implementation adds: 1. Support for milliseconds when the input is a fractional value (e.g., 1.456 → 1 sec, 456 ms). 2. Accurate rounding for milliseconds. 3. Respect for the precision parameter, limiting the number of time components displayed in the output. ## Examples ```php Helper::formatTime(0.001); // "1 ms" Helper::formatTime(1.456); // "1 s, 456 ms" Helper::formatTime(125.789, 2); // "2 min, 5 s" Helper::formatTime(172799.999, 4); // "1 d, 23 h, 59 min, 59 s" Helper::formatTime(0.0005); // "< 1 ms" ``` Commits ------- eac7d49 [Console] Add support of millisecondes for `formatTime`
2 parents 8c841e3 + eac7d49 commit d1a3375

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,41 @@ public static function substr(?string $string, int $from, ?int $length = null):
8787

8888
public static function formatTime(int|float $secs, int $precision = 1): string
8989
{
90+
$ms = (int) ($secs * 1000);
9091
$secs = (int) floor($secs);
9192

92-
if (0 === $secs) {
93-
return '< 1 sec';
93+
if (0 === $ms) {
94+
return '< 1 ms';
9495
}
9596

9697
static $timeFormats = [
97-
[1, '1 sec', 'secs'],
98-
[60, '1 min', 'mins'],
99-
[3600, '1 hr', 'hrs'],
100-
[86400, '1 day', 'days'],
98+
[1, 'ms'],
99+
[1000, 's'],
100+
[60000, 'min'],
101+
[3600000, 'h'],
102+
[86_400_000, 'd'],
101103
];
102104

103105
$times = [];
104106
foreach ($timeFormats as $index => $format) {
105-
$seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
107+
$milliSeconds = isset($timeFormats[$index + 1]) ? $ms % $timeFormats[$index + 1][0] : $ms;
106108

107109
if (isset($times[$index - $precision])) {
108110
unset($times[$index - $precision]);
109111
}
110112

111-
if (0 === $seconds) {
113+
if (0 === $milliSeconds) {
112114
continue;
113115
}
114116

115-
$unitCount = ($seconds / $format[0]);
116-
$times[$index] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2];
117+
$unitCount = ($milliSeconds / $format[0]);
118+
$times[$index] = $unitCount.' '.$format[1];
117119

118-
if ($secs === $seconds) {
120+
if ($ms === $milliSeconds) {
119121
break;
120122
}
121123

122-
$secs -= $seconds;
124+
$ms -= $milliSeconds;
123125
}
124126

125127
return implode(', ', array_reverse($times));

src/Symfony/Component/Console/Tests/Helper/HelperTest.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,34 @@ class HelperTest extends TestCase
2020
public static function formatTimeProvider()
2121
{
2222
return [
23-
[0, '< 1 sec', 1],
24-
[0.95, '< 1 sec', 1],
25-
[1, '1 sec', 1],
26-
[2, '2 secs', 2],
27-
[59, '59 secs', 1],
28-
[59.21, '59 secs', 1],
23+
[0, '< 1 ms', 1],
24+
[0.0004, '< 1 ms', 1],
25+
[0.95, '950 ms', 1],
26+
[1, '1 s', 1],
27+
[2, '2 s', 2],
28+
[59, '59 s', 1],
29+
[59.21, '59 s', 1],
30+
[59.21, '59 s, 210 ms', 5],
2931
[60, '1 min', 2],
30-
[61, '1 min, 1 sec', 2],
31-
[119, '1 min, 59 secs', 2],
32-
[120, '2 mins', 2],
33-
[121, '2 mins, 1 sec', 2],
34-
[3599, '59 mins, 59 secs', 2],
35-
[3600, '1 hr', 2],
36-
[7199, '1 hr, 59 mins', 2],
37-
[7200, '2 hrs', 2],
38-
[7201, '2 hrs', 2],
39-
[86399, '23 hrs, 59 mins', 2],
40-
[86399, '23 hrs, 59 mins, 59 secs', 3],
41-
[86400, '1 day', 2],
42-
[86401, '1 day', 2],
43-
[172799, '1 day, 23 hrs', 2],
44-
[172799, '1 day, 23 hrs, 59 mins, 59 secs', 4],
45-
[172800, '2 days', 2],
46-
[172801, '2 days', 2],
47-
[172801, '2 days, 1 sec', 4],
32+
[61, '1 min, 1 s', 2],
33+
[119, '1 min, 59 s', 2],
34+
[120, '2 min', 2],
35+
[121, '2 min, 1 s', 2],
36+
[3599, '59 min, 59 s', 2],
37+
[3600, '1 h', 2],
38+
[7199, '1 h, 59 min', 2],
39+
[7200, '2 h', 2],
40+
[7201, '2 h', 2],
41+
[86399, '23 h, 59 min', 2],
42+
[86399, '23 h, 59 min, 59 s', 3],
43+
[86400, '1 d', 2],
44+
[86401, '1 d', 2],
45+
[172799, '1 d, 23 h', 2],
46+
[172799, '1 d, 23 h, 59 min, 59 s', 4],
47+
[172799.123, '1 d, 23 h, 59 min, 59 s, 123 ms', 5],
48+
[172800, '2 d', 2],
49+
[172801, '2 d', 2],
50+
[172801, '2 d, 1 s', 4],
4851
];
4952
}
5053

0 commit comments

Comments
 (0)
0