Skip to content

Commit

Permalink
bug #43867 [VarDumper] Make dumping DateInterval instances timezone-i…
Browse files Browse the repository at this point in the history
…ndependent (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[VarDumper] Make dumping DateInterval instances timezone-independent

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

In our CI, we currently see failing VarDumper tests because the tests have been executed close to the end of DST in Europe. This PR attempts to make dumping `DateInterval` instances more robust by decoupling the dumper from global timezone settings and the current date.

Commits
-------

c1735c1 [VarDumper] Make dumping DateInterval instances timezone-independent
  • Loading branch information
fabpot committed Nov 1, 2021
2 parents bd1b1f4 + c1735c1 commit 897d2c9
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/DateCaster.php
Expand Up @@ -49,7 +49,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,

public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
{
$now = new \DateTimeImmutable();
$now = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
$numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
$title = number_format($numberOfSeconds, 0, '.', ' ').'s';

Expand All @@ -63,7 +63,8 @@ private static function formatInterval(\DateInterval $i): string
$format = '%R ';

if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
$i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
$d = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
$i = $d->diff($d->add($i)); // recalculate carry over points
$format .= 0 < $i->days ? '%ad ' : '';
} else {
$format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
Expand Down

0 comments on commit 897d2c9

Please sign in to comment.