Skip to content

Commit

Permalink
minor #23786 [VarDumper] Remove leading 0 in microseconds of date cas…
Browse files Browse the repository at this point in the history
…ter (maidmaid)

This PR was merged into the 3.4 branch.

Discussion
----------

[VarDumper] Remove leading 0 in microseconds of 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        | /

It looks more like a [engineering notation](https://en.wikipedia.org/wiki/Engineering_notation). We can now better discern **milli**seconds and **micro**seconds.

```php
// before -> after
1.000000 -> 1.0
1.100000 -> 1.100
1.120000 -> 1.120
1.123000 -> 1.123
1.123400 -> 1.123400
1.123450 -> 1.123450
1.123456 -> 1.123456
```

Commits
-------

94956eb Remove leading 0 in ms of date caster
  • Loading branch information
fabpot committed Aug 5, 2017
2 parents 4d80864 + 94956eb commit 9d66ee4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/DateCaster.php
Original file line number Diff line number Diff line change
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:s.u '.($location ? 'e (P)' : 'P')), $title);
$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);

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

Expand All @@ -59,7 +59,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:%S.%F' : '';
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, $i->f) : '';
} else {
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
}
Expand All @@ -79,4 +79,9 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu

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

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));
}
}
17 changes: 12 additions & 5 deletions src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testCastDateTime()

$xDump = <<<'EODUMP'
array:1 [
"\x00~\x00date" => 2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)
"\x00~\x00date" => 2017-08-30 00:00:00.0 Europe/Zurich (+02:00)
]
EODUMP;

Expand All @@ -61,7 +61,7 @@ public function testCastDateTime()
$xDump = <<<'EODUMP'
Symfony\Component\VarDumper\Caster\ConstStub {
+type: 1
+class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)"
+class: "2017-08-30 00:00:00.0 Europe/Zurich (+02:00)"
+value: """
Wednesday, August 30, 2017\n
+%a from now\n
Expand All @@ -81,8 +81,15 @@ public function testCastDateTime()
public function provideDateTimes()
{
return array(
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.000000 Europe/Zurich (+02:00)'),
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.0 Europe/Zurich (+02:00)'),
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.0 +02:00'),

array('2017-04-30 00:00:00.100000', '+02:00', '2017-04-30 00:00:00.100 +02:00'),
array('2017-04-30 00:00:00.120000', '+02:00', '2017-04-30 00:00:00.120 +02:00'),
array('2017-04-30 00:00:00.123000', '+02:00', '2017-04-30 00:00:00.123 +02:00'),
array('2017-04-30 00:00:00.123400', '+02:00', '2017-04-30 00:00:00.123400 +02:00'),
array('2017-04-30 00:00:00.123450', '+02:00', '2017-04-30 00:00:00.123450 +02:00'),
array('2017-04-30 00:00:00.123456', '+02:00', '2017-04-30 00:00:00.123456 +02:00'),
);
}

Expand Down Expand Up @@ -162,7 +169,7 @@ public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
public function provideIntervals()
{
$i = new \DateInterval('PT0S');
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.000000' : '';
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';

return array(
array('PT0S', 0, '0s', '0s'),
Expand Down

0 comments on commit 9d66ee4

Please sign in to comment.