1👍
✅
When working with dates in computers it is best to use a closed-open notation. In other words the first date or datetime is the actual start but the end is listed as the first datetime not included in the set. In other words your data could look like this:
data = [(datetime.datetime(2013,10,10), datetime.datetime(2013,10,16)),
(datetime.datetime(2013,10,17), datetime.datetime(2013,10,24)),
(datetime.datetime(2013,10,24), datetime.datetime(2013,11,1))
]
Once you have your data in closed-open notation then you just need to get the interval of end dates minus the start dates:
for i, d in enumerate(data):
if i > 0:
if d[0] - data[i-1][1] > datetime.timedelta(0):
print("Gap")
If you don’t want to change to using closed-open just check for an interval greater than 1 day, but that will start causing you trouble down the road.
If you are using postgres 9.2+ you may want to look at using Range Types as they make this stuff easy inside the database.
Source:stackexchange.com