Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Calculate the available time of a shared resource

, 19 Jul 2014
Rate this:
Please Sign up or sign in to vote.
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
</t>

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

License

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

Share

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMladen Borojevic21-Jul-14 2:27 
AnswerRe: My vote of 5 PinmemberJani Giannoudis21-Jul-14 11:21 
GeneralThoughts PinmemberPIEBALDconsult19-Jul-14 10:17 
AnswerRe: Thoughts PinmemberJani Giannoudis19-Jul-14 22:54 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 20 Jul 2014
Article Copyright 2014 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid