gpt4 book ai didi

python - 为什么 python 在 Ubuntu 和 MacOS 上解析日期不同?

转载 作者:行者123 更新时间:2023-11-28 21:54:29 27 4
gpt4 key购买 nike

我有一些代码在同一 python 版本的 Ubuntu 和 MacOS 编译上莫名其妙地工作不同。欢迎任何解释或解决方法。

首先,在 Ubuntu Python 上进行设置。机器以 UTC 运行。在时间字符串中使用 PST 失败,但将 PST 更改为 UTC 则工作正常。

Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date_str = 'Thu Jan 1 00:32:36 PST 2015'
>>> now = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Z %Y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'Thu Jan 1 00:32:36 PST 2015' does not match format '%a %b %d %H:%M:%S %Z %Y'
>>> date_str = 'Thu Jan 1 00:32:36 UTC 2015'
>>> now = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Z %Y')
>>>

现在在 MacOS(在 PDT 中运行)上,python 不关心指定的时区并适用于任何时区:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date_str = 'Thu Jan 1 00:32:36 GMT 2015'
>>> now = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Z %Y')
>>>

GCC 的版本不同,python 2.7.3 的日期戳也不同,但这似乎是非常简单的功能,不依赖于操作系统。

我看到了这样的错误 http://bugs.python.org/issue8957那是松散相关的。这是否更好地作为 python.org 上的错误报告提出?

最佳答案

文档说 %Z is inherently platform specific :

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

关于python - 为什么 python 在 Ubuntu 和 MacOS 上解析日期不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24152610/

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