I am completely stumped trying to save user inputed datetime. It saves, but does some sort of timezone conversion. I have tried setting USE_TZ = True and TIME_ZONE = 'UTC', but for some odd reason, Django saves my Datetime but it subttracts 7 from the hour. For example if I input the datetime as '2014-01-29 16:56:00', when I look at the database, I see '2014-01-29 09:56:00-07'. I have read the timezone docs, but I am completely confused as to why this is happening. Any help would be greatly appreciated!
-
is the timezone set correctly in settings.py?sirFunkenstine– sirFunkenstine2014-01-30 01:16:20 +00:00Commented Jan 30, 2014 at 1:16
-
I believe so, I have the setting set to TIME_ZONE = 'UTC'. I guess the bigger question is, will Django always try to do some sort of timezone conversion when saving to a model?tmuzzin– tmuzzin2014-01-30 12:58:10 +00:00Commented Jan 30, 2014 at 12:58
2 Answers
It appears that this is not specifically an issue with Django, but more of an issue with Postgres. Since Django tells Postgres to create a "timestamp with time zone", Postgres stores the timestamp correctly, and sets an offset for when you query that data directly from Postgres, it will display to you the timestamp with the offset applied. But when the data gets back to Django the offset is removed unless you provided an offset. Seems confusing, not sure why it is done this way. For my specific use case I opted to write a custom field to create "timestamp without time zone". Django only creates "timestamp with timezone", so the only way to get around this is to:
alter the table with SQL. Probably not the best choice when deploying to multiple environments if using South.
Create a custom field that creates "timestamp without time zone". This is the route I decided to go. Hopefully it is the correct route.
Thank you to everyone that tried to help me figure this out.
Comments
The Django docs state that the default time zone setting is:
TIME_ZONE = 'America/Chicago'
Here's the link:
https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
They also state a couple of options when setting the TIME_ZONE. One option is to manually set it by name, and here is the link for the TIME_ZONE names list off the Django Doc's: