0
我有很多時間範圍在數據庫中,並建立一個表格插入新的時間範圍。蟒蛇確定時間範圍重疊?
還我寫一個函數之間新的時間範圍檢查overlaping與其他時間範圍
當第一範圍例如是14:00到15:00和第二範圍是16:00至17:00這個功能效果很好,不會返回True。
如果第二個範圍是15:00到16:00雖然兩個範圍沒有任何重疊但是返回True!
def is_overlap(start_date, end_date, obj):
"""
Check overlaping between start_date and end_date
start_date = Start Meeting DateTime
end_date = End Meeting DateTime
obj = Model query objects
"""
for q in obj:
latest_start = max(start_date, q.start_date)
earliest_end = min(end_date, q.end_date)
if (earliest_end - latest_start).days == 0:
if not (earliest_end - latest_start).seconds == 0:
return True
break
if (earliest_end - latest_start).days >= 0:
return True
break
這裏有兩個可能重複的問題https://stackoverflow.com/questions/9044084/efficient-date-range-overlap-calculation-in-python和https://stackoverflow.com/questions/14885188/checking-for -overlap-時間跨度間 – Kasramvd