dateutil 未正确解析我的时区

问题描述 投票:0回答:1

我正在尝试解析字符串以获取日期时间对象。我正在使用 dateutil 库来解析我的输入字符串。

尝试过这个代码

from dateutil.parser import parse

datetime_str = '2023-06-29T00:15:53Z'      #Z implies UTC timezone
tzinfos = {"Z": -18000}                    #I want the EST timezone which is -5. Hence, -180000
parse(datetime_str, ignoretz=False, tzinfos=tzinfos)

但这给了我输出-

datetime.datetime(2023, 6, 29, 0, 15, 53, tzinfo=tzlocal())

为什么它不遵循我的时区说明?我的 tzinfos 字典专门将“Z”映射到 -18000 的偏移量。但它仍然向我显示 tzlocal()。

**** 更新 ****

好吧,我想我已经集中精力解决这个问题了。如果我将“Z”替换为“BRST”或“CST”之类的内容,并相应地更新 tzinfos 字典,则 tzinfos 会成功更新。那么为什么它给我带来了“Z”的问题。是因为 dateutil.parser 不喜欢单字符时区吗?

python datetime python-dateutil
1个回答
0
投票

UTC 时区缩写在源代码中是特殊大小写的(

UTC
GMT, 
Z
, 
z`):请参阅此处

如果您确实需要使

parserinfo
表示
UTCZONE
以外的含义,则可以子类化
Z
并覆盖
UTC
,但我可能建议不要这样做。

© www.soinside.com 2019 - 2024. All rights reserved.