令我惊讶的是,它似乎确实如此。
web81:~/webapps/dominicrodger2/dominicrodger$ python2.5 manage.py shell
Python 2.5.4 (r254:67916, Aug 5 2009, 12:42:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import settings
>>> settings.TIME_ZONE
'Europe/London'
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2009, 10, 15, 6, 29, 58, 85662)
>>> exit()
web81:~/webapps/dominicrodger2/dominicrodger$ date
Thu Oct 15 00:31:10 CDT 2009
是的,我在写这个答案时确实分心了:-)
我使用TIME_ZONE 设置,以便我在创建对象时自动添加的时间戳(使用auto_now_add,我相信它很快就会被弃用)在我设置的时区显示创建时间。
如果您想将这些时间转换为网站访问者的时区,则需要根据您提供的示例做更多的工作。如果您想进行大量时区转换以在您的网站访问者的时区中显示时间,那么我强烈建议您将 TIME_ZONE 设置设置为以 UTC 存储时间,因为从长远来看它会让您的生活更轻松运行(您可以只使用 UTC 偏移量,而不必担心夏令时)。
如果您有兴趣,我相信时区是从TIME_ZONE 设置here 设置的。
编辑,根据您的评论,它在 Windows 上不起作用,这是因为 Django 源代码中的以下内容:
if hasattr(time, 'tzset'):
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
os.environ['TZ'] = self.TIME_ZONE
time.tzset()
Windows:
C:\Documents and Settings\drodger>python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> hasattr(time, 'tzset')
False
Linux:
web81:~$ python2.5
Python 2.5.4 (r254:67916, Aug 5 2009, 12:42:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> hasattr(time, 'tzset')
True