Django: How to make a datetime object aware of the timezone in which it was created?


I am running a program that requests ocean tide data from a remote server. The time and date of this tide data is being computed based on my machine’s local time zone. I want to use these local dates and times to create a datetime object, which I will then save in a Django model.

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)

How do I ensure that the datetime object is aware that it was created based on a local time zone, before posting it to Django?

I guess, I would want it to look like this before posting it:

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute, loc_timezone)

How do I get the local timezone of my machine dynamically? And how to I ensure that all users see the time converted to their own local timezone.

Asked By: sgarza62



First, make sure you’re familiar with Django’s documentation on timezones, set USE_TZ = True, and install pytz.

I don’t quite understand where your date is coming from. If it’s coming from the server as part of their data (i.e. it represents when the tides were measured), it should either be in UTC already or you will need to know the time zone they are using. If you’re creating it, then the simplest thing is just to use (which returns a timezone-aware datetime) when you create your model instance.

If you really need to manually create it as you’ve described, follow the usage here or use make_aware():

from django.utils.timezone import make_aware

naive = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
aware = make_aware(naive, timezone=pytz.timezone("Europe/Helsinki"))
# can leave off the timezone parameter if converting to the current timezone

The current timezone will be the default timezone (as defined by the TIME_ZONE setting) unless you’ve used activate() to specify a different one. The default time zone may or may not be the same as the server’s system timezone. Getting the system timezone in a format that pytz can understand is discussed in this answer.

Finally, ensuring that users see the time converted to their local time zone is non-trivial, as discussed here:

The current time zone is the equivalent of the current locale for translations. However, there’s no equivalent of the Accept-Language HTTP header that Django could use to determine the user’s time zone automatically. Instead, Django provides time zone selection functions. Use them to build the time zone selection logic that makes sense for you.

See the examples there for guidance.

from django.utils import timezone
import pytz

Answered By: HimalayanCoder