开发者

Iterating over discrete periods of time within another period of time

I have a following table of periods which describes how often to ..say.. feed my fish:

        -------------------------------------开发者_如何学C-------------------
Period: Jan  Feb  March  April  May  Jun  Jul ... n - 1 .... n
        --------------------------------------------------------
Val_1:   5    2    3      6      3    2    4       x         x
Val_2    ...
        --------------------------------------------------------

And I have a period given with two DateTimes, start and end, ie:

DateTime start = new DateTime(2010, 3, 11);
DateTime end = new DateTime(2012, 7, 12);

..in which time the feeding process occurs. How can I get the values from the table in every period in correlation with the period given by start and end ?

For example, the period given by start and end is 2.5 years, but my table only describes 12 months. How can I loop over every period in the table WITHIN the whole period given by start and end ?

I came up with something like this:

class PeriodTableValue
{
   DateTime period; // Ignore year component of datetime
   double val_1;
   double val_2;
}
void FeedMyFish(double howmuch, DateTime period_start, DateTime period_end)
{
   ...
}
...
PeriodTableValue[] table = ...
DateTime start = ...
DateTime end = ...

DateTime d1 = start;
for(int i = 0; i < table.Length; i++)
{
   DateTime d2 = table[i].period;
   int nI = find the occurrances of period table[i]. How ???
   for(int j = 0; j < nI; j++)
   {
      FeedMyFish(..parameters ???)
   }
   d1 = d2;
}

And I'm stuck right here. Please advise.

Thanks!


This article includes support for various period types and the search for intersection periods:

// ----------------------------------------------------------------------
public void TimePeriodIntersectorSample()
{
  TimePeriodCollection periods = new TimePeriodCollection();

  periods.Add( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 15 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 12 ), new DateTime( 2011, 3, 18 ) ) );

  periods.Add( new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 24 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 22 ), new DateTime( 2011, 3, 28 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 24 ), new DateTime( 2011, 3, 26 ) ) );

  TimePeriodIntersector<TimeRange> periodIntersector = 
                    new TimePeriodIntersector<TimeRange>();
  ITimePeriodCollection intersectedPeriods = periodIntersector.IntersectPeriods( periods );

  foreach ( ITimePeriod intersectedPeriod in intersectedPeriods )
  {
    Console.WriteLine( "Intersected Period: " + intersectedPeriod );
  }
  // > Intersected Period: 05.03.2011 - 10.03.2011 | 5.00:00
  // > Intersected Period: 12.03.2011 - 15.03.2011 | 3.00:00
  // > Intersected Period: 22.03.2011 - 26.03.2011 | 4.00:00
} // TimePeriodIntersectorSample
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