Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.4 and timezones

In django docs, it is written that they can always store the datetime objects in TIME_ZONE provided in settings.

I wanted to ask that is it just sufficient to date time aware objects or do we have to convert them to TIME_ZONE setting? ie if my TIME_ZONE = "America/Los_Angeles" and USE_TZ = True, and I try to save a time zone aware object which is in "UTC", will that work? That is, will django convert that "UTC" timezone object to "America/Los_Angeles" before saving to the db?

like image 960
Rajat Avatar asked Feb 20 '23 21:02

Rajat


1 Answers

I believe that with USE_TZ = True django will save everything to the DB in UTC. Every DateTime object must be timezone aware to be saved. When django retrieves a datetime from the database, it will automatically change it from UTC to an aware time in the time zone indicated by TIME_ZONE in the settings.

So to answer your question, if you have a timezone aware datetime in UTC (let's say 19:00) and you save it, it will save to the DB as 19:00. When you retrieve it, django will make it TZ aware in "America/Los_angelos" so the datetime will now be 12:00.

You can also override the current Timezone setting from what's in settings by calling

from django.utils import timezone
timezone.activate('US/Central')
like image 55
dgel Avatar answered Feb 23 '23 09:02

dgel