Skip to content

Commit 283875b

Browse files
committed
bug #18429 [Console] Correct time formatting. (camporter)
This PR was squashed before being merged into the 2.7 branch (closes #18429). Discussion ---------- [Console] Correct time formatting. | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18420 | License | MIT | Doc PR | The previous behavior caused dramatic jumps in the reported time instead of smoothly transitioning between time ranges. Added tests around the new behavior and the transitions between seconds, minutes, and days. Commits ------- b264b66 [Console] Correct time formatting.
2 parents 5c94dcc + b264b66 commit 283875b

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,28 @@ public static function formatTime($secs)
6666
{
6767
static $timeFormats = array(
6868
array(0, '< 1 sec'),
69-
array(2, '1 sec'),
70-
array(59, 'secs', 1),
69+
array(1, '1 sec'),
70+
array(2, 'secs', 1),
7171
array(60, '1 min'),
72-
array(3600, 'mins', 60),
73-
array(5400, '1 hr'),
74-
array(86400, 'hrs', 3600),
75-
array(129600, '1 day'),
76-
array(604800, 'days', 86400),
72+
array(120, 'mins', 60),
73+
array(3600, '1 hr'),
74+
array(7200, 'hrs', 3600),
75+
array(86400, '1 day'),
76+
array(172800, 'days', 86400),
7777
);
7878

79-
foreach ($timeFormats as $format) {
79+
foreach ($timeFormats as $index => $format) {
8080
if ($secs >= $format[0]) {
81-
continue;
81+
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
82+
|| $index == count($timeFormats) - 1
83+
) {
84+
if (2 == count($format)) {
85+
return $format[1];
86+
}
87+
88+
return floor($secs / $format[2]).' '.$format[1];
89+
}
8290
}
83-
84-
if (2 == count($format)) {
85-
return $format[1];
86-
}
87-
88-
return ceil($secs / $format[2]).' '.$format[1];
8991
}
9092
}
9193

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Helper;
13+
14+
use Symfony\Component\Console\Helper\Helper;
15+
16+
class HelperTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function formatTimeProvider()
19+
{
20+
return array(
21+
array(0, '< 1 sec'),
22+
array(1, '1 sec'),
23+
array(2, '2 secs'),
24+
array(59, '59 secs'),
25+
array(60, '1 min'),
26+
array(61, '1 min'),
27+
array(119, '1 min'),
28+
array(120, '2 mins'),
29+
array(121, '2 mins'),
30+
array(3599, '59 mins'),
31+
array(3600, '1 hr'),
32+
array(7199, '1 hr'),
33+
array(7200, '2 hrs'),
34+
array(7201, '2 hrs'),
35+
array(86399, '23 hrs'),
36+
array(86400, '1 day'),
37+
array(86401, '1 day'),
38+
array(172799, '1 day'),
39+
array(172800, '2 days'),
40+
array(172801, '2 days'),
41+
);
42+
}
43+
44+
/**
45+
* @dataProvider formatTimeProvider
46+
*
47+
* @param int $secs
48+
* @param string $expectedFormat
49+
*/
50+
public function testFormatTime($secs, $expectedFormat)
51+
{
52+
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
53+
}
54+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,17 @@ public function testAnsiColorsAndEmojis()
591591
$this->generateOutput(
592592
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
593593
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
594-
" \xf0\x9f\x8f\x81 1 sec \033[44;37m 0 B \033[0m"
594+
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
595595
).
596596
$this->generateOutput(
597597
" \033[44;37m Looks good to me... \033[0m\n".
598598
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
599-
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 97 KiB \033[0m"
599+
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m"
600600
).
601601
$this->generateOutput(
602602
" \033[44;37m Thanks, bye \033[0m\n".
603603
' 15/15 '.str_repeat($done, 28)." 100%\n".
604-
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 195 KiB \033[0m"
604+
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 195 KiB \033[0m"
605605
),
606606
stream_get_contents($output->getStream())
607607
);

0 commit comments

Comments
 (0)