出乎我的意料,它似乎至。
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
而且讓我的對象創建自動添加時間戳(使用auto_now_add
,我相信是的,我沒有得到分心而寫這個答案:-)
我使用TIME_ZONE
設置爲即將不贊成)在我設定的時區顯示創建時間。
如果您想將這些時間轉換爲您網站訪問者的時區,則需要按照您提供的示例做更多的工作。如果您想進行大量時區轉換以在您的網站訪問者時區中顯示時間,那麼我強烈建議您將您的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
我只是試圖在這裏使用manage shell重複你的測試,它似乎沒有......奇怪。 >>> datetime.datetime.now()datetime.datetime(2009,10,15,15,37,47,869000)>>>導入設置>>> settings.TIME_ZONE'Europe/Zurich'>>> datetime.datetime .now()datetime.datetime(2009,10,15,15,37,59,838000)>>> exit()PS D:\> date Thu Oct 15 15:38:25 TST 2009 – monkut 2009-10-15 06:41:07
無論如何,正如您所提到的這聽起來像是一個好主意,只是使用UTC時間。 – monkut 2009-10-15 06:43:28
我不知道這是因爲我發佈的來源106-107行的評論。嘗試運行'hasattr(time,'tzset')'。 – 2009-10-15 09:23:30