繁体   English   中英

将当地时间和时区转换为UTC?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM