Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer][Bug] In DateTimeNormalizer, apply timezone to \DateTime instance *after* having created it, not in the constructor #31232

Copy link
Copy link
Closed
@dkarlovi

Description

@dkarlovi
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.