[Flow] Doctrine losing datetimezone and remaps strict to systems date.timezone
Carsten Bleicker
carsten at bleicker.de
Wed Feb 25 17:59:45 CET 2015
If you are using DateTime properties in a Model using simply new DateTime() will lead to corrupt data
because they are created with new as UTC. Storing this to db and read it again you get a DateTime object
in you systems date.timezone. There is no recalculation.
So what you put in and what you get out has a lack of the timezone diff between UTC and Europe/Berlin.
@see http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html#default-timezone-gotcha
this means in practice:
request contains a datetime string maybe from an event datetimetz picker:
incoming: "2008-11-16 18:00:00 UTC"
its persisted as "2008-11-16 18:00:00" (note missing datetimezone infos but still 18:00:00)
reading this again from persistence results in: "2008-11-16 18:00:00 Europe/Berlin"
so we lost 1 hour :(
2008-11-16 18:03:30 UTC
2008-11-16 18:03:30 Europe/Berlin
Solution is maybe simple:
I think the DateTime Converter should follow exaclty this Doctrine Behaviour.
It respects incoming tz info but internaly recalculates to systems date.timezone.
So if you debug a converted String to Datetime result you shlould always see it in a recalculated datetime object holding systems date.timezone.
The example above doesn't have a timediff of 1 hour anymore after reading from db.
incoming: "2008-11-16 18:00:00 UTC"
its persisted as "2008-11-16 19:00:00" (note missing datetimezone infos)
reading this again from persistence results in: "2008-11-16 19:00:00 Europe/Berlin"
2008-11-16 18:03:30 UTC
2008-11-16 19:03:30 Europe/Berlin
Converting per default to systems date.timezone should be skipped if the the annotation is used to store also the timezone in db: @ORM\Column(type="datetimetz")
But beware using because of:
http://derickrethans.nl/storing-date-time-in-database.html
http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html#handling-different-timezones-with-the-datetime-type
So i would prefer:
In case of doctrine type of "datetimetz" there is no recalculation needed in flows DateTime Converter.
If case of doctrine type of "datetime" there is converting needed to systems date.timezone
At the end flows DateTimeConverter is more a generic "SystemDateTimeConverter" wich returns allways a system matching DateTime.
We should also add some information to the documentation that it is not a good idea to use just new \DateTime().
Use new \DateTime('now', new \DateTimeZone(ini_get('date.timezone'))) instead to be in sync with doctrines behaviour.
Or maybe we can introduce an extended SystemDateTime() object wich allways creates with systems DateTimeZone on new SystemDateTime('now');
More information about the Flow
mailing list