django.utils.timezone.make_aware confusion

147 views
Skip to first unread message

Dan Davis

unread,
Nov 6, 2018, 12:21:32 AM11/6/18
to Django developers (Contributions to Django itself)
Working on ticket #29984, I noticed that the following raises:

from datetime import datetime
import pytz
from django.utils.timezone import make_aware

euberlin = pytz.timezone('Europe/Berlin')s a
dt1 = datetime(2018, 10, 24, tzinfo=euberlin)
dt2 = make_aware(datetime(2018, 10, 24))
assert dt1 == dt2

It is certainly necessary to have make_aware, because datetime may come in without a timezone.  I'm familiar with the various solutions - user picks a timezone in their profile, and/or JavaScript detects the timezone in the browser and provides that to the backend at some point.  I'm even urging a coworker to fix his public Django webapp to implement the latter solution.

However, it is disturbing that the datetimes are not equal.   Can someone explain why not?

Aymeric Augustin

unread,
Nov 6, 2018, 1:56:32 AM11/6/18
to django-d...@googlegroups.com
Hello Dan,

If that assertion fails, I assume the default time zone (the TIME_ZONE setting) isn't Europe/Berlin.


If you change it to dt2 = make_aware(datetime(2018, 10, 24), euberlin), it should work.

Best regards,

-- 
Aymeric.



--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/46a382ef-2b19-4b81-87b8-babb1f93b9cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages