我有一个下表的时间表,描述了...喂我的鱼的频率:
--------------------------------------------------------
Period: Jan Feb March April May Jun Jul ... n - 1 .... n
--------------------------------------------------------
Val_1: 5 2 3 6 3 2 4 x x
Val_2 ...
--------------------------------------------------------
我有一个时间段,有两个DateTimes,start和end,即:
DateTime start = new DateTime(2010, 3, 11);
DateTime end = new DateTime(2012, 7, 12);
..在哪个时间进行喂食过程。如何从每个时间段的表中获取与开始和结束给出的时间段相关的值?
例如,开始和结束时间为2。5年,但我的表仅描述了12个月。如何在开始和结束给定的整个时间内循环表中的每个句点?
我想出了类似的东西:
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;
}
我被困在这里。请指教。
谢谢!
答案 0 :(得分:0)
此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