I need to check if a DateTime-span is overlapping with any existing blocks of time.
My function is supposed to check if anything is overlapping and will run a few hundred times with new start and end date with a span of 10 minutes in between. Spans from the existingTimeBlocks might be anything from 5-60 minutes long.
The first span received will always be the earliest date and the last span received will always be the latest date. The existingTimeBlocks list is also sorted by StartDate
private bool TimeBlockIsOverLapping(DateTime newTimeBlockStart, DateTime newTimeBlockEnd, IEnumerable<TimeBlock> existingTimeBlocks)
{
// The new TimeBlock starts later than latest existing TimeBlock or ends earlier that first existing timeblock so it won't overlap
if (existingTimeBlocks.Count() == 0)
{
return false;
}
// The new TimeBlock is within the scope of old TimeBlocks and might overlap
else
{
// TODO: Insert overlap checks here
}
}
EDIT: Simplified definition of TimeBlock:
public class TimeBlock
{
[Required]
public int Id { get; set; }
[Required]
public DateTime StartTime { get; set; }
[Required]
public DateTime EndTime { get; set; }
[Required]
public int ScheduleId { get; set; }
[Required]
public virtual Schedule Schedule { get; set; }
}
EDIT: Further clarification: existingTimeBlocks can be for example every Monday and Thursday between 9:00 - 12:00 and 13:00-16:00 for 6 weeks, so a timeSpan of 60 minutes can be passed on a monday between 12:00 and 13:00 and be vaild