8000 [VarDumper] Fix interval caster with PT3600S-like spec · symfony/symfony@046f8c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 046f8c1

Browse files
maidmaidnicolas-grekas
authored andcommitted
[VarDumper] Fix interval caster with PT3600S-like spec
1 parent 978eca9 commit 046f8c1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Symfony/Component/VarDumper/Caster/DateCaster.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ public static function castInterval(\DateInterval $interval, array $a, Stub $stu
5252

5353
private static function formatInterval(\DateInterval $i)
5454
{
55-
$format = '%R '
56-
.($i->y ? '%yy ' : '')
57-
.($i->m ? '%mm ' : '')
58-
.($i->d ? '%dd ' : '')
59-
;
55+
$format = '%R ';
56+
57+
if ($i->y === 0 && $i->m === 0 && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
58+
$i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
59+
$format .= 0 < $i->days ? '%ad ' : '';
60+
} else {
61+
$format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
62+
}
6063

6164
if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
6265
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';

src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ public function provideIntervals()
173173
array('P5M', 0, '+ 5m', null),
174174
array('P6Y', 0, '+ 6y', null),
175175
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
176+
array('PT1M60S', 0, '+ 00:02:00'.$ms, null),
177+
array('PT1H60M', 0, '+ 02:00:00'.$ms, null),
178+
array('P1DT24H', 0, '+ 2d', null),
179+
array('P1M32D', 0, '+ 1m 32d', null),
176180

177181
array('PT0S', 1, '0s', '0s'),
178182
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
@@ -182,6 +186,10 @@ public function provideIntervals()
182186
array('P5M', 1, '- 5m', null),
183187
array('P6Y', 1, '- 6y', null),
184188
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
189+
array('PT1M60S', 1, '- 00:02:00'.$ms, null),
190+
array('PT1H60M', 1, '- 02:00:00'.$ms, null),
191+
array('P1DT24H', 1, '- 2d', null),
192+
array('P1M32D', 1, '- 1m 32d', null),
185193
);
186194
}
187195

0 commit comments

Comments
 (0)
0