🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@frankdekker
Copy link
Contributor

@frankdekker frankdekker commented Apr 5, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #59807
License MIT

By default the DateTimeNormalizer will denormalize timestamps and date strings that include a timezone to DateTime objects with respective timezone UTC or the timezone from the date string. Even with the context key TIMEZONE_KEY set, the timezone from the input superceeds the context timezone.

This PR allows to set PRESERVE_CONTEXT_TIMEZONE_KEY context to true (default: false), which in combination with TIMEZONE_KEY will set the Timezone of the denormalized DateTimes to the given timezone. The timezone from the input will be overwritten.

Documentation

symfony/symfony-docs#20860

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [WIP][Serializer] Allow custom timezone in DateTimeNormalizer during denormalization [Serializer] [WIP] Allow custom timezone in DateTimeNormalizer during denormalization Apr 5, 2025
@frankdekker frankdekker changed the title [Serializer] [WIP] Allow custom timezone in DateTimeNormalizer during denormalization [Serializer] Allow custom timezone in DateTimeNormalizer during denormalization Apr 5, 2025
@frankdekker
Copy link
Contributor Author

Possibility of lowering complexity:

There are currently 2 similar blocks of code for $dateTimeFormat and $defaultDateTimeFormat. The first with a bit more error handling. These blocks could be combined with using the dateTimeFormat in something like $dateTimeFormat ?? $defaultDateTimeFormat ?? null.

I was hesistant to do this immediately because i have no idea why there is different error handling for the context $dateTimeFormat.

@OskarStark OskarStark changed the title [Serializer] Allow custom timezone in DateTimeNormalizer during denormalization [Serializer] Allow custom timezone in DateTimeNormalizer during denormalization Apr 7, 2025
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@mdriessen
Copy link

Is there anything I can do to help this being merged in Symfony 7.4?

@frankdekker frankdekker force-pushed the PR_59807 branch 2 times, most recently from 551c4f8 to 8b9a4f9 Compare November 1, 2025 09:30
@frankdekker frankdekker force-pushed the PR_59807 branch 2 times, most recently from e95ddf0 to dacfe45 Compare November 1, 2025 10:04
@nicolas-grekas nicolas-grekas changed the title [Serializer] Allow custom timezone in DateTimeNormalizer during denormalization [Serializer] Allow forcing timezone in DateTimeNormalizer during denormalization Nov 1, 2025
@frankdekker
Copy link
Contributor Author

@nicolas-grekas Thanks for the feedback. The branch is now up-to-date again with 7.4. There are 3 failings tests but I don't see the relation to my changes though.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost forgot: Mapping/Loader/schema/serialization.schema.json needs also an update

* @throws InvalidArgumentException
*/
public function withTimezone(\DateTimeZone|string|null $timezone): static
public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static
public function withTimezone(\DateTimeZone|string|null $timezone, bool $force = false): static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about setting it to true|false but in the implementation there's a "default context" and given context. Basicly you could set default context to ->withTimezone(null, true), and in your custom context ->withTimezone('UTC, null). In this way for every context the timezone will be forced, but per serializer context you can enforce different timezones.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 nits and GTM, thanks

@nicolas-grekas
Copy link
Member

Thank you @frankdekker.

@nicolas-grekas nicolas-grekas merged commit c5d0e49 into symfony:7.4 Nov 1, 2025
2 checks passed
This was referenced Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Serializer] Allow custom timezone in DateTimeNormalizer during denormalization

6 participants