检查日程是否相互重叠?
我需要确定创建的新事件是否与任何现有事件重叠。
事件表
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
在上表中,事件 2 与事件 1 重叠,事件 3 与事件 1 重叠,事件 5 与事件 1 重叠...
当前事件开始时间和现有日程结束时间,当前结束时间和现有日程开始时间可以相同。
我试图使用以下逻辑比较新事件的事件时间是否与现有事件时间重叠,但失败
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
倒置问题并问“两个区间何时不重叠?”可能更简单。答案:当间隔 A 的结束 <= 间隔 B 的开始时,或者当间隔 B 的结束 <= 间隔 A 的开始时。为此创建一个表达式,并将其取反。
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.
您可能会发现以下文章很有用,尤其是
TimePeriodIntersector
类。You may find the following article useful and especially the
TimePeriodIntersector
class.