python - 字符串到日期 - 格式字符串问题

标签 python datetime python-2.4

我需要将以下格式的日期转换为不同的格式以供显示。但在此之前,我尝试将字符串中的日期转换为时间对象,但无法做到这一点。

>>> time.strptime("Thu Mar 13 23:15:13 2014 EDT", '%a %b %d %H:%M:%S %Y %Z')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/_strptime.py", line 293, in strptime
    raise ValueError("time data did not match format:  data=%s  fmt=%s" %
ValueError: time data did not match format:  data=Thu Mar 13 23:15:13 2014 EDT  fmt=%a %b %d %H:%M:%S %Y %Z

经过反复试验,问题是“%Z”引起的,下面工作正常(只是删除了 %Z)

>>> time.strptime("Thu Mar 13 23:15:13 2014", '%a %b %d %H:%M:%S %Y')
(2014, 3, 13, 23, 15, 13, 3, 72, -1)

Python wiki ( https://docs.python.org/2/library/time.html ) 说时区说明符是 %Z,那么这里有什么问题。请帮我找到。

Python版本:2.4.3

最佳答案

来自 Python 文档。 https://docs.python.org/2/library/time.html#time.strptime

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).

这基本上是说 time.strptime() 只会识别 time.tzname 中列出的时区

希望这有帮助

关于python - 字符串到日期 - 格式字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835414/

相关文章:

python - scrapy中如何提取准确的标签

python - 如何使 Python 格式具有一定数量的有效数字?

python - Python 中的符号链接(symbolic link)漏洞利用

python - python decimal/float 中的邪恶

python - 使用 python 在 mySQL BLOB 中插入和检索 PDF

python - 如何用 Python 编写代理服务器?

python - 使用Python读取Google Trends时间序列数据

datetime - Ansible - 将日期增加 'X' 天/分钟

sql - 在 PostgreSQL 中直到当前日期的每月工作日

datetime - 如何将包含日期和时区的字符串解析为DateTime