GetIntersection() публичный Метод

public GetIntersection ( ITimePeriod period ) : ITimeRange
period ITimePeriod
Результат ITimeRange
Пример #1
0
        }         // CalcDayllightDuration

        // ----------------------------------------------------------------------
        protected virtual TimeSpan DoCalcDuration(DateTime start, DateTime end, TimeZoneInfo timeZone = null)
        {
            if (start.Equals(end))
            {
                return(TimeSpan.Zero);
            }

            // test range
            TimeRange testRange = new TimeRange(start, end);

            // search range
            DateTime  searchStart  = new Day(testRange.Start).Start;
            DateTime  serachEnd    = new Day(testRange.End).GetNextDay().Start;
            TimeRange searchPeriod = new TimeRange(searchStart, serachEnd);

            // exclude periods
            filter.ExcludePeriods.Clear();
            filter.ExcludePeriods.AddAll(excludePeriods);

            // collect hours
            TimeCalendar calendar = new TimeCalendar(new TimeCalendarConfig {
                EndOffset = TimeSpan.Zero
            });
            CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, searchPeriod, SeekDirection.Forward, calendar);

            collector.CollectHours();

            TimeSpan duration = TimeSpan.Zero;

            collector.Periods.AddAll(includePeriods);
            foreach (ICalendarTimeRange period in collector.Periods)
            {
                // get the intersection of the test-range and the day hours
                ITimePeriod intersection = testRange.GetIntersection(period);
                if (intersection == null)
                {
                    continue;
                }
                duration = duration.Add(durationProvider.GetDuration(intersection.Start, intersection.End));
            }

            return(start < end ? duration : duration.Negate());
        }         // DoCalcDuration
Пример #2
0
        public void TimeRangeSample()
        {
            // --- time range 1 ---
            TimeRange timeRange1 = new TimeRange(
                new DateTime( 2011, 2, 22, 14, 0, 0 ),
                new DateTime( 2011, 2, 22, 18, 0, 0 ) );
            Console.WriteLine( "TimeRange1: " + timeRange1 );
            // > TimeRange1: 22.02.2011 14:00:00 - 18:00:00 | 04:00:00

            // --- time range 2 ---
            TimeRange timeRange2 = new TimeRange(
                new DateTime( 2011, 2, 22, 15, 0, 0 ),
                new TimeSpan( 2, 0, 0 ) );
            Console.WriteLine( "TimeRange2: " + timeRange2 );
            // > TimeRange2: 22.02.2011 15:00:00 - 17:00:00 | 02:00:00

            // --- time range 3 ---
            TimeRange timeRange3 = new TimeRange(
                new DateTime( 2011, 2, 22, 16, 0, 0 ),
                new DateTime( 2011, 2, 22, 21, 0, 0 ) );
            Console.WriteLine( "TimeRange3: " + timeRange3 );
            // > TimeRange3: 22.02.2011 16:00:00 - 21:00:00 | 05:00:00

            // --- relation ---
            Console.WriteLine( "TimeRange1.GetRelation( TimeRange2 ): " + timeRange1.GetRelation( timeRange2 ) );
            // > TimeRange1.GetRelation( TimeRange2 ): Enclosing
            Console.WriteLine( "TimeRange1.GetRelation( TimeRange3 ): " + timeRange1.GetRelation( timeRange3 ) );
            // > TimeRange1.GetRelation( TimeRange3 ): EndInside
            Console.WriteLine( "TimeRange3.GetRelation( TimeRange2 ): " + timeRange3.GetRelation( timeRange2 ) );
            // > TimeRange3.GetRelation( TimeRange2 ): StartInside

            // --- intersection ---
            Console.WriteLine( "TimeRange1.GetIntersection( TimeRange2 ): " + timeRange1.GetIntersection( timeRange2 ) );
            // > TimeRange1.GetIntersection( TimeRange2 ): 22.02.2011 15:00:00 - 17:00:00 | 02:00:00
            Console.WriteLine( "TimeRange1.GetIntersection( TimeRange3 ): " + timeRange1.GetIntersection( timeRange3 ) );
            // > TimeRange1.GetIntersection( TimeRange3 ): 22.02.2011 16:00:00 - 18:00:00 | 02:00:00
            Console.WriteLine( "TimeRange3.GetIntersection( TimeRange2 ): " + timeRange3.GetIntersection( timeRange2 ) );
            // > TimeRange3.GetIntersection( TimeRange2 ): 22.02.2011 16:00:00 - 17:00:00 | 01:00:00
        }
