如何将“Tue Feb 25 2014 00:00:00 GMT+0530 (IST)”转换为python日期时间对象?

suh*_*lvs 4 python timezone datetime python-datetime

我对 dateformat 有一些疑问Tue Feb 25 2014 00:00:00 GMT+0530 (IST)

  • 确实Tue Feb 25 2014 00:00:00意味着GMTIST
  • 是否可以将其转换为 python datetime
  • 是否可以将其转换为DD-MM-YY,HH:MM:SSGMT 格式。

这是我尝试转换成的内容python datetime::

但当我尝试使用时出现错误%z

>>> time_format="%a %b %d %Y %H:%M:%S GMT%z (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT+0530 (IST)"
>>> mydate=datetime.strptime(v,time_format)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S GMT%z (%Z)'
Run Code Online (Sandbox Code Playgroud)

但这有效:

>>> time_format="%a %b %d %Y %H:%M:%S GMT (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT (IST)"
>>> datetime.strptime(v,time_format)    
datetime.datetime(2014, 2, 25, 0, 0)
Run Code Online (Sandbox Code Playgroud)

但仍然不明白任何事情TIMEZONE

Jor*_*ley 5

在系统终端中

easy_install python-dateutil
Run Code Online (Sandbox Code Playgroud)

在 python shell 中

from dateutil import parser as date_parser
print date_parser.parse("Tue Feb 25 2014 00:00:00 GMT+0530 (IST)")
Run Code Online (Sandbox Code Playgroud)

Dateutil 的解析通常可以将您扔给它的任何内容解析为 Python 日期时间。