我需要使用或strptime或任何此类方法将所有这些转换为python时间对象 -
“8秒前”
“5分钟前”
“11小时前”
“昨天上午11:34”
答案 0 :(得分:1)
似乎parsedatetime
module应解析人类可读的日期/时间文本在这种情况下有效:
#!/usr/bin/env python
from datetime import datetime
import parsedatetime as pdt # $ pip install parsedatetime
cal = pdt.Calendar()
print(datetime.now())
for time_str in ["8 seconds ago", "5 minutes ago", "11 hours ago",
"11:34 AM yesterday"]:
dt, flags = cal.parseDT(time_str)
assert flags
print(dt)
2015-06-26 21:07:19.108521
2015-06-26 21:07:11
2015-06-26 21:02:19
2015-06-26 10:07:19
2015-06-25 11:34:00