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] Allow custom timezone in DateTimeNormalizer during denormalization #59807

Copy link
Copy link
Open
@frankdekker

Description

@frankdekker
Issue body actions

Description

I am using an API which returns a json response with dates according to format: RFC3339.

Example json response with date in UTC timezone:

{"orderDate":"2025-01-01T12:12:12+00:00"}

An example model I denormalize to:

class Order {
    #[Context([DateTimeNormalizer::TIMEZONE_KEY => 'Europe/Amsterdam'])]
    public DateTimeImmutable $orderDate;
}

The problem:

The DateTimeNormalizer nicely denormalizes this to my Order object with orderDate as DateTimeImmutable class. However my expectation was that the DateTimeImmutable had the timezone Europe/Amsterdam instead of UTC.

After a bit of digging I found out that inside the DateTimeNormalizer the following is executed during denormalization:

DateTimeImmutable::createFromFormat(\DateTimeInterface::RFC3339, "2025-01-01T12:12:12+00:00", new DateTimeZone('Europe/Amsterdam'));

This creates a DateTimeImmutable object in the timezone of the date-string and ignores the specified timezone. From the php.net documentation this is expected behaviour. However from the available normalization options this was a bit confusing.

The solution ( ? )

After some more researching I found this closed issue #31232 and PR #31256 which seems to address this issue. The PR provides a key PRESERVE_CONTEXT_TIMEZONE which sets the denormalized DateTime object to the timezone specified by the TIMEZONE_KEY. However this PR seems to be closed with the comment: "5.1 provides more flexibility", but I can't really find a solution to my problem.

Any objections again if I create a similar PR which adds the PRESERVE_CONTEXT_TIMEZONE context key?

Example

No response

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.