I'm trying to build a function that will tell me whether or not 2 time range are overlapping. All the existing solutions I have found so far are all about detecting overlapping but over the same day which is no good to me.
I thought something like that would work but it doesn't handle all scenarios:
var current = new Day()
{
Start = TimeSpan.Parse("23:00"),
End = TimeSpan.Parse("02:00")
};
var next = new Day()
{
Start = TimeSpan.Parse("22:45"),
End = TimeSpan.Parse("23:30")
};
current.End = (current.Start > current.End) ?
current.End.Add(new TimeSpan(24, 0, 0)) :
current.End;
next.End = (next.Start > next.End) ?
next.End.Add(new TimeSpan(24, 0, 0)) :
next.End;
bool overlap = (current.Start < next.End && next.Start < current.End);
Debug.WriteLine(overlap);
The provide scenario work as expected but if I try the following it does not work:
var current = new Day()
{
Start = TimeSpan.Parse("23:00"),
End = TimeSpan.Parse("02:00")
};
var next = new Day()
{
Start = TimeSpan.Parse("01:00"),
End = TimeSpan.Parse("04:00")
};
Any suggestions on what I can use to handle all scenarios. It needs to handle overlapping but it also need to handle that anytime a end time is smaller than a start time, it will assume that it is over the next day and the overlapping calculation should take this into account.
Thanks.