How can I parse free-text time intervals in Python, ranging from years to seconds?
This one is new to me, but based on some googling have you tried whoosh?
Edit: There's also parsedatetime:
#!/usr/bin/env python
from datetime import datetime
import parsedatetime as pdt # $ pip install parsedatetime
cal = pdt.Calendar()
for time_str in ['1 second', '2 minutes','3 hours','5 weeks','6 months','7 years']:
diff = cal.parseDT(time_str, sourceTime=datetime.min)[0] - datetime.min
print("{time_str:<10} -> {diff!s:>20} <{diff!r}>".format(**vars()))
Output
1 second -> 0:00:01 <datetime.timedelta(0, 1)>
2 minutes -> 0:02:00 <datetime.timedelta(0, 120)>
3 hours -> 3:00:00 <datetime.timedelta(0, 10800)>
5 weeks -> 35 days, 0:00:00 <datetime.timedelta(35)>
6 months -> 181 days, 0:00:00 <datetime.timedelta(181)>
7 years -> 2556 days, 0:00:00 <datetime.timedelta(2556)>
how about pytimeparse
lib
Returns the time as a number of seconds:
from pytimeparse.timeparse import timeparse
>>> timeparse('33m')
1980
>>> timeparse('2h33m')
9180
>>> timeparse('4:17')
257
>>> timeparse('5hr34m56s')
20096
>>> timeparse('1.2 minutes')
72
source seems to be here https://github.com/wroberts/pytimeparse