Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

Calculate the Available Time of a Shared Resource

Rate me:
Please Sign up or sign in to vote.
4.89/5 (6 votes)
19 Jul 2014CPOL 13K   6   5
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:
Image 1
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:
C#
// ----------------------------------------------------------------------
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:

C#
// ----------------------------------------------------------------------
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions

 
QuestionGreat Article, but this too does not solve the problem of finding gaps in overlapping dates. Pin
Glenn Long 202222-May-23 10:55
Glenn Long 202222-May-23 10:55 
GeneralMy vote of 5 Pin
Mladen Borojevic21-Jul-14 2:27
professionalMladen Borojevic21-Jul-14 2:27 
great
AnswerRe: My vote of 5 Pin
Jani Giannoudis21-Jul-14 11:21
Jani Giannoudis21-Jul-14 11:21 
GeneralThoughts Pin
PIEBALDconsult19-Jul-14 10:17
mvePIEBALDconsult19-Jul-14 10:17 
AnswerRe: Thoughts Pin
Jani Giannoudis19-Jul-14 22:54
Jani Giannoudis19-Jul-14 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.