Skip to content
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

Add days and hours to DefaultLogger::formatTime method #1554

Merged
merged 4 commits into from
Mar 21, 2021
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
run: vendor/bin/phpcs -s -n --standard=./ruleset.xml src tests/Phing

- name: Static analysis (level 1)
run: vendor/bin/phpstan
run: vendor/bin/phpstan --ansi --no-progress --no-interaction

test:
needs:
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ that you might stumble across when upgrading from Phing 2 to 3.
* Visualizer
* ZendCodeAnalyser
* ZendServerDevelopmentTools
* The signature from `\Phing\Listener\DefaultLogger::formatTime` has been changed. Therefore, if you have written a
logger that overrides this method, you will need to update its signature accordingly.
71 changes: 57 additions & 14 deletions src/Phing/Listener/DefaultLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
use Phing\Phing;
use Phing\Project;
use Phing\Util\StringHelper;
use function end;
use function fmod;
use function intdiv;
use function vsprintf;

/**
* Writes a build event to the console.
Expand All @@ -45,6 +49,21 @@ class DefaultLogger implements StreamRequiredBuildLogger
*/
public const LEFT_COLUMN_SIZE = 12;

/**
* A day in seconds.
*/
protected const A_DAY = 86400;

/**
* An hour in seconds.
*/
protected const AN_HOUR = 3600;

/**
* A minute in seconds.
*/
protected const A_MINUTE = 60;

/**
* The message output level that should be used. The default is
* <code>Project::MSG_VERBOSE</code>.
Expand Down Expand Up @@ -306,26 +325,50 @@ public function messageLogged(BuildEvent $event)
}

/**
* Formats a time micro int to human readable format.
* Formats time (expressed in seconds) to a human readable format.
*
* @param int The time stamp
* @param float $seconds Time to convert, can have decimals.
* @return string
* @noinspection PhpMissingBreakStatementInspection
*/
public static function formatTime($micros)
public static function formatTime(float $seconds): string
{
$seconds = $micros;
$minutes = (int) floor($seconds / 60);
if ($minutes >= 1) {
return sprintf(
"%1.0f minute%s %0.2f second%s",
$minutes,
($minutes === 1 ? " " : "s "),
$seconds - floor($seconds / 60) * 60,
($seconds % 60 === 1 ? "" : "s")
);
/** @var int|float $number */
$getPlural = function ($number): string {
return $number == 1 ? '' : 's';
};
$chunks = [];
$format = '';
$precision = 4;
switch (true) {
// Days
case ($seconds >= self::A_DAY):
$chunks[] = intdiv($seconds, self::A_DAY);
$chunks[] = $getPlural(end($chunks));
$seconds = fmod($seconds, self::A_DAY);
$format .= '%u day%s ';
// Hours
case ($seconds >= self::AN_HOUR):
$chunks[] = intdiv($seconds, self::AN_HOUR);
$chunks[] = $getPlural(end($chunks));
$seconds = fmod($seconds, self::AN_HOUR);
$format .= '%u hour%s ';
// Minutes
case ($seconds >= self::A_MINUTE):
$chunks[] = intdiv($seconds, self::A_MINUTE);
$chunks[] = $getPlural(end($chunks));
$seconds = fmod($seconds, self::A_MINUTE);
$format .= '%u minute%s ';
$precision = 2;
// Seconds
default:
$chunks[] = $seconds;
$chunks[] = $getPlural(end($chunks));
$format .= "%.{$precision}F second%s";
break;
}

return sprintf("%0.4f second%s", $seconds, ($seconds % 60 === 1 ? "" : "s"));
return vsprintf($format, $chunks);
}

/**
Expand Down
37 changes: 36 additions & 1 deletion tests/Phing/Listener/DefaultLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function printMessage($message, ?OutputStream $stream = null, $priority =
echo $message;
}

public static function formatTime($micros)
public static function formatTime(float $seconds): string
{
return 'TIME_STRING';
}
Expand All @@ -96,4 +96,39 @@ public static function formatTime($micros)
$this->expectOutputString($msg);
$logger->buildFinished($event);
}

/**
* @dataProvider formatTimeProvider
*/
public function testFormatTime($seconds, string $expectedText)
{
$formattedText = DefaultLogger::formatTime($seconds);
$this->assertSame($formattedText, $expectedText);
}

public function formatTimeProvider()
{
return [
[0.0005, '0.0005 seconds'],
[0.000099, '0.0001 seconds'],
[1, '1.0000 second'],
[30.1234, '30.1234 seconds'],
[59.9999, '59.9999 seconds'],
[60.00, '1 minute 0.00 seconds'],
[61.0099, '1 minute 1.01 seconds'],
[61.9999, '1 minute 2.00 seconds'],
[3000.2020, '50 minutes 0.20 seconds'],
[3061, '51 minutes 1.00 second'],
[3600.0000, '1 hour 0 minutes 0.00 seconds'],
[3660.0000, '1 hour 1 minute 0.00 seconds'],
[3661.0000, '1 hour 1 minute 1.00 second'],
[7261, '2 hours 1 minute 1.00 second'],
[7458.5499, '2 hours 4 minutes 18.55 seconds'],
[86400.3, '1 day 0 hours 0 minutes 0.30 seconds'],
[90000.2, '1 day 1 hour 0 minutes 0.20 seconds'],
[90061, '1 day 1 hour 1 minute 1.00 second'],
[210546.8614, '2 days 10 hours 29 minutes 6.86 seconds'],
[3938279.8591, '45 days 13 hours 57 minutes 59.86 seconds'],
];
}
}
2 changes: 1 addition & 1 deletion tests/Phing/Listener/SilentLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function printMessage($message, ?OutputStream $stream = null, $priority =
echo $message;
}

public static function formatTime($micros)
public static function formatTime(float $micros): string
{
return 'TIME_STRING';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Phing/Listener/TimestampedLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function printMessage($message, ?OutputStream $stream = null, $priority =
echo $message;
}

public static function formatTime($micros)
public static function formatTime(float $micros): string
{
return 'TIME_STRING';
}
Expand Down