I used parsedatetime and it worked fine for me. The home page lists some formats that can be processed, for example:
- In 5 minutes
- In 5 minutes
- 2 hours before noon
- 2 days from tomorrow
The main drawback that I discovered is that it does not make sense in time.
In case it's worth it, a wrapper function is used here, which always returns a datetime object, regardless of whether the input string is relative (like all your examples) or fixed:
def parse_datetime(datetime_string): datetime_parser = parsedatetime.Calendar(parsedatetime_consts.Constants()) timestamp = datetime_parser.parse(datetime_string) if len(timestamp) == 2: if timestamp[1] == 0: raise ValueError(u'Failed to parse datetime: %s' % datetime_string) timestamp = timestamp[0] return datetime.fromtimestamp(time.mktime(timestamp))
source share