6

Is there a Python module to interpret fuzzy timestamps like the date command in unix:

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

The closest I have found so far is dateutil.parser, which fails for the above example.

thanks

hoju
  • 28,392
  • 37
  • 134
  • 178
  • 1
    What is your idea of "fuzzy" in the example? The fact that you are specifying a relative offset? Or the syntax of doing this ("2 minutes ago")? Would "N - 00:02:00" be fuzzy as well? Or is it because you include the notion of "now", which is ever changing? – ThomasH Aug 11 '09 at 08:15

3 Answers3

9

Check out this open source module: parsedatetime

APerson
  • 8,140
  • 8
  • 35
  • 49
Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
2

dateparser

Usage:

>>> 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)
idanp
  • 973
  • 12
  • 18
1

I have been dabbling with this using pyparsing - you can find my latest attempt here. It works for these test cases:

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
PaulMcG
  • 62,419
  • 16
  • 94
  • 130