I have two lists:
lis1 = [(datetime.datetime(2022, 6, 2, 9, 0), datetime.datetime(2022, 6, 2, 9, 30)), (datetime.datetime(2022, 6, 2, 11, 0), datetime.datetime(2022, 6, 2, 12, 10))],
lis2 = [(datetime.datetime(2022, 6, 2, 5, 9), datetime.datetime(2022, 6, 2, 6, 56)), (datetime.datetime(2022, 6, 2, 15, 37), datetime.datetime(2022, 6, 2, 17, 24)), (datetime.datetime(2022, 6, 2, 9, 5), datetime.datetime(2022, 6, 2, 9, 25))]
and I have a event duration of 10 minutes.
I want a common time range that resembles with both the list, meaning it should return a time which is available in both lists and is greater than the event duration for example in the above lists:
datetime.datetime(2022, 6, 2, 9, 5)
is the time which is available.
what i've tried
def compare_lists(list1, list2, duration):
gap_list = []
for x in list1:
for y in list2:
if x[0] < y[0] and y[0] < x[1] < y[1]:
gap_list.append((y[0], x[1]))
if y[0] < x[0] < y[1] and y[0] < x[1] < y[1]:
gap_list.append((y[0], y[1]))
if y[0] < x[0] < y[1] and y[1] > x[1]:
gap_list.append((x[0], x[1]))
if x[0] < y[0] < x[1] and x[0] < y[1] < x[1]:
gap_list.append((y[0], y[1]))
common_time = [t for t in gap_list if (t[1] - t[0]) > duration]
return common_time
duration = timedelta(minutes=20)
ranges = [
[(datetime.datetime(2022, 6, 2, 9, 0), datetime.datetime(2022, 6, 2, 9, 30)),(datetime.datetime(2022, 6, 2, 11, 0), datetime.datetime(2022, 6, 2, 12, 10))],
[(datetime.datetime(2022, 6, 2, 5, 9), datetime.datetime(2022, 6, 2, 6, 56)), (datetime.datetime(2022, 6, 2, 15, 37),datetime.datetime(2022, 6, 2, 17, 24)), (datetime.datetime(2022, 6, 2, 15, 37), datetime.datetime(2022, 6, 2, 9, 25))],
]
ret = compare_lists(ranges[0], ranges[1], duration)
print(ret)
the results is an empty list weather i have an common time or not.