When you're not working with Python3 yet and thus cannot make use of the memory efficient range
objects, you could make a namedtuple as shown in the answer you link to (otherwise you could just as well use the new range
objects).
From there, all you need to do is use datetime.date.fromordinal
on the overlapping daterange:
>>> from datetime import date
>>> from collections import namedtuple
>>> Range = namedtuple('Range', ['start', 'end'])
>>> r1 = Range(start=date(2016, 1, 1), end=date(2016, 2, 5))
>>> r2 = Range(start=date(2016, 1, 28), end=date(2016, 2, 28))
>>> latest_start = max(r1.start, r2.start)
>>> earliest_end = min(r1.end, r2.end)
>>> overlap = (earliest_end - latest_start).days + 1
>>> overlapping_dates = [] # default
>>> if overlap > 0:
... overlapping_dates = range(latest_start.toordinal(), earliest_end.toordinal() + 1) # as numbers
... overlapping_dates = [ date.fromordinal(x) for x in overlapping_dates ] # back to datetime.date objects
...
>>> overlapping_dates
[datetime.date(2016, 1, 28),
datetime.date(2016, 1, 29),
datetime.date(2016, 1, 30),
datetime.date(2016, 1, 31),
datetime.date(2016, 2, 1),
datetime.date(2016, 2, 2),
datetime.date(2016, 2, 3),
datetime.date(2016, 2, 4),
datetime.date(2016, 2, 5)]
An approach using set
will work too (one of those approaches is in this answer's edit history), but is usually less efficient, because it has to have all dates in memory, even those that are not in the intersection.