13,453,456 members (42,358 online)
Tip/Trick
alternative version

#### Stats

8.3K views
6 bookmarked
Posted 19 Jul 2014

# Calculate the Available Time of a Shared Resource

, 19 Jul 2014
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 );
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();
new TimeRange( new DateTime( 2014, 7, 19, 08, 15, 0 ), new DateTime( 2014, 7, 19, 14, 00, 0 ) ) );
new TimeRange( new DateTime( 2014, 7, 19, 12, 45, 0 ), new DateTime( 2014, 7, 19, 20, 30, 0 ) ) );
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```

## Share

 Software Developer (Senior) Switzerland
No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 Re: My vote of 5 Jani Giannoudis21-Jul-14 11:21 Jani Giannoudis 21-Jul-14 11:21
 Thoughts PIEBALDconsult19-Jul-14 10:17 PIEBALDconsult 19-Jul-14 10:17
 Re: Thoughts Jani Giannoudis19-Jul-14 22:54 Jani Giannoudis 19-Jul-14 22:54
 Last Visit: 31-Dec-99 18:00     Last Update: 21-Mar-18 11:16 Refresh 1