Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This <a href="http://www.codeproject.com/KB/datetime/TimePeriod.aspx" rel="nofollow">article</a> allows you to handle multiple time periods.</p> <p>First let's define a custom time range including your id:</p> <pre><code>// ------------------------------------------------------------------------ public class IdTimeRange : TimeRange { // ---------------------------------------------------------------------- public IdTimeRange( int id, DateTime start, DateTime end ) : base( start, end ) { Id = id; } // IdTimeRange // ---------------------------------------------------------------------- public int Id { get; private set; } // ---------------------------------------------------------------------- public override string ToString() { return Id + ": " + base.ToString(); } // ToString } // IdTimeRange </code></pre> <p>Now you can use a <strong>TimePeriodCollection</strong> in combination with a <strong>TimeLine</strong> to find all intersecting periods:</p> <pre><code>// ---------------------------------------------------------------------- public void TimeLinePeriodsSample() { TimePeriodCollection periods = new TimePeriodCollection(); periods.Add( new IdTimeRange( 1, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) ); periods.Add( new IdTimeRange( 2, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) ); periods.Add( new IdTimeRange( 3, new DateTime( 2000, 1, 1 ), new DateTime( 2009, 12, 31 ) ) ); periods.Add( new IdTimeRange( 3, new DateTime( 2010, 1, 1 ), new DateTime( 2019, 12, 31 ) ) ); periods.Add( new IdTimeRange( 4, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) ); periods.Add( new IdTimeRange( 5, new DateTime( 2000, 1, 1 ), new DateTime( 2014, 12, 31 ) ) ); periods.Add( new IdTimeRange( 5, new DateTime( 2015, 1, 1 ), new DateTime( 2019, 12, 31 ) ) ); foreach ( ITimePeriod period in periods ) { Console.WriteLine( "Period: " + period ); } // time line with all period start and end moments ITimeLineMomentCollection moments = new TimeLineMomentCollection(); moments.AddAll( periods ); DateTime start = periods.Start; foreach ( ITimeLineMoment moment in moments ) { if ( moment.EndCount &lt;= 0 ) // search the next period end { continue; } DateTime end = moment.Moment; TimeRange timeRange = new TimeRange( start, end ); Console.WriteLine( "Period: {0}", timeRange ); ITimePeriodCollection intersections = periods.IntersectionPeriods( timeRange ); foreach ( ITimePeriod intersection in intersections ) { Console.WriteLine( " Intersection: {0}", intersection ); } start = moment.Moment; } } // TimeLinePeriodsSample </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload