Time Zones, did I get it right?

Let’s say I have an application that manages events. Events have a datetime field for start and for end time. I want the datetime fields to be aware. I’ve read through the documentation on it, but it was a lot, and so I would like to get some feedback on my concept.

Extending the user
I will extend the user model so that each user can set their timezone.

I set the time zone to UTC so that internally, Django will use UTC.

Templates in Admin
I include the tags for date localization in the templates (if they’re not there already). The timezone for display will be taken from request.user.profile. So that would mean that when a user has selected “Europe/Berlin” it would add two hours (Summertime) to the UTC timestamp in the database when the value is displayed.

Templates for the public part
For a list of events that I display publicly, I want the event times to be displayed in the local time of the event but also in the local time of the visitor. So this would be a case where I localize the UTC database value to the timezone of the event’s location and to the visitor’s location.

Any suggestions?

I’m not sure that this is one of those topics where there’s one definitively “right” answer. (There are however probably any number of “wrong” answers, but this isn’t one of them.)

But, for what it’s worth, given that set of identified requirements, you’re taking the approach I would take.

(Whether that gives you a “warm & fuzzy” feeling or “scares you to the core” is up to you. :grin:)


1 Like

You’re doing it right, handle all time in UTC, save timezones in a field and do the conversion in the front end.

Btw, if you’re using postgresql DateTimeRangeField is a good option for saving start/end times.

1 Like

I agree with what the others have said about your strategy for saving times in UTC. I was going down the path of adding a timezone attribute to a user’s profile too, then I found tz_detect as a Django app to look up the user’s timezone via some JavaScript when they access the page.

Before you go too far with storing the timezone of a user, tz_detect might an interesting option worth considering. One aspect that I like about it is that my app will still show the right times even if the user is not in their home timezone (e.g., while traveling).

I’m sure the app is not perfect, but it’s pretty cool solution. You can find it on GitHub at https://github.com/adamcharnock/django-tz-detect.

Or possibly both - because, unfortunately, in a corporate environment, your “IP-detected location” may have very little to do with your physical location.

When I’m connected to work through our VPN, every web site thinks I’m hundreds of miles away from where I’m sitting.