Skip to content

Commit

Permalink
[Serializer] fixed DateTimeNormalizer to maintain microseconds when a…
Browse files Browse the repository at this point in the history
… different timezone required
  • Loading branch information
rvitaliy authored and nicolas-grekas committed Dec 2, 2018
1 parent 3808566 commit 40b326c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Normalizer/DateTimeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function normalize($object, $format = null, array $context = array())
$timezone = $this->getTimezone($context);

if (null !== $timezone) {
$object = (new \DateTimeImmutable('@'.$object->getTimestamp()))->setTimezone($timezone);
$object = clone $object;
$object = $object->setTimezone($timezone);
}

return $object->format($format);
Expand Down
76 changes: 76 additions & 0 deletions Tests/Normalizer/DateTimeNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,82 @@ public function normalizeUsingTimeZonePassedInContextProvider()
yield array('2016-12-01T09:00:00+09:00', new \DateTimeImmutable('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
}

/**
* @dataProvider normalizeUsingTimeZonePassedInContextAndExpectedFormatWithMicrosecondsProvider
*/
public function testNormalizeUsingTimeZonePassedInContextAndFormattedWithMicroseconds($expected, $expectedFormat, $input, $timezone)
{
$this->assertSame(
$expected,
$this->normalizer->normalize(
$input,
null,
array(
DateTimeNormalizer::TIMEZONE_KEY => $timezone,
DateTimeNormalizer::FORMAT_KEY => $expectedFormat,
)
)
);
}

public function normalizeUsingTimeZonePassedInContextAndExpectedFormatWithMicrosecondsProvider()
{
yield array(
'2018-12-01T18:03:06.067634',
'Y-m-d\TH:i:s.u',
\DateTime::createFromFormat(
'Y-m-d\TH:i:s.u',
'2018-12-01T18:03:06.067634',
new \DateTimeZone('UTC')
),
null,
);

yield array(
'2018-12-01T18:03:06.067634',
'Y-m-d\TH:i:s.u',
\DateTime::createFromFormat(
'Y-m-d\TH:i:s.u',
'2018-12-01T18:03:06.067634',
new \DateTimeZone('UTC')
),
new \DateTimeZone('UTC'),
);

yield array(
'2018-12-01T19:03:06.067634+01:00',
'Y-m-d\TH:i:s.uP',
\DateTimeImmutable::createFromFormat(
'Y-m-d\TH:i:s.u',
'2018-12-01T18:03:06.067634',
new \DateTimeZone('UTC')
),
new \DateTimeZone('Europe/Rome'),
);

yield array(
'2018-12-01T20:03:06.067634+02:00',
'Y-m-d\TH:i:s.uP',
\DateTime::createFromFormat(
'Y-m-d\TH:i:s.u',
'2018-12-01T18:03:06.067634',
new \DateTimeZone('UTC')
),
new \DateTimeZone('Europe/Kiev'),
);

yield array(
'2018-12-01T21:03:06.067634',
'Y-m-d\TH:i:s.u',
\DateTime::createFromFormat(
'Y-m-d\TH:i:s.u',
'2018-12-01T18:03:06.067634',
new \DateTimeZone('UTC')
),
new \DateTimeZone('Europe/Moscow'),
);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
* @expectedExceptionMessage The object must implement the "\DateTimeInterface".
Expand Down

0 comments on commit 40b326c

Please sign in to comment.