Skip to content

Improve 'duration' filter - #205

Open
iatanasov77 wants to merge 3 commits into
KnpLabs:mainfrom
iatanasov77:master
Open

Improve 'duration' filter#205
iatanasov77 wants to merge 3 commits into
KnpLabs:mainfrom
iatanasov77:master

Conversation

@iatanasov77

Copy link
Copy Markdown

No description provided.

@kbond kbond left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I'm not sure about this. I wanted to keep the same logic as symfony/console, which is a very approximate duration. I admit at lower durations, the approximation can be way off though.

Comment thread src/DateTimeFormatter.php Outdated
* @source https://github.com/symfony/symfony/blob/ad72245261792c6b5d2db821fcbd141b11095215/src/Symfony/Component/Console/Helper/Helper.php#L97
*/
public function formatDuration(float $seconds, string $locale = null): string
public function formatDuration(float $seconds, int $precision = 2, string $locale = null): string

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

Suggested change
public function formatDuration(float $seconds, int $precision = 2, string $locale = null): string
public function formatDuration(float $seconds, string $locale = null, ?int $precision = null): string

Adding the $precision before $locale is a BC break. Default to null to keep current behavior by default.

Comment thread src/DateTimeFormatter.php Outdated
return $this->translator->trans(
$format[1],
['%count%' => floor($seconds / $format[2])],
['%count%' => number_format($seconds / $format[2], $precision)],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
['%count%' => number_format($seconds / $format[2], $precision)],
['%count%' => null === $precision ? floor($seconds / $format[2]) : number_format($seconds / $format[2], $precision)],

To keep the current behaviour.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My previous logic was wrong :( I remove the precision parameter and add a private method that format the seconds into float number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants