I have a List<ObjectInstance>
.
The ObjectInstance
is a class, which has two TimeSpan attributes: StartTime and EndTimes.
The start and end times can be anywhere between 8am and 10pm. Anytimes after 5pm is off peak time, else it is a peak time.
I need to calculate the total peak time and the total off peak time in the list.
What is the best approach to take to do this?
The way I did was basic really: I wrote functions similar to the one below, which have a lot of if conditions on where the peakTimeStart is, relative to the interval start and end times but I feel there must be a better way to do this; perhaps using LINQ, or some extension methods to call on the list?
public static double CalculateTimeIntervalPeakHours(TimeSpan intervalStart, TimeSpan intervalEnd, TimeSpan peakTimeStart)
{
double peakHours = 0.0;
// some logic here to find where the interval starts & ends relative to the peakTimeStart!
return peakHours;
}