【问题标题】:Iterating over discrete periods of time within another period of time在另一个时间段内迭代离散时间段
【发布时间】:2011-04-04 09:27:46
【问题描述】:

我有一个周期表,其中描述了……说……喂我的鱼的频率:

        --------------------------------------------------------
Period: Jan  Feb  March  April  May  Jun  Jul ... n - 1 .... n
        --------------------------------------------------------
Val_1:   5    2    3      6      3    2    4       x         x
Val_2    ...
        --------------------------------------------------------

我有两个日期时间,开始和结束,即:

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

..进料过程发生的时间。如何从表中获取与 start 和 end 给出的时间段相关的每个时间段的值?

例如 start 和 end 给出的周期是 2.5 年,但我的表只描述了 12 个月。如何在 start 和 end 给出的整个期间内循环遍历表中的每个期间?

我想出了这样的东西:

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;
}

我被困在这里了。请指教。

谢谢!

【问题讨论】:

    标签: algorithm datetime date calendar timespan


    【解决方案1】:

    article 包括对各种周期类型的支持以及对交叉周期的搜索:

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

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多