Closed
Description
Symfony version(s) affected: 4.2.3
Description
PHP docs say:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
This means the behaviour is different when the timezone is applied in the constructor or later with setTimezone
.
If you specify the timezone when denormalizing, you'd expect all datetimes to come out in that timezone, regardless of (user) input. This currently does not happen.
How to reproduce
$normalizer = new \Symfony\Component\Serializer\Normalizer\DateTimeNormalizer(
[
\Symfony\Component\Serializer\Normalizer\DateTimeNormalizer::TIMEZONE_KEY => 'UTC'
]
);
$datetime = $normalizer->denormalize('2016-01-27T16:39:26+01:00', \DateTimeInterface::class);
// should be in UTC, but it's exactly the same as input, +01:00
var_dump($datetime->format(\DATE_RFC3339));
Possible Solution
When denormalizing, specify the timezone after having created the \DateTime
instance, not in the constructor.
Additional context
- using in API Platform context means the datetime string is passed directly to this denormalizer, allowing the user to control the timezone of the created
\DateTime
instances