protected virtual IEnumerable <ITimePeriod> GetAvailableWeekPeriods(Week week) { if (_weekDays.Count == 0 && _workingHours.Count == 0 && WorkingDayHours.Count == 0) { return(new TimePeriodCollection { week }); } 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, week, SeekDirection.Forward, Calendar); weekCollector.CollectHours(); return(weekCollector.Periods); }
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()); }
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()); }