Efficient date range overlap calculation in python?
Solution 1:
- Determine the latest of the two start dates and the earliest of the two end dates.
- Compute the timedelta by subtracting them.
- If the delta is positive, that is the number of days of overlap.
Here is an example calculation:
>>> from datetime import datetime
>>> from collections import namedtuple
>>> Range = namedtuple('Range', ['start', 'end'])
>>> r1 = Range(start=datetime(2012, 1, 15), end=datetime(2012, 5, 10))
>>> r2 = Range(start=datetime(2012, 3, 20), end=datetime(2012, 9, 15))
>>> latest_start = max(r1.start, r2.start)
>>> earliest_end = min(r1.end, r2.end)
>>> delta = (earliest_end - latest_start).days + 1
>>> overlap = max(0, delta)
>>> overlap
52
Solution 2:
Function calls are more expensive than arithmetic operations.
The fastest way of doing this involves 2 subtractions and 1 min():
min(r1.end - r2.start, r2.end - r1.start).days + 1
compared with the next best which needs 1 subtraction, 1 min() and a max():
(min(r1.end, r2.end) - max(r1.start, r2.start)).days + 1
Of course with both expressions you still need to check for a positive overlap.