2015-07-01 00:33:43 +02:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from dateutil import tz
|
|
|
|
from pytimeparse import timeparse
|
|
|
|
|
|
|
|
|
|
|
|
def timedelta_type(value):
|
2015-12-14 21:52:22 +01:00
|
|
|
"""Return the :class:`datetime.datetime.DateTime` for a time in the past.
|
2015-07-01 00:33:43 +02:00
|
|
|
|
2016-04-20 19:01:02 +02:00
|
|
|
:param value: a string containing a time format supported by
|
|
|
|
mod:`pytimeparse`
|
2015-07-01 00:33:43 +02:00
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return datetime_seconds_ago(timeparse.timeparse(value))
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_seconds_ago(seconds):
|
|
|
|
now = datetime.datetime.now(tz.tzutc())
|
|
|
|
return now - datetime.timedelta(seconds=seconds)
|