Time zone issue with MySQL timestamp fields
|Reported by:||mwaterfall||Owned by:||nobody|
|Severity:||Normal||Keywords:||mysql timezone timestamp datetime datetimefield|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
MySQL timestamp fields are supported by Django ORM's DateTimeField, however if time zone support is enabled Django is not ensuring the Python datetime objects (from MySQLdb) have a default tzinfo applied to them (UTC) in the same way that it does when dates come from a datetime MySQL field.
Because of this, when saving the model after it has just been read from the database, Django throws a naive datetime / time zone warning from within the field's get_prep_value method:
RuntimeWarning: DateTimeField received a naive datetime (2012-11-17 16:08:44) while time zone support is active.
I have tracked the problem down to Django's MySQL cursor wrapper class, which applies alternative conversion functions for certain field types. The parse_datetime_with_timezone_support function ensures Python datetime objects always have a default tzinfo if time zone support is enabled. The FIELD_TYPE.DATETIME type gets handled by this function, however FIELD_TYPE.TIMESTAMP doesn't, and the naive datetime objects slip through and produce errors.
Change History (4)
Changed 17 months ago by mwaterfall
comment:1 Changed 17 months ago by aaugustin
- Easy pickings unset
- Needs documentation unset
- Needs tests set
- Patch needs improvement set
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Bug