DJANGO timezone.now is diffefent to datetime.now after we set the timezone config in setting.py

26 views
Skip to first unread message

Lau Louis

unread,
Jan 18, 2018, 8:39:48 AM1/18/18
to Django users
Hi 

I am confused about the timezone setting. I have my OS timezone set to 'Asia/Shanghai', and my MYSQL/Mariadb is also using 'Asia/Shanghai' timezone. So i tried to configure the timezone in DJANGO settings.py

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

And then i aware that after setting this, the time used in the models.py auto_now=True seems to be incorrect. but when i use datetime.datetime.now() the time should be correct, so i use the manage.py shell to check.

Below is the checking result

>>> timezone.get_current_timezone()
<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>
>>> timezone.now()
datetime.datetime(2018, 1, 18, 10, 11, 0, 936572, tzinfo=<UTC>)
>>> datetime.datetime.now()
datetime.datetime(2018, 1, 18, 18, 20, 23, 722825)
>>>

Seems that the timezone.get_current_timezone has returned the right "Asia/Shanghai' timezone, but when i run timezone.now(), it is using UTC time, and when i use datetime.datetime.now(), it is using the correct time.

So it is quite weird behavoir. Can someone tell me if this is a bug or i had missed out some setting?

Louis
Reply all
Reply to author
Forward
0 new messages