Skip to content

Commit 94956eb

Browse files
committed
Remove leading 0 in ms of date caster
1 parent 0f2563c commit 94956eb

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
3232
;
3333

3434
$a = array();
35-
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title);
35+
$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);
3636

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

@@ -59,7 +59,7 @@ private static function formatInterval(\DateInterval $i)
5959
;
6060

6161
if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
62-
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';
62+
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, $i->f) : '';
6363
} else {
6464
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
6565
}
@@ -79,4 +79,9 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu
7979

8080
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
8181
}
82+
83+
private static function formatSeconds($s, $us)
84+
{
85+
return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));
86+
}
8287
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testCastDateTime()
5252

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

@@ -61,7 +61,7 @@ public function testCastDateTime()
6161
$xDump = <<<'EODUMP'
6262
Symfony\Component\VarDumper\Caster\ConstStub {
6363
+type: 1
64-
+class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)"
64+
+class: "2017-08-30 00:00:00.0 Europe/Zurich (+02:00)"
6565
+value: """
6666
Wednesday, August 30, 2017\n
6767
+%a from now\n
@@ -81,8 +81,15 @@ public function testCastDateTime()
8181
public function provideDateTimes()
8282
{
8383
return array(
84-
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.000000 Europe/Zurich (+02:00)'),
85-
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
84+
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.0 Europe/Zurich (+02:00)'),
85+
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.0 +02:00'),
86+
87+
array('2017-04-30 00:00:00.100000', '+02:00', '2017-04-30 00:00:00.100 +02:00'),
88+
array('2017-04-30 00:00:00.120000', '+02:00', '2017-04-30 00:00:00.120 +02:00'),
89+
array('2017-04-30 00:00:00.123000', '+02:00', '2017-04-30 00:00:00.123 +02:00'),
90+
array('2017-04-30 00:00:00.123400', '+02:00', '2017-04-30 00:00:00.123400 +02:00'),
91+
array('2017-04-30 00:00:00.123450', '+02:00', '2017-04-30 00:00:00.123450 +02:00'),
92+
array('2017-04-30 00:00:00.123456', '+02:00', '2017-04-30 00:00:00.123456 +02:00'),
8693
);
8794
}
8895

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

167174
return array(
168175
array('PT0S', 0, '0s', '0s'),

0 commit comments

Comments
 (0)