在另一个时间段内的离散时间段上迭代

发布于 2024-10-30 03:14:39 字数 1278 浏览 3 评论 0原文

我有一个周期表,描述了..比如说..喂鱼的频率:

        --------------------------------------------------------
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 给定期间相关的每个期间的值?

例如,开始和结束给出的期限是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;
}

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

谢谢!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

擦肩而过的背影 2024-11-06 03:14:39

这篇文章包括对各种周期类型的支持以及交叉周期的搜索:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文