Reputation: 312
I'm having a hard time understanding how timezones works in django, so I looked at the source code in django/utils/timezone.py and I found the following code:
def now():
"""
Returns an aware or naive datetime.datetime, depending on settings.USE_TZ.
"""
if settings.USE_TZ:
# timeit shows that datetime.now(tz=utc) is 24% slower
return datetime.utcnow().replace(tzinfo=utc)
else:
return datetime.now()
And I don't really understand why the provided tzinfo doesn't depend on settings.py's TIME_ZONE.
Shouldn't it be something like that instead?
return datetime.utcnow().replace(tzinfo=get_default_timezone())
This way timezone.now() will really be time zone aware, doesn't it?
Upvotes: 0
Views: 335
Reputation: 8250
When USE_TZ
is enabled, Django stores all datetimes in UTC and uses the TIME_ZONE
setting to display the stored timezone.
From the django's timezone docs:
When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.
Upvotes: 1