A TimePeriodCollection (implementing ITimePeriodCollection) holds multiple ITimePeriod elements.
Key Behaviors:
- Automatic Bounds: The collection's own time period is defined by the earliest start and the latest end of all contained elements.
- Intersection by Moment: You can find which periods in the collection overlap with a specific
DateTime using IntersectionPeriods(DateTime moment). - Intersection by Period: You can find which periods in the collection overlap with another
ITimePeriod using IntersectionPeriods(ITimePeriod period).
Both intersection methods return an ITimePeriodCollection containing the resulting overlapping periods.
TimePeriodCollection timePeriods = new TimePeriodCollection();
DateTime testDay = new DateTime( 2010, 7, 23 );
// Add various periods
timePeriods.Add( new TimeRange( TimeTrim.Hour( testDay, 8 ), TimeTrim.Hour( testDay, 11 ) ) );
timePeriods.Add( new TimeBlock( TimeTrim.Hour( testDay, 10 ), Duration.Hours( 3 ) ) );
// Find intersections with a specific moment
DateTime intersectionMoment = new DateTime( 2010, 7, 23, 10, 30, 0 );
ITimePeriodCollection momentIntersections = timePeriods.IntersectionPeriods( intersectionMoment );
// Find intersections with another period
TimeRange intersectionPeriod = new TimeRange( TimeTrim.Hour( testDay, 9 ), TimeTrim.Hour( testDay, 14, 30 ) );
ITimePeriodCollection periodIntersections = timePeriods.IntersectionPeriods( intersectionPeriod );