Django: How to make a datetime object aware of the timezone in which it was created?
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 django.utils.timezone.now()
(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)
make_aware(naive) # current timezone, or...
make_aware(naive, timezone=pytz.timezone("Europe/Helsinki")) # ...specific 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
timezone.activate(pytz.timezone("Asia/Kolkata"))
timezone.localtime(timezone.now())