1

I've the following settings:-

TIME_ZONE = 'Asia/Kolkata'  
USE_I18N = True
USE_L10N = True
USE_TZ = True

Also, I've set auto_now = True in my DateTimeField's. However, the datetime that gets saved in the column in of UTC (ie -5:30 hours).

I have set my system date as per the Asia/Kolkata timezone. Also,

>>> from django.utils import timezone
>>> from datetime import datetime
>>>
>>> datetime.now()
datetime.datetime(2019, 7, 13, 17, 40, 1, 505516)  # this is right
>>> timezone.now()
datetime.datetime(2019, 7, 13, 12, 10, 6, 496772, tzinfo=<UTC>)  # this is not what I expect.

Why is there a discrepency in timezone.now() even when TIME_ZONE is rightly set??

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • 2
    The very first sentence in the [official documentation](https://docs.djangoproject.com/en/2.2/topics/i18n/timezones/): "When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms." – ipaleka Jul 13 '19 at 12:37
  • Shall I set `USE_TZ` to `False`? – Praful Bagai Jul 13 '19 at 12:45
  • If you don't have any user with a location in a different time zone than your server, then yes - or simply delete that line as it defaults to False. – ipaleka Jul 13 '19 at 12:49

1 Answers1

2

TIME_ZONE is only used in the templates and forms etc. If USE_TZ is set to True (which is recommended) then the timezone.now() will always be set in UTC (time aware) https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-TIME_ZONE

"..... this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms."

This is why {{ now|date:"G" }} prints 16 (Moscow time) prints the Moscow time!

Another note is timezone.now() in a normally configured Django app will pick up whatever TIME_ZONE is set to in the settings ('America/New_York' in example below). But can be overridden as well (see below to Paris for example in a middleware or directly in a view for that request)

In [27]: timezone.localtime(timezone.now())

Out[27]: datetime.datetime(2021, 2, 23, 1, 53, 49, 793743, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)

In [29]: timezone.activate(pytz.timezone("Europe/Paris"))

In [30]: timezone.localtime(timezone.now())

Out[30]: datetime.datetime(2021, 2, 23, 7, 54, 19, 21898, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
chachra
  • 769
  • 6
  • 7