diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index 9736d8e78e4a..3b8b7fe1f0e9 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -88,11 +88,8 @@ public function supportsNormalization($data, string $format = null) */ public function denormalize($data, string $type, string $format = null, array $context = []) { - $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; - $timezone = $this->getTimezone($context); - if (\is_int($data) || \is_float($data)) { - switch ($dateTimeFormat) { + switch ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) { case 'U': $data = sprintf('%d', $data); break; case 'U.u': $data = sprintf('%.6F', $data); break; } @@ -103,6 +100,9 @@ public function denormalize($data, string $type, string $format = null, array $c } try { + $timezone = $this->getTimezone($context); + $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; + if (null !== $dateTimeFormat) { $object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php index 25b7c784fe0e..ee82f319d673 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php @@ -280,6 +280,22 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex $this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']); } + public function testDenormalizeTimestampWithFormatInContext() + { + $normalizer = new DateTimeNormalizer(); + $denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']); + + $this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s')); + } + + public function testDenormalizeTimestampWithFormatInDefaultContext() + { + $normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'U']); + $denormalizedDate = $normalizer->denormalize(1698202249, \DateTimeInterface::class); + + $this->assertSame('2023-10-25 02:50:49', $denormalizedDate->format('Y-m-d H:i:s')); + } + public function testDenormalizeDateTimeStringWithDefaultContextFormat() { $format = 'd/m/Y'; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy