【问题标题】:Why does python parse dates differently on Ubuntu and MacOS?为什么 python 在 Ubuntu 和 MacOS 上解析日期的方式不同?
【发布时间】:2014-06-10 23:48:06
【问题描述】:

我有一些代码在相同 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 上的错误报告询问是否更好?

【问题讨论】:

  • 因为 windows 很痛苦。 xD

标签: python macos ubuntu strptime date-parsing


【解决方案1】:

文档说%Z is inherently platform specific:

对 %Z 指令的支持基于 tzname 中包含的值 以及日光是否真实。因此,它是特定于平台的 除了识别始终已知的 UTC 和 GMT(并且是 被认为是非夏令时时区)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2015-05-26
    相关资源
    最近更新 更多