Calculate the Available Time of a Shared Resource






4.89/5 (6 votes)
Get available time periods, restricted by a maximum usage count.
Introduction
Shared resources are often limited by their usage count, like jobs per person or beds per room. This tip demonstrates based on the Time Period Library for .NET, how you can calculate the available time of such a resource.
Algorithm
The calculation of the resource occupation is based on the
TimeLineMomentCollection:

Iterating through the time line moments, we detect the period
Start
when the cumulated BalanceCount
exceeds the required usage count. The period End
is arrived, when the balance counter falls below the usage count:// ----------------------------------------------------------------------
public ITimePeriodCollection GetOccupiedPeriods<t>( ITimePeriodCollection periods, int usageCount )
where T : ITimeRange, new()
{
TimePeriodCollection occupiedPeriods = new TimePeriodCollection();
if ( periods.Count > 0)
{
TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection( periods );
DateTime? start = null;
int balance = 0;
foreach ( ITimeLineMoment timeLineMoment in timeLineMoments )
{
balance += timeLineMoment.BalanceCount;
if ( !start.HasValue )
{
// start
if ( balance >= usageCount )
{
start = timeLineMoment.Moment;
}
}
else if ( balance < usageCount )
{
// end
T period = new T();
period.Setup( start.Value, timeLineMoment.Moment );
occupiedPeriods.Add( period );
start = null;
}
}
}
return occupiedPeriods;
} // GetOccupiedPeriods
Using the Code
The following example calculates the occupation periods (step one) of a resource using GetOccupiedPeriods
. Based on the occupied periods, the library tool TimeGapCalculator
(step two) calculates the available time periods of the resource:
// ----------------------------------------------------------------------
public void FreePeriodFinder()
{
// resource
ITimePeriodCollection resourcePeriods = new TimePeriodCollection();
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 08, 15, 0 ), new DateTime( 2014, 7, 19, 14, 00, 0 ) ) );
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 12, 45, 0 ), new DateTime( 2014, 7, 19, 20, 30, 0 ) ) );
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 18, 00, 0 ), new DateTime( 2014, 7, 19, 23, 00, 0 ) ) );
// step 1: calculate occupied periods
ITimePeriodCollection occupiedPeriods = GetOccupiedPeriods<TimeRange>( resourcePeriods, 2 );
foreach ( ITimePeriod occupiedPeriod in occupiedPeriods )
{
Console.WriteLine( "occupied: {0}", occupiedPeriod );
}
// > occupied: 19.07.2014 12:45:00 - 14:00:00 | 0.01:15
// > occupied: 19.07.2014 18:00:00 - 20:30:00 | 0.02:30
// step 2: calculate free periods
ITimePeriod freePeriodLimits = resourcePeriods; // can be any other period like week, month...
ITimePeriodCollection freePeriods = new TimeGapCalculator<TimeRange>().
GetGaps( occupiedPeriods, freePeriodLimits );
foreach ( ITimePeriod freePeriod in freePeriods )
{
Console.WriteLine( "free: {0}", freePeriod );
}
// > free: 19.07.2014 08:15:00 - 12:45:00 | 0.04:30
// > free: 19.07.2014 14:00:00 - 18:00:00 | 0.04:00
// > free: 19.07.2014 20:30:00 - 23:00:00 | 0.02:30
} // FreePeriodFinder