Check whether the schedule overlaps each other?

社会主义新天地 提交于 2019-12-11 18:17:33

问题


I need to find whether a new event created over laps any of the existing event.

Event Table

ID EventName StartTime EndTime 
1  Event1    11:30AM   12PM    
2  Event2    11:30AM   11:40AM 
3  Event3    11:40AM   12PM    
4  Event4    12PM      12:30PM 
5  Event5    11:30AM   12:30PM 

In above table, event 2 overlaps event 1, event 3 overlaps event 1, event 5 overlaps event 1...

Current event start time and existing schedule end time, current end time and existing schedule start time can be same.

I am trying to compare the whether the new event event time overlaps existing event time using the following logic but it fails

if (currentStartTime == schedule.StartTime)
{
count++;
continue;
}
else if (currentStartTime == schedule.EndTime)
{
continue;
}
else if (currentEndTime == schedule.StartTime)
{
continue;
}
else if (currentStartTime > schedule.StartTime && currentStartTime < schedule.EndTime     && currentEndTime >= schedule.EndTime)
{
count++;
continue;
}
else if (currentEndTime <= schedule.EndTime && currentEndTime > schedule.StartTime &&     currentStartTime < schedule.StartTime)
{
count++;
continue;
}

回答1:


It is probably simpler to invert the problem and ask "when do two intervals not overlap?" Answer: when the end of interval A is <= the start of interval B, or when the end of interval B is <= the start of interval A. Create an expression for this, and negate it.




回答2:


You may find the following article useful and especially the TimePeriodIntersector class.



来源:https://stackoverflow.com/questions/5565564/check-whether-the-schedule-overlaps-each-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!