IntersectionPeriods() public method

public IntersectionPeriods ( System.DateTime test ) : ITimePeriodCollection
test System.DateTime
return ITimePeriodCollection
        public void IntersectionPeriodsTimePeriodTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            TimeRange timeRange1 = new TimeRange( new DateTime( now.Year, now.Month, 8 ), new DateTime( now.Year, now.Month, 18 ) );
            TimeRange timeRange2 = new TimeRange( new DateTime( now.Year, now.Month, 10 ), new DateTime( now.Year, now.Month, 11 ) );
            TimeRange timeRange3 = new TimeRange( new DateTime( now.Year, now.Month, 13 ), new DateTime( now.Year, now.Month, 15 ) );
            TimeRange timeRange4 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 13 ) );
            TimeRange timeRange5 = new TimeRange( new DateTime( now.Year, now.Month, 15 ), new DateTime( now.Year, now.Month, 17 ) );

            TimePeriodCollection timePeriods = new TimePeriodCollection();
            timePeriods.Add( timeRange1 );
            timePeriods.Add( timeRange2 );
            timePeriods.Add( timeRange3 );
            timePeriods.Add( timeRange4 );
            timePeriods.Add( timeRange5 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange1 ).Count, 5 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange2 ).Count, 3 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange3 ).Count, 4 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange4 ).Count, 4 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange5 ).Count, 3 );

            ITimeRange test1 = timeRange1.Copy( new TimeSpan( 100, 0, 0, 0 ).Negate() );
            ITimePeriodCollection insidePeriods1 = timePeriods.IntersectionPeriods( test1 );
            Assert.AreEqual( insidePeriods1.Count, 0 );

            ITimeRange test2 = timeRange1.Copy( new TimeSpan( 100, 0, 0, 0 ) );
            ITimePeriodCollection insidePeriods2 = timePeriods.IntersectionPeriods( test2 );
            Assert.AreEqual( insidePeriods2.Count, 0 );

            TimeRange test3 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 11 ) );
            ITimePeriodCollection insidePeriods3 = timePeriods.IntersectionPeriods( test3 );
            Assert.AreEqual( insidePeriods3.Count, 3 );

            TimeRange test4 = new TimeRange( new DateTime( now.Year, now.Month, 14 ), new DateTime( now.Year, now.Month, 17 ) );
            ITimePeriodCollection insidePeriods4 = timePeriods.IntersectionPeriods( test4 );
            Assert.AreEqual( insidePeriods4.Count, 3 );
        }
        public void IntersectionPeriodsDateTimeTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            TimeRange timeRange1 = new TimeRange( new DateTime( now.Year, now.Month, 8 ), new DateTime( now.Year, now.Month, 18 ) );
            TimeRange timeRange2 = new TimeRange( new DateTime( now.Year, now.Month, 10 ), new DateTime( now.Year, now.Month, 11 ) );
            TimeRange timeRange3 = new TimeRange( new DateTime( now.Year, now.Month, 13 ), new DateTime( now.Year, now.Month, 15 ) );
            TimeRange timeRange4 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 14 ) );
            TimeRange timeRange5 = new TimeRange( new DateTime( now.Year, now.Month, 16 ), new DateTime( now.Year, now.Month, 17 ) );

            TimePeriodCollection timePeriods = new TimePeriodCollection();
            timePeriods.Add( timeRange1 );
            timePeriods.Add( timeRange2 );
            timePeriods.Add( timeRange3 );
            timePeriods.Add( timeRange4 );
            timePeriods.Add( timeRange5 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange1.Start ).Count, 1 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange1.End ).Count, 1 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange2.Start ).Count, 3 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange2.End ).Count, 3 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange3.Start ).Count, 3 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange3.End ).Count, 2 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange4.Start ).Count, 2 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange4.End ).Count, 3 );

            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange5.Start ).Count, 2 );
            Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange5.End ).Count, 2 );

            DateTime test1 = timeRange1.Start.AddMilliseconds( -1 );
            ITimePeriodCollection insidePeriods1 = timePeriods.IntersectionPeriods( test1 );
            Assert.AreEqual( insidePeriods1.Count, 0 );

            DateTime test2 = timeRange1.End.AddMilliseconds( 1 );
            ITimePeriodCollection insidePeriods2 = timePeriods.IntersectionPeriods( test2 );
            Assert.AreEqual( insidePeriods2.Count, 0 );

            DateTime test3 = new DateTime( now.Year, now.Month, 12 );
            ITimePeriodCollection insidePeriods3 = timePeriods.IntersectionPeriods( test3 );
            Assert.AreEqual( insidePeriods3.Count, 2 );

            DateTime test4 = new DateTime( now.Year, now.Month, 14 );
            ITimePeriodCollection insidePeriods4 = timePeriods.IntersectionPeriods( test4 );
            Assert.AreEqual( insidePeriods4.Count, 3 );
        }
