} // Difference // ---------------------------------------------------------------------- public TimeSpan Difference(DateTime date1, DateTime date2) { if (date1.Equals(date2)) { return(TimeSpan.Zero); } if (collectorFilter.WeekDays.Count == 0 && collectorFilter.CollectingHours.Count == 0 && collectorFilter.CollectingDayHours.Count == 0) { return(new DateDiff(date1, date2, calendar.Culture.Calendar, calendar.FirstDayOfWeek, calendar.YearBaseMonth).Difference); } TimeRange differenceRange = new TimeRange(date1, date2); CalendarPeriodCollector collector = new CalendarPeriodCollector( collectorFilter, new TimeRange(differenceRange.Start.Date, differenceRange.End.Date.AddDays(1)), SeekDirection.Forward, calendar); collector.CollectHours(); // calculate gaps TimeGapCalculator <TimeRange> gapCalculator = new TimeGapCalculator <TimeRange>(calendar); ITimePeriodCollection gaps = gapCalculator.GetGaps(collector.Periods, differenceRange); // calculate difference (sum gap durations) TimeSpan difference = gaps.GetTotalDuration(durationProvider); return(date1 < date2 ? difference : difference.Negate()); } // Difference
} // FindPreviousWeek // ---------------------------------------------------------------------- private IEnumerable <ITimePeriod> GetAvailableWeekPeriods(ITimePeriod period) { if (weekDays.Count == 0 && workingHours.Count == 0 && WorkingDayHours.Count == 0) { return(new TimePeriodCollection { period }); } CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); // days foreach (DayOfWeek weekDay in weekDays) { filter.WeekDays.Add(weekDay); } // hours foreach (HourRange workingHour in workingHours) { filter.CollectingHours.Add(workingHour); } // day hours foreach (DayHourRange workingDayHour in workingDayHours) { filter.CollectingDayHours.Add(workingDayHour); } CalendarPeriodCollector weekCollector = new CalendarPeriodCollector(filter, period, SeekDirection.Forward, calendar); weekCollector.CollectHours(); return(weekCollector.Periods); } // GetAvailableWeekPeriods
} // 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