gpt4 book ai didi

python - 将时区缩写解析为 UTC

转载 作者:太空狗 更新时间:2023-10-30 02:23:43 26 4
gpt4 key购买 nike

<分区>

如何将格式为 Feb 25 2010, 16:19:20 CET 的日期时间字符串转换为 unix 纪元?

目前我最好的方法是使用 time.strptime() 是这样的:

def to_unixepoch(s):
# ignore the time zone in strptime
a = s.split()
b = time.strptime(" ".join(a[:-1]) + " UTC", "%b %d %Y, %H:%M:%S %Z")
# this puts the time_tuple(UTC+TZ) to unixepoch(UTC+TZ+LOCALTIME)
c = int(time.mktime(b))
# UTC+TZ
c -= time.timezone
# UTC
c -= {"CET": 3600, "CEST": 2 * 3600}[a[-1]]
return c

我从其他问题中看到,可以使用 calendar.timegm()pytz 等来简化这个问题,但这些并不能处理缩写时区。

我想要一个需要最少多余库的解决方案,我喜欢尽可能多地使用标准库。

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