8000 bug #23793 [VarDumper] Fix interval caster with PT3600S-like spec (ma… · symfony/symfony@9026aed · GitHub
[go: up one dir, main page]

Skip to content

Commit 9026aed

Browse files
bug #23793 [VarDumper] Fix interval caster with PT3600S-like spec (maidmaid)
This PR was squashed before being merged into the 3.4 branch (closes #23793). Discussion ---------- [VarDumper] Fix interval caster with PT3600S-like spec | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | / | License | MIT | Doc PR | / According [ISO 8601](https://fr.wikipedia.org/wiki/ISO_8601): > The standard does not prohibit date and time values in a duration representation from exceeding their "carry over points" except as noted below. Thus, "PT36H" could be used as well as "P1DT12H" for representing the same duration. But this *breaks* ``interval`` representation, and this PR improves this. ```php dump(new DateInterval('PT3600S')); // before DateInterval { interval: + 00:00:3600.0 } // after DateInterval { interval: + 01:00:00.0 } ``` Commits ------- 046f8c1 [VarDumper] Fix interval caster with PT3600S-like spec
2 parents ff62871 + 046f8c1 commit 9026aed

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:'.self::formatSeconds($i->s, $i->f) : '';

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ public function provideIntervals()
184184
array('P5M', 0, '+ 5m', null),
185185
array('P6Y', 0, '+ 6y', null),
186186
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
187+
array('PT1M60S', 0, '+ 00:02:00'.$ms, null),
188+
array('PT1H60M', 0, '+ 02:00:00'.$ms, null),
189+
array('P1DT24H', 0, '+ 2d', null),
190+
array('P1M32D', 0, '+ 1m 32d', null),
187191

188192
array('PT0S', 1, '0s', '0s'),
189193
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
@@ -193,6 +197,10 @@ public function provideIntervals()
193197
array('P5M', 1, '- 5m', null),
194198
array('P6Y', 1, '- 6y', null),
195199
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
200+
array('PT1M60S', 1, '- 00:02:00'.$ms, null),
201+
array('PT1H60M', 1, '- 02:00:00'.$ms, null),
202+
array('P1DT24H', 1, '- 2d', null),
203+
array('P1M32D', 1, '- 1m 32d', null),
196204
);
197205
}
198206

0 commit comments

Comments
 (0)
0