Let's say I have two timespan ranges
07.00.00 - 18.59.59
19.00.00 - 06.59.59
Now i have a timespan range, let's call it a duration.
public class Duration {
public TimeSpan StartingTime{get; set;}
public TimeSpan EndingTime{get; set;}
}
Which is initialized as
StartingTime 05.00.00
EndingTime 06.00.00
I would like to generate a method where i Pass in the list of timespan ranges, and a new Timespan, where it would return me a correct timespan range.
I have tried modifying Check if a date range is within a date range for TimeSpan, also, tried to convert my timespan ranges to datetime, but it resulted in fail (returning nothing).
Also I've tried going with
var fee = fees.Where(a=> parking.StartTime <= a.EndingTime && parking.EndTime >= a.StartingTime).First();
Where parking would be the duration class, and fees would be a list of timespan ranges.
Also I've looked at c# check if a timespan range is between timespan range and how many hours but this did not result in working solution either.
I am quite frustrated already, and could not think out a solution.
EDIT: Taking example of @NetMage there was a case where 19.40 - 20.35 would result in fail, which I fixed adding another condition.
var fee = fees
.First(r => (r.StartingTime <= r.EndingTime) ?
(r.StartingTime <= parking.StartTime && parking.StartTime <= r.EndingTime) :
(parking.StartTime <= r.EndingTime) ?
(r.StartingTime <= parking.StartTime + oneDay && parking.StartTime <= r.EndingTime)
: (r.StartingTime <= parking.StartTime + oneDay && parking.StartTime <= r.EndingTime + oneDay)
);
EDIT2//
Now I am still having problems with checking the overlap from endTime. Tried using the method that NetMage told, but so far nothing
EDIT3//
To check for the ending part it would be
fees.First(r => (r.StartingTime <= r.EndingTime) ?
(r.EndingTime >= end && end >= r.StartingTime) :
(end >= r.StartingTime) ?
(r.EndingTime >= end + oneDay && end >= r.StartingTime) :
(r.EndingTime >= end + oneDay && end >= r.StartingTime + oneDay)
);
And Finally to check wether it overlaps both ways:
fees.First(r => r.StartingTime >= start && r.EndingTime <= end);