Python與Django的時區問題

來源:互聯網
上載者:User
在編碼中牽扯到時間問題的時候,總是容易被時區問題搞混,一直以來,都是反覆實驗應付過去,今天終於搞清楚了個中緣由,一個心結也得以化解。

Python 的時區問題

datetime.today() / datetime.now()

這兩個函數獲得的是當前的系統時間,但得到的datetime對象中的tzinfo是空的,即使系統中設定了時區。

datetime.utcnow()

這個函數獲得當前的utc時間,應該是根據當前系統時間和時區來計算的。

例如系統時間為14:00,時區為 Asia/Shanghai (北京時間),utcnow返回時間為 6:00。同樣,得到的對象中的tzinfo 為空白。

環境變數 TZ 對以上函數的影響:

當系統中設定了環境變數 TZ 的時候,或者在python中設定了 os.environ[‘TZ’] 的時候,上面的函數擷取的時間便是TZ對應時區的時間。其實這裡可以認為 TZ 影響的不是這些函數,而是影響的系統時間,這可以從date命令的返回結果看出。datetime.now() 與 date命令返回的結果總是一致的。

Django的時區問題

明白了上面幾個python中的函數,django的時區問題看起來就簡單了。

在django的setting中,有一個設定是 TIME_ZONE, 來設定程式中使用的時區。

從django的文檔中得知,TIME_ZONE的作用就是改變 os.environ[‘TZ’] ,但改變os.environ[‘TZ’] 並不會改變系統內容變數 TZ , 因此,如果 TIME_ZONE 的設定於系統時區設定不一致,則在程式中 datetime.now() 獲得的時間就與 date 命令的時間不一致了。

因此,TIME_ZONE 應該設定為程式希望使用的時區。對於一個本地的程式,TIME_ZONE 設定為與系統時區一樣即可;而對於一個國際化的應用,TIME_ZONE 最好設定為UTC,在顯示的時候再根據目前使用者所在的時區做調整。

手冊

classmethod datetime.now([tz])

Return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).

Else tz must be an instance of a class tzinfo subclass, and the current date and time are converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)). See also today(), utcnow().

classmethod datetime.utcnow()

Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naivedatetime object. See also now().

時區轉碼

import pytz....#dt the datetime vardt.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Shanghai'))
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.