django's timezone.now does not show the right time

廉价感情. 提交于 2019-12-03 02:59:23

问题


My server is in London.

In my settings.py I have:

TIME_ZONE = 'Europe/Moscow'
USE_TZ = True

But when I do this:

from django.utils import timezone

print timezone.now().hour

It prints London's time. What do I do wrong?

UPDATE:

>> timezone.now()
datetime.datetime(2013, 4, 16, 12, 28, 52, 797923, tzinfo=<UTC>)

tzinfo = <UTC>, so maybe it prints not London time, but UTC's +0 time? Anyway, how to make django show Moscow time?

Also, when I render template with now = timezone.now().

{{ now.hour }} prints, for example, 12 (London time)

{{ now|date:"G" }} prints 16 (Moscow time)


回答1:


See question #2 in the "Usage" section of the Django docs.

>>> from django.utils import timezone
>>> timezone.localtime(timezone.now())


来源:https://stackoverflow.com/questions/16037020/djangos-timezone-now-does-not-show-the-right-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!