gpt4 book ai didi

python - 如何在 Python 中找到不同时区的时间差异?

转载 作者:太空狗 更新时间:2023-10-30 00:25:42 26 4
gpt4 key购买 nike

我正在尝试计算两个日期/时间之间的差异(以秒为单位),格式如下:

2010-05-11 17:07:33 UTC

2010-05-11 17:07:33 美国东部时间

time1 = '2010-05-11 17:07:33 UTC'
time2 = '2010-05-11 17:07:33 EDT'
delta = time.mktime(time.strptime(time1,"%Y-%m-%d %H:%M:%S %Z"))-\
time.mktime(time.strptime(time2, "%Y-%m-%d %H:%M:%S %Z"))

我得到的问题是EDT无法识别,具体报错是

ValueError: time data '2010-05-11 17:07:33 EDT' does not match format '%Y-%m-%d %H:%M:%S %Z'

最佳答案

查看 pytz世界时区定义库。

This library allows accurate and cross platform timezone calculations using Python 2.3 or higher. It also solves the issue of ambiguous times at the end of daylight savings, which you can read more about in the Python Library Reference (datetime.tzinfo).

它利用了 tz database ,其中应该包括 EDT,并允许您执行所需的计算(并且可能比您当前的实现更可靠和准确)。

关于python - 如何在 Python 中找到不同时区的时间差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2828158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com