An "unfinished interval" can be expressed with datetime
(and possibly subclass it to contain a flag stating start
or end
) - that's what I would do for a quick implementation.
When you have the interval's corresponding end or start), get the timedelta
using their difference:
>>> from datetime import datetime, timedelta
# represent infinite time from 1/1/2014 onwards
>>> start = datetime(2014, 1, 1)
# stop that time
>>> end = datetime(2015, 1, 1)
>>> interval = end - start
>>> interval
datetime.timedelta(365)
If you have the time to do this "the right way" you can create a speacialized timedelta
-like class with two datetime
objects (start and end). If one of them is missing you can assume it's an infinite timedelta and treat it as such.
Example:
from datetime import timedelta, datetime
class SpecialTimedelta(object):
def __init__(self, start=None, end=None):
self.start = start
self.end = end
if start and not isinstance(start, datetime):
raise TypeError("start has to be of type datetime.datetime")
if end and not isinstance(start, datetime):
raise TypeError("end has to be of type datetime.datetime")
def timedelta(self):
if not (self.start and self.end):
# possibly split cases
return "infinite"
# alternatives:
# return self.start or self.end
# raise OverflowError("Cannot quantiate infinite time with timedelta")
return self.end - self.start
print SpecialTimedelta(datetime(1986, 1, 1), datetime(2015, 1, 1)).timedelta()
# out: 10592 days, 0:00:00
print SpecialTimedelta(datetime(1986, 1, 1)).timedelta()
# out: infinite