Skip to content

Commit

Permalink
bug #24030 [VarDumper] Improve microseconds support in date caster (m…
Browse files Browse the repository at this point in the history
…aidmaid)

This PR was merged into the 3.4 branch.

Discussion
----------

[VarDumper] Improve microseconds support in date caster

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

This PR adds the microseconds in ``period`` virtual property and in tooltip overview.

Before:
![](https://user-images.githubusercontent.com/4578773/29843773-58a2e212-8d0d-11e7-9053-3eb1cf933e3b.png)

After:
![](https://user-images.githubusercontent.com/4578773/29843774-58bd8342-8d0d-11e7-8d83-40835c89313c.png)

And fixes interval caster:

```php
dump((new DateTime('00:00:00'))->diff(new DateTime('00:00:00.123')));

// before
DateInterval {
  interval: + 00:00:00.0.123
}

// after
DateInterval {
  interval: + 00:00:00.123
}
```

Commits
-------

479b5d6 Improve microseconds support in date caster
  • Loading branch information
nicolas-grekas committed Sep 4, 2017
2 parents c9bee84 + 479b5d6 commit aad90fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
15 changes: 10 additions & 5 deletions src/Symfony/Component/VarDumper/Caster/DateCaster.php
Expand Up @@ -32,7 +32,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
;

$a = array();
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).($location ? ' e (P)' : ' P')), $title);
$a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);

$stub->class .= $d->format(' @U');

Expand Down Expand Up @@ -62,7 +62,7 @@ private static function formatInterval(\DateInterval $i)
}

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) : '';
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
} else {
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
}
Expand Down Expand Up @@ -100,23 +100,28 @@ public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNeste
);
break;
}
$dates[] = sprintf('%s) %s', $i + 1, $d->format('Y-m-d H:i:s'));
$dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d));
}
}

$period = sprintf(
'every %s, from %s (%s) %s',
self::formatInterval($p->getDateInterval()),
$p->getStartDate()->format('Y-m-d H:i:s'),
self::formatDateTime($p->getStartDate()),
$p->include_start_date ? 'included' : 'excluded',
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'recurring '.$p->recurrences.' time/s'
($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s'
);

$p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates)));

return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
}

private static function formatDateTime(\DateTimeInterface $d, $extra = '')
{
return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra);
}

private static function formatSeconds($s, $us)
{
return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));
Expand Down
112 changes: 61 additions & 51 deletions src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
Expand Up @@ -100,10 +100,9 @@ public function provideDateTimes()
/**
* @dataProvider provideIntervals
*/
public function testDumpInterval($intervalSpec, $invert, $expected)
public function testDumpInterval($intervalSpec, $ms, $invert, $expected)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);

$xDump = <<<EODUMP
DateInterval {
Expand All @@ -117,10 +116,9 @@ public function testDumpInterval($intervalSpec, $invert, $expected)
/**
* @dataProvider provideIntervals
*/
public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected)
public function testDumpIntervalExcludingVerbosity($intervalSpec, $ms, $invert, $expected)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);

$xDump = <<<EODUMP
DateInterval {
Expand All @@ -134,10 +132,9 @@ public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expe
/**
* @dataProvider provideIntervals
*/
public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
public function testCastInterval($intervalSpec, $ms, $invert, $xInterval, $xSeconds)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);
$stub = new Stub();

$cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
Expand Down Expand Up @@ -167,40 +164,42 @@ public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
}
EODUMP;

$this->assertDumpEquals($xDump, $cast["\0~\0interval"]);
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
}

public function provideIntervals()
{
$i = new \DateInterval('PT0S');
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
$ms = ($withMs = \PHP_VERSION_ID >= 70100 && isset($i->f)) ? '.0' : '';

return array(
array('PT0S', 0, '0s', '0s'),
array('PT1S', 0, '+ 00:00:01'.$ms, '1s'),
array('PT2M', 0, '+ 00:02:00'.$ms, '120s'),
array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'),
array('P4D', 0, '+ 4d', '345 600s'),
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'),
array('PT2M', 1, '- 00:02:00'.$ms, '-120s'),
array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'),
array('P4D', 1, '- 4d', '-345 600s'),
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),
array('PT0S', 0, 0, '0s', '0s'),
array('PT0S', 0.1, 0, $withMs ? '+ 00:00:00.100' : '0s', '%is'),
array('PT1S', 0, 0, '+ 00:00:01'.$ms, '1s'),
array('PT2M', 0, 0, '+ 00:02:00'.$ms, '120s'),
array('PT3H', 0, 0, '+ 03:00:00'.$ms, '10 800s'),
array('P4D', 0, 0, '+ 4d', '345 600s'),
array('P5M', 0, 0, '+ 5m', null),
array('P6Y', 0, 0, '+ 6y', null),
array('P1Y2M3DT4H5M6S', 0, 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, 0, '+ 00:02:00'.$ms, null),
array('PT1H60M', 0, 0, '+ 02:00:00'.$ms, null),
array('P1DT24H', 0, 0, '+ 2d', null),
array('P1M32D', 0, 0, '+ 1m 32d', null),

array('PT0S', 0, 1, '0s', '0s'),
array('PT0S', 0.1, 1, $withMs ? '- 00:00:00.100' : '0s', '%is'),
array('PT1S', 0, 1, '- 00:00:01'.$ms, '-1s'),
array('PT2M', 0, 1, '- 00:02:00'.$ms, '-120s'),
array('PT3H', 0, 1, '- 03:00:00'.$ms, '-10 800s'),
array('P4D', 0, 1, '- 4d', '-345 600s'),
array('P5M', 0, 1, '- 5m', null),
array('P6Y', 0, 1, '- 6y', null),
array('P1Y2M3DT4H5M6S', 0, 1, '- 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, 1, '- 00:02:00'.$ms, null),
array('PT1H60M', 0, 1, '- 02:00:00'.$ms, null),
array('P1DT24H', 0, 1, '- 2d', null),
array('P1M32D', 0, 1, '- 1m 32d', null),
);
}

Expand Down Expand Up @@ -349,7 +348,7 @@ public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDa
]
EODUMP;

$this->assertDumpMatchesFormat($xDump, $cast);
$this->assertDumpEquals($xDump, $cast);

$xDump = <<<EODUMP
Symfony\Component\VarDumper\Caster\ConstStub {
Expand All @@ -373,26 +372,26 @@ public function providePeriods()
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';

$periods = array(
array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00', '1) 2017-01-01%a2) 2017-01-02'),
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),
array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02'),
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),

array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-04 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-04 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),

array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-05 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),
array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-05 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),

array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-21 00:00:00', '1) 2017-01-01%a17 more'),
array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),
array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-21 00:00:00.0', '1) 2017-01-01%a17 more'),
array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),

array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00 (included) to 2017-01-03 01:00:00', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) to 2017-01-03 01:00:00.0', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),

array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),

array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) to 2017-01-04 00:00:00', '1) 2017-01-02%a2) 2017-01-03'),
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) to 2017-01-04 00:00:00.0', '1) 2017-01-02%a2) 2017-01-03'),
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
);

if (\PHP_VERSION_ID < 70107) {
Expand All @@ -401,4 +400,15 @@ public function providePeriods()

return $periods;
}

private function createInterval($intervalSpec, $ms, $invert)
{
$interval = new \DateInterval($intervalSpec);
if (\PHP_VERSION_ID >= 70100 && isset($interval->f)) {
$interval->f = $ms;
}
$interval->invert = $invert;

return $interval;
}
}

0 comments on commit aad90fa

Please sign in to comment.