[Answered ]-Handling per-object timezone settings in Django

1👍

Ref the answer, “Etc/GMT” timezones have their sign reversed. For example, I’m in +0800, it should be 'Etc/GMT-8' instead of 'Etc/GMT+8' (and I suspect that your UTC-8 actually should be "Etc/GMT+8" ):

>>> from django.template import Template, Context
>>> from django.utils.timezone import now
>>> print(Template("""{% load tz %}
localtime: {{ t }}
Etc/GMT-8: {{ t|timezone:"Etc/GMT-8" }}
Etc/GMT+8: {{ t|timezone:"Etc/GMT+8" }}
""").render(Context({'t':now()})))

localtime: Jan. 29, 2013, 11:48 p.m.
Etc/GMT-8: Jan. 29, 2013, 11:48 p.m.
Etc/GMT+8: Jan. 29, 2013, 7:48 a.m.

So, you have to reverse your timezoneHoursOffset

For DB storing, according to the doc, it’s better to store data in UTC.

Also, you need to make sure your javascript generates correct hour offset because of DST. If you can, it’s better to store timezone name and use it accordingly.

👤okm

1👍

from datetime import datetime
from dateutil import zoneinfo

from_zone = zoneinfo.gettz('UTC')
to_zone = zoneinfo.gettz('UK/UK') 

utc = created # your datetime object from the db

# Tell the datetime object that it's in UTC time zone since 
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)

# Convert time zone
eastern_time = utc.aztimezone(to_zone)

Leave a comment