Skip to content

[Console] Correct time formatting. #18429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,28 @@ public static function formatTime($secs)
{
static $timeFormats = array(
array(0, '< 1 sec'),
array(2, '1 sec'),
array(59, 'secs', 1),
array(1, '1 sec'),
array(2, 'secs', 1),
array(60, '1 min'),
array(3600, 'mins', 60),
array(5400, '1 hr'),
array(86400, 'hrs', 3600),
array(129600, '1 day'),
array(604800, 'days', 86400),
array(120, 'mins', 60),
array(3600, '1 hr'),
array(7200, 'hrs', 3600),
array(86400, '1 day'),
array(172800, 'days', 86400),
);

foreach ($timeFormats as $format) {
foreach ($timeFormats as $index => $format) {
if ($secs >= $format[0]) {
continue;
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|| $index == count($timeFormats) - 1
) {
if (2 == count($format)) {
return $format[1];
}

return floor($secs / $format[2]).' '.$format[1];
}
}

if (2 == count($format)) {
return $format[1];
}

return ceil($secs / $format[2]).' '.$format[1];
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Helper;

use Symfony\Component\Console\Helper\Helper;

class HelperTest extends \PHPUnit_Framework_TestCase
{
public function formatTimeProvider()
{
return array(
array(0, '< 1 sec'),
array(1, '1 sec'),
array(2, '2 secs'),
array(59, '59 secs'),
array(60, '1 min'),
array(61, '1 min'),
array(119, '1 min'),
array(120, '2 mins'),
array(121, '2 mins'),
array(3599, '59 mins'),
array(3600, '1 hr'),
array(7199, '1 hr'),
array(7200, '2 hrs'),
array(7201, '2 hrs'),
array(86399, '23 hrs'),
array(86400, '1 day'),
array(86401, '1 day'),
array(172799, '1 day'),
array(172800, '2 days'),
array(172801, '2 days'),
);
}

/**
* @dataProvider formatTimeProvider
*
* @param int $secs
* @param string $expectedFormat
*/
public function testFormatTime($secs, $expectedFormat)
{
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,17 @@ public function testAnsiColorsAndEmojis()
$this->generateOutput(
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
" \xf0\x9f\x8f\x81 1 sec \033[44;37m 0 B \033[0m"
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
).
$this->generateOutput(
" \033[44;37m Looks good to me... \033[0m\n".
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 97 KiB \033[0m"
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m"
).
$this->generateOutput(
" \033[44;37m Thanks, bye \033[0m\n".
' 15/15 '.str_repeat($done, 28)." 100%\n".
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 195 KiB \033[0m"
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 195 KiB \033[0m"
),
stream_get_contents($output->getStream())
);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy