使用Python解析模糊时间戳

时间:2009-08-11 06:29:12

标签: python date parsing timestamp

是否有Python模块来解释模糊时间戳,例如unix中的date命令:

> date -d "2 minutes ago"
Tue Aug 11 16:24:05 EST 2009

到目前为止,我发现的最接近的是dateutil.parser,但上述示例失败了。

感谢

3 个答案:

答案 0 :(得分:8)

查看此开源模块:parsedatetime

答案 1 :(得分:1)

我一直在尝试使用pyparsing - 你可以找到我最近的尝试here。它适用于这些测试用例:

today
tomorrow
yesterday
in a couple of days
a couple of days from now
a couple of days from today
in a day
3 days ago
3 days from now
a day ago
now
10 minutes ago
10 minutes from now
in 10 minutes
in a minute
in a couple of minutes
20 seconds ago
in 30 seconds
20 seconds before noon
20 seconds before noon tomorrow
noon
midnight
noon tomorrow

答案 2 :(得分:1)

dateparser

用法:

>>> import dateparser
>>> dateparser.parse('2 minutes ago')
datetime.datetime(2018, 11, 27, 13, 44, 54, 993170)
>>> dateparser.parse('yesterday at 15:12')
datetime.datetime(2018, 11, 26, 15, 12)