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
This commit is contained in:
Nicolas Grekas 2017-08-16 18:14:34 +02:00
commit 9026aed89e
2 changed files with 16 additions and 5 deletions

View File

@ -52,11 +52,14 @@ class DateCaster
private static function formatInterval(\DateInterval $i)
{
$format = '%R '
.($i->y ? '%yy ' : '')
.($i->m ? '%mm ' : '')
.($i->d ? '%dd ' : '')
;
$format = '%R ';
if ($i->y === 0 && $i->m === 0 && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
$i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
$format .= 0 < $i->days ? '%ad ' : '';
} else {
$format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
}
if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, $i->f) : '';

View File

@ -184,6 +184,10 @@ EODUMP;
array('P5M', 0, '+ 5m', null),
array('P6Y', 0, '+ 6y', null),
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, '+ 00:02:00'.$ms, null),
array('PT1H60M', 0, '+ 02:00:00'.$ms, null),
array('P1DT24H', 0, '+ 2d', null),
array('P1M32D', 0, '+ 1m 32d', null),
array('PT0S', 1, '0s', '0s'),
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
@ -193,6 +197,10 @@ EODUMP;
array('P5M', 1, '- 5m', null),
array('P6Y', 1, '- 6y', null),
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 1, '- 00:02:00'.$ms, null),
array('PT1H60M', 1, '- 02:00:00'.$ms, null),
array('P1DT24H', 1, '- 2d', null),
array('P1M32D', 1, '- 1m 32d', null),
);
}