You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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'])]
publicDateTimeImmutable$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
The text was updated successfully, but these errors were encountered:
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:
The problem:
The DateTimeNormalizer nicely denormalizes this to my
Order
object withorderDate
as DateTimeImmutable class. However my expectation was that theDateTimeImmutable
had the timezoneEurope/Amsterdam
instead ofUTC
.After a bit of digging I found out that inside the DateTimeNormalizer the following is executed during denormalization:
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 denormalizedDateTime
object to the timezone specified by theTIMEZONE_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
The text was updated successfully, but these errors were encountered: