I have a question concerning django.utils.timezone
package.
I am at UTC+7 timezone. Thus, if it is 16:00 local time,
timezone.now()
returns 9:00. That is just fine.
However when I do the following:
current_tz = pytz.timezone('Europe/Moscow') # UTC + 4
timezone.activate(current_tz)
I expect timezone.now()
to return 12:00 at 16:00 localtime, but the output remains the same "9:00".
Does timezone.activate() affects on anything at all?
To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default. Time zone support uses zoneinfo , which is part of the Python standard library from Python 3.9.
When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.
utils. tzinfo , is a support system within Django that stores data and time information in UTC in the database. It uses the time-zone-aware- datetime objects internally and translates them to the user's time zone in templates.
timezone.now()
explicitly returns UTC time.
After timezone.activate, then timezone.localtime(timezone.now())
returns the output you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With