检查时间表是否重叠? [英] Check whether the schedule overlaps each other?

查看:60
本文介绍了检查时间表是否重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定是否有一个新事件在任何现有事件上发生.

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

事件表

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重叠...

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;
}

推荐答案

将问题反转并询问什么时候两个间隔重叠?"可能更简单.答案:当间隔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.

这篇关于检查时间表是否重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