Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.utils.timezone.activate effect

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?

like image 393
Daria Avatar asked Aug 22 '14 10:08

Daria


People also ask

How do I enable timezone in Django?

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.

What is Use_tz in Django?

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.

What are the time zone utilities used in Django?

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.


1 Answers

timezone.now() explicitly returns UTC time.

After timezone.activate, then timezone.localtime(timezone.now()) returns the output you want.

like image 104
fon Avatar answered Sep 28 '22 22:09

fon