Пример #3
0
        public void GetIntersectionTest()
        {
            TimeRange readOnlyTimeRange = new TimeRange( start, end );
            Assert.AreEqual( readOnlyTimeRange.GetIntersection( readOnlyTimeRange ), new TimeRange( readOnlyTimeRange ) );

            TimeRange timeRange = new TimeRange( start, end );

            // before
            ITimeRange before1 = timeRange.GetIntersection( new TimeRange( start.AddHours( -2 ), start.AddHours( -1 ) ) );
            Assert.AreEqual( before1, null );
            ITimeRange before2 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), start ) );
            Assert.AreEqual( before2, new TimeRange( start ) );
            ITimeRange before3 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), start.AddMilliseconds( 1 ) ) );
            Assert.AreEqual( before3, new TimeRange( start, start.AddMilliseconds( 1 ) ) );

            // after
            ITimeRange after1 = timeRange.GetIntersection( new TimeRange( end.AddHours( 1 ), end.AddHours( 2 ) ) );
            Assert.AreEqual( after1, null );
            ITimeRange after2 = timeRange.GetIntersection( new TimeRange( end, end.AddMilliseconds( 1 ) ) );
            Assert.AreEqual( after2, new TimeRange( end ) );
            ITimeRange after3 = timeRange.GetIntersection( new TimeRange( end.AddMilliseconds( -1 ), end.AddMilliseconds( 1 ) ) );
            Assert.AreEqual( after3, new TimeRange( end.AddMilliseconds( -1 ), end ) );

            // intersect
            Assert.AreEqual( timeRange.GetIntersection( timeRange ), timeRange );
            ITimeRange itersect1 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), end.AddMilliseconds( 1 ) ) );
            Assert.AreEqual( itersect1, timeRange );
            ITimeRange itersect2 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( 1 ), end.AddMilliseconds( -1 ) ) );
            Assert.AreEqual( itersect2, new TimeRange( start.AddMilliseconds( 1 ), end.AddMilliseconds( -1 ) ) );
        }
Пример #4
0
        // ----------------------------------------------------------------------
        public TimeSpan CalcWorkingPeriod( DateTime start, DateTime end,
            ITimePeriodCollection excludePeriods = null)
        {
            if ( start.Equals( end ) )
            {
                return TimeSpan.Zero;
            }

            // test range
            TimeRange testRange = new TimeRange( start, end );

            // search range
            DateTime searchStart = new Day( testRange.Start ).Start;
            DateTime serachEnd = new Day( testRange.End ).GetNextDay().Start;
            TimeRange searchPeriod = new TimeRange( searchStart, serachEnd );

            // search filter
            CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
            filter.AddWorkingWeekDays(); // working days
            if ( excludePeriods != null )
            {
                filter.ExcludePeriods.AddAll( excludePeriods );
            }
            filter.CollectingHours.Add( new HourRange( 07, 12 ) ); // working hours
            filter.CollectingHours.Add( new HourRange( 13, 19 ) ); // working hours

            // collect working hours
            TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } );
            CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, searchPeriod, SeekDirection.Forward, calendar );
            collector.CollectHours();

            TimeSpan workingPeriod = new TimeSpan();
            foreach ( ICalendarTimeRange period in collector.Periods )
            {
                // get the intersection of the test-range and the day working-hours
                ITimePeriod intersection = testRange.GetIntersection( period );
                if ( intersection == null )
                {
                    continue;
                }
                workingPeriod = workingPeriod.Add( intersection.Duration );
            }
            return workingPeriod;
        }
Пример #5
0
        // ----------------------------------------------------------------------
        private ITimePeriod GetCommonIntersection( ITimePeriodCollection periods )
        {
            if ( periods.Count == 0 )
            {
                return null;
            }

            ITimeRange timeRange = new TimeRange( periods[ 0 ] );
            if ( periods.Count > 1 )
            {
                for ( int i = 1; i < periods.Count; i++ )
                {
                    timeRange = timeRange.GetIntersection( periods[ i ] );
                    if ( timeRange == null )
                    {
                        return null;
                    }
                }
            }

            return timeRange;
        }