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))));
        }         // CollectHoursTest
예제 #2
0
        // ----------------------------------------------------------------------
        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
        }         // CalendarPeriodCollectorSample
        public void CollectAllDayHoursTest()
        {
            CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();

            filter.Months.Add(YearMonth.January);
            filter.WeekDays.Add(DayOfWeek.Friday);
            filter.CollectingHours.Add(new HourRange(0, 24));

            TimeRange testPeriod = new TimeRange(new DateTime(2010, 1, 1), new DateTime(2012, 1, 1));

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

            collector.CollectHours();

            Assert.Equal(9, collector.Periods.Count);
            Assert.True(collector.Periods[0].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 01), new DateTime(2010, 1, 02))));
            Assert.True(collector.Periods[1].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 08), new DateTime(2010, 1, 09))));
            Assert.True(collector.Periods[2].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 15), new DateTime(2010, 1, 16))));
            Assert.True(collector.Periods[3].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 22), new DateTime(2010, 1, 23))));
            Assert.True(collector.Periods[4].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 29), new DateTime(2010, 1, 30))));
            Assert.True(collector.Periods[5].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 07), new DateTime(2011, 1, 08))));
            Assert.True(collector.Periods[6].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 14), new DateTime(2011, 1, 15))));
            Assert.True(collector.Periods[7].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 21), new DateTime(2011, 1, 22))));
            Assert.True(collector.Periods[8].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 28), new DateTime(2011, 1, 29))));
        }         // CollectAllDayHoursTest
예제 #4
0
        public void Excluded_slot()
        {
            var filter = new CalendarPeriodCollectorFilter();

            filter.AddWorkingWeekDays();
            filter.CollectingHours.Add(new HourRange(8, 12));  // working hours
            filter.CollectingHours.Add(new HourRange(14, 18)); // working hours

            filter.ExcludePeriods.Add(new CalendarTimeRange(
                                          DateTime.Parse("2014-07-15T09:30"),
                                          DateTime.Parse("2014-07-15T10:30")
                                          ));

            var testPeriod = new CalendarTimeRange(new DateTime(2014, 7, 14), new DateTime(2014, 7, 31));

            Console.WriteLine("Calendar period collector of period: " + testPeriod);

            var collector = new CalendarPeriodCollector(filter, testPeriod);

            collector.CollectHours();
            foreach (ITimePeriod period in collector.Periods)
            {
                Console.WriteLine("Period: " + period);
            }

            Assert.IsFalse(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T10:00"))));
            Assert.IsTrue(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T09:00"))));
            Assert.IsTrue(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T11:00"))));
        }
        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))));
        }         // CollectMonthsTest
        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))));
        }         // CollectYearsTest
        public void CollectHoursMissingLastPeriodTest()
        {
            CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();

            filter.Months.Add(YearMonth.September);
            filter.WeekDays.Add(DayOfWeek.Monday);
            filter.WeekDays.Add(DayOfWeek.Tuesday);
            filter.WeekDays.Add(DayOfWeek.Wednesday);
            filter.WeekDays.Add(DayOfWeek.Thursday);
            filter.WeekDays.Add(DayOfWeek.Friday);
            filter.CollectingHours.Add(new HourRange(9, 17)); // working hours
            filter.ExcludePeriods.Add(new TimeBlock(new DateTime(2015, 9, 15, 00, 0, 0), new DateTime(2015, 9, 16, 0, 0, 0)));

            CalendarTimeRange       testPeriod = new CalendarTimeRange(new DateTime(2015, 9, 14, 9, 0, 0), new DateTime(2015, 9, 17, 18, 0, 0));
            CalendarPeriodCollector collector  = new CalendarPeriodCollector(filter, testPeriod);

            collector.CollectHours();
            Assert.Equal(3, collector.Periods.Count);
        } // CollectHoursMissingLastPeriodTest
예제 #8
0
파일: DateUtils.cs 프로젝트: JWPLAY/AUBE
        public static double NetworkDays(DateTime start, DateTime end,
                                         IEnumerable <DateTime> holidays      = null,
                                         ITimePeriodCollection holidayPeriods = null)
        {
            var startDay = new Day(start < end ? start : end);
            var endDay   = new Day(end > start ? end : start);

            if (startDay.Equals(endDay))
            {
                return(0);
            }

            var filter = new CalendarPeriodCollectorFilter();

            filter.AddWorkingWeekDays();
            if (holidays != null)
            {
                foreach (DateTime holiday in holidays)
                {
                    filter.ExcludePeriods.Add(new Day(holiday));
                }
            }
            if (holidayPeriods != null)
            {
                filter.ExcludePeriods.AddAll(holidayPeriods);
            }

            var testPeriod = new CalendarTimeRange(start, end);
            var collector  = new CalendarPeriodCollector(filter, testPeriod);

            collector.CollectDays();

            var networkDays = 0.0d;

            foreach (ICalendarTimeRange period in collector.Periods)
            {
                networkDays += Math.Round(period.Duration.TotalDays, 2);
            }
            return(networkDays);
        }
        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))));
        }         // CollectDaysTest
        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);
        } // CollectExcludePeriodTest
예제 #11
0
        }         // SelectPeriod

        // ----------------------------------------------------------------------
        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();
            }
        }         // Collect
 // ------------------------------------------------------------------------
 public LastDaysOfMonthPeriodCollector(CalendarPeriodCollectorFilter filter, ITimePeriod limits) :
     base(filter, limits)
 {
 }         // LastDaysOfMonthPeriodCollector