aun*_*uny 49 python datetime timestamp rfc3339
我正在尝试使用strptime方法将格式为"2012-07-24T23:14:29-07:00"的时间戳转换为python中的datetime对象.问题在于结束时的时间偏移(-07:00).没有偏移我可以成功做到
time_str = "2012-07-24T23:14:29"
time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S')
Run Code Online (Sandbox Code Playgroud)
但随着我尝试的抵消
time_str = "2012-07-24T23:14:29-07:00"
time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S-%z').
Run Code Online (Sandbox Code Playgroud)
但是它给出了一个Value错误,说"z"是一个糟糕的指令.
任何解决方案的想法?
Mar*_*ers 61
Python 2 strptime()函数确实不支持%z时区格式(因为底层time.strptime()函数不支持它).您有两种选择:
使用strptime以下方法解析时忽略时区:
time_obj = datetime.datetime.strptime(time_str[:19], '%Y-%m-%dT%H:%M:%S')
Run Code Online (Sandbox Code Playgroud)使用该dateutil模块,它的解析函数确实处理时区:
from dateutil.parser import parse
time_obj = parse(time_str)
Run Code Online (Sandbox Code Playgroud)命令提示符下的快速演示:
>>> from dateutil.parser import parse
>>> parse("2012-07-24T23:14:29-07:00")
datetime.datetime(2012, 7, 24, 23, 14, 29, tzinfo=tzoffset(None, -25200))
Run Code Online (Sandbox Code Playgroud)
您还可以升级到Python 3.2或更高版本,其中时区支持已经改进到%z可行的点,前提是您:从输入中移除了最后一个,并且-从以下位置开始%z:
>>> import datetime
>>> time_str = "2012-07-24T23:14:29-07:00"
>>> datetime.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S%z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '2012-07-24T23:14:29-07:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
>>> ''.join(time_str.rsplit(':', 1))
'2012-07-24T23:14:29-0700'
>>> datetime.datetime.strptime(''.join(time_str.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
datetime.datetime(2012, 7, 24, 23, 14, 29, tzinfo=datetime.timezone(datetime.timedelta(-1, 61200)))
Run Code Online (Sandbox Code Playgroud)
jfs*_*jfs 18
在Python 3.7+中:
from datetime import datetime
time_str = "2012-07-24T23:14:29-07:00"
dt_aware = datetime.fromisoformat(time_str)
print(dt_aware.isoformat('T'))
# -> 2012-07-24T23:14:29-07:00
Run Code Online (Sandbox Code Playgroud)
在Python 3.2+中:
from datetime import datetime
time_str = "2012-07-24T23:14:29-0700"
dt_aware = datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S%z')
print(dt_aware.isoformat('T'))
# -> 2012-07-24T23:14:29-07:00
Run Code Online (Sandbox Code Playgroud)
注意:在Python 3.7之前,此变体不支持:该-0700部分(rfc 3339允许这两种格式).请参阅datetime:添加解析RFC 3339日期和时间的功能.
在较旧的Python版本(如Python 2.7)上,您可以手动解析utc偏移量:
from datetime import datetime
time_str = "2012-07-24T23:14:29-0700"
# split the utc offset part
naive_time_str, offset_str = time_str[:-5], time_str[-5:]
# parse the naive date/time part
naive_dt = datetime.strptime(naive_time_str, '%Y-%m-%dT%H:%M:%S')
# parse the utc offset
offset = int(offset_str[-4:-2])*60 + int(offset_str[-2:])
if offset_str[0] == "-":
offset = -offset
dt = naive_dt.replace(tzinfo=FixedOffset(offset))
print(dt.isoformat('T'))
Run Code Online (Sandbox Code Playgroud)
ValueError: 'z' is a bad directive in format...(注意:在我的情况下,我必须坚持使用python 2.7)
我在解析提交日期时遇到了类似的问题,该提交日期的输出git log --date=iso8601实际上不是ISO8601格式的(因此--date=iso8601-strict在以后的版本中增加了)。
由于我正在使用django,因此可以在此处利用这些实用程序。
https://github.com/django/django/blob/master/django/utils/dateparse.py
>>> from django.utils.dateparse import parse_datetime
>>> parse_datetime('2013-07-23T15:10:59.342107+01:00')
datetime.datetime(2013, 7, 23, 15, 10, 59, 342107, tzinfo=+0100)
Run Code Online (Sandbox Code Playgroud)
相反,strptime您可以使用自己的正则表达式。