} // FindPreviousWeek // ---------------------------------------------------------------------- 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); } // GetAvailableWeekPeriods
public void CollectExcludePeriodTest() { const int workingDays2011 = 365 - 2 - ( 51 * 2 ) - 1; const int workingDaysMarch2011 = 31 - 8; // total days - weekend days Year year2011 = new Year( 2011 ); CalendarPeriodCollectorFilter filter1 = new CalendarPeriodCollectorFilter(); filter1.AddWorkingWeekDays(); CalendarPeriodCollector collector1 = new CalendarPeriodCollector( filter1, year2011 ); collector1.CollectDays(); Assert.AreEqual( collector1.Periods.Count, workingDays2011 ); // exclude month CalendarPeriodCollectorFilter filter2 = new CalendarPeriodCollectorFilter(); filter2.AddWorkingWeekDays(); filter2.ExcludePeriods.Add( new Month( 2011, YearMonth.March ) ); CalendarPeriodCollector collector2 = new CalendarPeriodCollector( filter2, year2011 ); collector2.CollectDays(); Assert.AreEqual( collector2.Periods.Count, workingDays2011 - workingDaysMarch2011 ); // exclude weeks (holidays) CalendarPeriodCollectorFilter filter3 = new CalendarPeriodCollectorFilter(); filter3.AddWorkingWeekDays(); filter3.ExcludePeriods.Add( new Month( 2011, YearMonth.March ) ); filter3.ExcludePeriods.Add( new Weeks( 2011, 26, 2 ) ); CalendarPeriodCollector collector3 = new CalendarPeriodCollector( filter3, year2011 ); collector3.CollectDays(); Assert.AreEqual( collector3.Periods.Count, workingDays2011 - workingDaysMarch2011 - 10 ); }
public void CalendarPeriodCollectorSample() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); // only Januaries filter.WeekDays.Add( DayOfWeek.Friday ); // only Fridays filter.CollectingHours.Add( new HourRange( 8, 18 ) ); // working hours CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); Console.WriteLine( "Calendar period collector of period: " + testPeriod ); // > Calendar period collector of period: 01.01.2010 00:00:00 - 30.12.2011 23:59:59 | 728.23:59 CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); foreach ( ITimePeriod period in collector.Periods ) { Console.WriteLine( "Period: " + period ); } // > Period: 01.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 08.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 15.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 22.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 29.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 07.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 14.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 21.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 28.01.2011; 08:00 - 17:59 | 0.09:59 }
public void CollectDaysTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); filter.WeekDays.Add( DayOfWeek.Friday ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectDays(); Assert.AreEqual( collector.Periods.Count, 9 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 01 ), new DateTime( 2010, 1, 02 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 08 ), new DateTime( 2010, 1, 09 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 15 ), new DateTime( 2010, 1, 16 ) ) ) ); Assert.IsTrue( collector.Periods[ 3 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 22 ), new DateTime( 2010, 1, 23 ) ) ) ); Assert.IsTrue( collector.Periods[ 4 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 29 ), new DateTime( 2010, 1, 30 ) ) ) ); Assert.IsTrue( collector.Periods[ 5 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 07 ), new DateTime( 2011, 1, 08 ) ) ) ); Assert.IsTrue( collector.Periods[ 6 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 14 ), new DateTime( 2011, 1, 15 ) ) ) ); Assert.IsTrue( collector.Periods[ 7 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 21 ), new DateTime( 2011, 1, 22 ) ) ) ); Assert.IsTrue( collector.Periods[ 8 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 28 ), new DateTime( 2011, 1, 29 ) ) ) ); }
/// <summary> /// The get remaining working time. /// </summary> /// <param name="start"> /// The start. /// </param> /// <param name="dueDate"> /// The due date. /// </param> /// <returns> /// The <see cref="TimeSpan"/>. /// </returns> public TimeSpan GetRemainingWorkingTime(DateTime start, DateTime dueDate) { var filter = new CalendarPeriodCollectorFilter(); foreach (var dayOfWeek in this.WorkDays) { filter.CollectingDayHours.Add(new DayHourRange(dayOfWeek.DayOfWeek, new Time(dayOfWeek.StartTime), new Time(dayOfWeek.EndTime))); } foreach (var holiday in this.Holidays) { filter.ExcludePeriods.Add(new TimeBlock(holiday.StartTime, holiday.EndTime)); } var range = new CalendarTimeRange(start, dueDate); var collector = new CalendarPeriodCollector(filter, range); collector.CollectHours(); var duration = collector.Periods.GetTotalDuration(new TimeZoneDurationProvider(TimeZoneInfo.FindSystemTimeZoneById("UTC"))); return duration; //var rounded = Math.Round(duration.TotalMinutes, MidpointRounding.AwayFromZero); //return TimeSpan.FromMinutes(rounded); }
public void CollectYearsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Years.Add( 2006 ); filter.Years.Add( 2007 ); filter.Years.Add( 2012 ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2001, 1, 1 ), new DateTime( 2019, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectYears(); Assert.AreEqual( collector.Periods.Count, 3 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2006, 1, 1 ), new DateTime( 2007, 1, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2007, 1, 1 ), new DateTime( 2008, 1, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2012, 1, 1 ), new DateTime( 2013, 1, 1 ) ) ) ); }
public void CollectMonthsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectMonths(); Assert.AreEqual( collector.Periods.Count, 2 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2010, 2, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 2, 1 ) ) ) ); }
// ---------------------------------------------------------------------- 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; }
// ------------------------------------------------------------------------ public LastDaysOfMonthPeriodCollector( CalendarPeriodCollectorFilter filter, ITimePeriod limits ) : base(filter, limits) { }
// ---------------------------------------------------------------------- public double NetworkDays( DateTime start, DateTime end, IEnumerable<DateTime> holidays = null, ITimePeriodCollection holidayPeriods = null ) { Day startDay = new Day( start < end ? start : end ); Day endDay = new Day( end > start ? end : start ); if ( startDay.Equals( endDay ) ) { return 0; } CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); // only working days if ( holidays != null ) { foreach ( DateTime holiday in holidays ) { filter.ExcludePeriods.Add( new Day( holiday ) ); } } if ( holidayPeriods != null ) { filter.ExcludePeriods.AddAll( holidayPeriods ); } CalendarTimeRange testPeriod = new CalendarTimeRange( start, end ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectDays(); double networkDays = 0.0; foreach ( ICalendarTimeRange period in collector.Periods ) { networkDays += Math.Round( period.Duration.TotalDays, 2 ); } return networkDays; }
public void FindRemainigYearFridaysSample() { // filter: only Fridays CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.WeekDays.Add( DayOfWeek.Friday ); // the collecting period CalendarTimeRange collectPeriod = new CalendarTimeRange( DateTime.Now, new Year().End.Date ); // collect all Fridays CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, collectPeriod ); collector.CollectDays(); // show the results foreach ( ITimePeriod period in collector.Periods ) { Console.WriteLine( "Friday: " + period ); } }
// ---------------------------------------------------------------------- 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; }
// ---------------------------------------------------------------------- public double CalculateBusinessHours( CalendarTimeRange testPeriod, ITimePeriodCollection holidays = null ) { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.CollectingMonths.Add( new MonthRange( YearMonth.January, YearMonth.January ) ); filter.CollectingDays.Add( new DayRange( 1, 1 ) ); filter.AddWorkingWeekDays(); // only working days filter.CollectingHours.Add( new HourRange( 8, 12 ) ); // opening hours morning filter.CollectingHours.Add( new HourRange( 13, 18 ) ); // opening hours afternoon if ( holidays != null ) { filter.ExcludePeriods.AddAll( holidays ); } CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); double businessHours = 0.0; foreach ( ICalendarTimeRange period in collector.Periods ) { businessHours += Math.Round( period.Duration.TotalHours, 2 ); } return businessHours; }
// ---------------------------------------------------------------------- private double CalculateBusinessHours( ITimePeriod timePeriod ) { double businessHours = 0; // collect filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); filter.CollectingHours.Add( new HourRange( 8, 18 ) ); //filter.ExcludePeriods.Add( new Day( 2012, 4, 6 ) ); // mor exclude periods // collect the working hours TimeRange collectorTimeRange = new TimeRange( timePeriod.Start.Date, timePeriod.End.Date.AddDays( 1 ) ); TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, collectorTimeRange, SeekDirection.Forward, calendar ); collector.CollectHours(); if ( collector.Periods.Count > 0 ) { // calculate the business hour periods collector.Periods.Add( timePeriod ); // add source period to find the intersections TimePeriodIntersector<TimeRange> intersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection businessHourPeriods = intersector.IntersectPeriods( collector.Periods ); // collect the business hours foreach ( TimeRange businessHourPeriod in businessHourPeriods ) { businessHours += businessHourPeriod.Duration.TotalHours; } } return businessHours; }
public void WorkingHourSum() { DateTime start = new DateTime( 2011, 9, 11, 18, 0, 0 ); DateTime end = new DateTime( 2011, 9, 15, 9, 0, 0 ); // collect working hours CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Monday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Tuesday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Wednesday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Thursday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Friday, 8, 12 ) ); filter.ExcludePeriods.Add( new Day( 2011, 9, 13 ) ); // exclude Tuesday CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, new TimeRange( start.Date, end.Date.AddDays( 1 ) ), SeekDirection.Forward, new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ) ); collector.CollectHours(); // add boundaries collector.Periods.Add( new TimeRange( start, end ) ); // intersect periods TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( collector.Periods ); // calculate working hours (sum period durations) TimeSpan workingHours = TimeSpan.Zero; foreach ( ITimePeriod period in periods ) { workingHours = workingHours.Add( period.Duration ); } Console.WriteLine( "Total working hours: " + workingHours ); // > Total working hours: 09:00:00 }
public void CollectHoursTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); filter.WeekDays.Add( DayOfWeek.Friday ); filter.CollectingHours.Add( new HourRange( 8, 18 ) ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); Assert.AreEqual( collector.Periods.Count, 9 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 01, 8, 0, 0 ), new DateTime( 2010, 1, 01, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 08, 8, 0, 0 ), new DateTime( 2010, 1, 08, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 15, 8, 0, 0 ), new DateTime( 2010, 1, 15, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 3 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 22, 8, 0, 0 ), new DateTime( 2010, 1, 22, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 4 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 29, 8, 0, 0 ), new DateTime( 2010, 1, 29, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 5 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 07, 8, 0, 0 ), new DateTime( 2011, 1, 07, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 6 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 14, 8, 0, 0 ), new DateTime( 2011, 1, 14, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 7 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 21, 8, 0, 0 ), new DateTime( 2011, 1, 21, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 8 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 28, 8, 0, 0 ), new DateTime( 2011, 1, 28, 18, 0, 0 ) ) ) ); }
// ---------------------------------------------------------------------- private void Collect( CollectType collectType ) { // filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); foreach ( int year in yearFilter.SelectedItems ) { filter.Years.Add( year ); } foreach ( YearMonth yearMonth in yearMonthFilter.SelectedItems ) { filter.Months.Add( yearMonth ); } foreach ( int day in dayFilter.SelectedItems ) { filter.Days.Add( day ); } foreach ( DayOfWeek dayOfWeek in dayOfWeekFilter.SelectedItems ) { filter.WeekDays.Add( dayOfWeek ); } // period limits TimeRange workingTimeRange = WorkingTimeRange; if ( workingTimeRange == null ) { CollectorStatus = "Invalid Working Time Range!"; return; } // collect months YearMonth? collectMonthStart = CollectMonthStart; YearMonth? collectMonthEnd = CollectMonthEnd; if ( collectMonthStart.HasValue && collectMonthEnd.HasValue ) { if ( collectMonthEnd.Value < collectMonthStart.Value ) { CollectorStatus = "Invalid Collecting Periods Months!"; return; } filter.CollectingMonths.Add( new MonthRange( collectMonthStart.Value, collectMonthEnd.Value ) ); } // collect day int? collectDayStart = CollectDayStart; int? collectDayEnd = CollectDayEnd; if ( collectDayStart.HasValue && collectDayEnd.HasValue ) { if ( collectDayEnd.Value < collectDayStart.Value ) { CollectorStatus = "Invalid Collecting Periods Days!"; return; } filter.CollectingDays.Add( new DayRange( collectDayStart.Value, collectDayEnd.Value ) ); } // collect hour int? collectHourStart = CollectHourStart; int? collectHourEnd = CollectHourEnd; if ( collectHourStart.HasValue && collectHourEnd.HasValue ) { if ( collectHourEnd.Value < collectHourStart.Value ) { CollectorStatus = "Invalid Collecting Periods Hours!"; return; } filter.CollectingHours.Add( new HourRange( collectHourStart.Value, collectHourEnd.Value ) ); } StringBuilder status = new StringBuilder(); // collector string collectContext = string.Empty; TimePeriod.CalendarPeriodCollector collector = new TimePeriod.CalendarPeriodCollector( filter, workingTimeRange ); switch ( collectType ) { case CollectType.Year: collectContext = "year"; collector.CollectYears(); break; case CollectType.Month: collectContext = "month"; collector.CollectMonths(); break; case CollectType.Day: collectContext = "day"; collector.CollectDays(); break; case CollectType.Hour: collectContext = "hour"; collector.CollectHours(); break; } collectedPeriods.Clear(); if ( collector.Periods.Count == 0 ) { status.Append( string.Format( "no {0} periods found", collectContext ) ); } else if ( collector.Periods.Count > maxPeriodCount ) { status.Append( string.Format( "{0} {1} periods found, a maximum of {2} periods can be displayed", collector.Periods.Count, collectContext, maxPeriodCount ) ); } else { foreach ( ITimePeriod timePeriod in collector.Periods ) { collectedPeriods.Add( timePeriod ); } status.Append( string.Format( "{0} {1} periods found", collectedPeriods.Count, collectContext ) ); } CollectorStatus = status.ToString(); LastCollectionDate = DateTime.Now; if ( CopyToClipboard ) { collectedPeriods.CopyToClipboard(); } }