Exemplo n.º 3
0
        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 <= 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;
            }
        }
Exemplo n.º 4
0
        public void TimePeriodCollectionSample()
        {
            TimePeriodCollection timePeriods = new TimePeriodCollection();

            DateTime testDay = new DateTime( 2010, 7, 23 );

            // --- items ---
            timePeriods.Add( new TimeRange( TimeTrim.Hour( testDay, 8 ), TimeTrim.Hour( testDay, 11 ) ) );
            timePeriods.Add( new TimeBlock( TimeTrim.Hour( testDay, 10 ), Duration.Hours( 3 ) ) );
            timePeriods.Add( new TimeRange( TimeTrim.Hour( testDay, 16, 15 ), TimeTrim.Hour( testDay, 18, 45 ) ) );
            timePeriods.Add( new TimeRange( TimeTrim.Hour( testDay, 14 ), TimeTrim.Hour( testDay, 15, 30 ) ) );
            Console.WriteLine( "TimePeriodCollection: " + timePeriods );
            // > TimePeriodCollection: Count = 4; 23.07.2010 08:00:00 - 18:45:00 | 0.10:45
            Console.WriteLine( "TimePeriodCollection.Items" );
            foreach ( ITimePeriod timePeriod in timePeriods )
            {
                Console.WriteLine( "Item: " + timePeriod );
            }
            // > Item: 23.07.2010 08:00:00 - 11:00:00 | 03:00:00
            // > Item: 23.07.2010 10:00:00 - 13:00:00 | 03:00:00
            // > Item: 23.07.2010 16:15:00 - 18:45:00 | 02:30:00
            // > Item: 23.07.2010 14:00:00 - 15:30:00 | 01:30:00

            // --- intersection by moment ---
            DateTime intersectionMoment = new DateTime( 2010, 7, 23, 10, 30, 0 );
            ITimePeriodCollection momentIntersections = timePeriods.IntersectionPeriods( intersectionMoment );
            Console.WriteLine( "TimePeriodCollection.IntesectionPeriods of " + intersectionMoment );
            // > TimePeriodCollection.IntesectionPeriods of 23.07.2010 10:30:00
            foreach ( ITimePeriod momentIntersection in momentIntersections )
            {
                Console.WriteLine( "Intersection: " + momentIntersection );
            }
            // > Intersection: 23.07.2010 08:00:00 - 11:00:00 | 03:00:00
            // > Intersection: 23.07.2010 10:00:00 - 13:00:00 | 03:00:00

            // --- intersection by period ---
            TimeRange intersectionPeriod = new TimeRange( TimeTrim.Hour( testDay, 9 ), TimeTrim.Hour( testDay, 14, 30 ) );
            ITimePeriodCollection periodIntersections = timePeriods.IntersectionPeriods( intersectionPeriod );
            Console.WriteLine( "TimePeriodCollection.IntesectionPeriods of " + intersectionPeriod );
            // > TimePeriodCollection.IntesectionPeriods of 23.07.2010 09:00:00 - 14:30:00 | 0.05:30
            foreach ( ITimePeriod periodIntersection in periodIntersections )
            {
                Console.WriteLine( "Intersection: " + periodIntersection );
            }
            // > Intersection: 23.07.2010 08:00:00 - 11:00:00 | 03:00:00
            // > Intersection: 23.07.2010 10:00:00 - 13:00:00 | 03:00:00
            // > Intersection: 23.07.2010 14:00:00 - 15:30:00 | 01:30:00
        }
Exemplo n.º 5
0
        public void CombinedPeriodsSample()
        {
            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 );
            }

            TimeRange test1 = new TimeRange( new DateTime( 2000, 1, 1 ), new DateTime( 2009, 12, 31 ) );
            ITimePeriodCollection intersections1 = periods.IntersectionPeriods( test1 );
            foreach ( ITimePeriod intersection1 in intersections1 )
            {
                Console.WriteLine( "Intersection of {0}: {1}", test1, intersection1 );
            }

            TimeRange test2 = new TimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2014, 12, 31 ) );
            ITimePeriodCollection intersections2 = periods.IntersectionPeriods( test2 );
            foreach ( ITimePeriod intersection2 in intersections2 )
            {
                Console.WriteLine( "Intersection of {0}: {1}", test2, intersection2 );
            }

            TimeRange test3 = new TimeRange( new DateTime( 2015, 1, 1 ), new DateTime( 2019, 12, 31 ) );
            ITimePeriodCollection intersections3 = periods.IntersectionPeriods( test3 );
            foreach ( ITimePeriod intersection3 in intersections3 )
            {
                Console.WriteLine( "Intersection of {0}: {1}", test3, intersection3 );
            }
        }