2015-07-30 02:39:30 -04:00
|
|
|
from datetime import datetime
|
2015-07-16 20:55:36 -04:00
|
|
|
import parsedatetime
|
2015-07-15 13:12:32 -04:00
|
|
|
|
|
|
|
|
2015-07-16 20:55:36 -04:00
|
|
|
def parse_human_datetime(s):
|
|
|
|
"""
|
|
|
|
Use the parsedatetime lib to return ``datetime.datetime`` from human
|
|
|
|
generated strings
|
|
|
|
|
|
|
|
>>> parse_human_datetime("now") <= datetime.now()
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
cal = parsedatetime.Calendar()
|
2015-07-17 02:46:00 -04:00
|
|
|
return dttm_from_timtuple(cal.parse(s)[0])
|
|
|
|
|
|
|
|
|
|
|
|
def dttm_from_timtuple(d):
|
2015-07-16 20:55:36 -04:00
|
|
|
return datetime(
|
|
|
|
d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec)
|
2015-07-17 02:46:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def parse_human_timedelta(s):
|
|
|
|
"""
|
|
|
|
Use the parsedatetime lib to return ``datetime.datetime`` from human
|
|
|
|
generated strings
|
|
|
|
|
|
|
|
>>> parse_human_datetime("now") <= datetime.now()
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
cal = parsedatetime.Calendar()
|
|
|
|
dttm = dttm_from_timtuple(datetime.now().timetuple())
|
|
|
|
d = cal.parse(s, dttm)[0]
|
|
|
|
d = datetime(
|
|
|
|
d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec)
|
|
|
|
return d - dttm
|