[英]Convert local time and timezone to UTC?
我有多個時區和日期時間,我想使用python將其轉換為UTC時區。 例如:
時區 日期時間
GMT + 5 10-03-2019 01:00:00
EST 10-03-2019 01:00:00
CET 10-03-2019 01:00:00
答案應該是:
UTC 09-03-2019 20:00:00
UTC 10-03-2019 05:00:00
UTC 10-03-2019 02:00:00
我無法將時區解析與內置日期時間一起使用,但是使用arrow
模塊可以正常工作。 我假設您的日期是DD / MM / YYYY,而不是MM / DD / YYYY。
import arrow
def parse_date(string):
mydate = arrow.get(string, "ZZZ DD-MM-YYYY HH:mm:SS")
return mydate.to("utc").strftime("%Z %d-%m-%Y %H:%M:%S")
parse_date("GMT+5 10-03-2019 01:00:00")
parse_date("EST 10-03-2019 01:00:00")
parse_date("CET 10-03-2019 01:00:00")
輸出:
'UTC 09-03-2019 20:00:00'
'UTC 10-03-2019 06:00:00'
'UTC 10-03-2019 00:00:00'
可以在這里找到有關格式標記的arrow
文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.