Ejemplo n.º 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