Inheritance: TimeRange, ICalendarTimeRange
Esempio n. 1
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
        }
        public void CalendarGetGapTest()
        {
            // simmulation of some reservations
            TimePeriodCollection periods = new TimePeriodCollection();
            periods.Add( new Days( 2011, 3, 7, 2 ) );
            periods.Add( new Days( 2011, 3, 16, 2 ) );

            // the overall search range
            CalendarTimeRange limits = new CalendarTimeRange( new DateTime( 2011, 3, 4 ), new DateTime( 2011, 3, 21 ) );
            Days days = new Days( limits.Start, limits.Duration.Days + 1 );
            ITimePeriodCollection dayList = days.GetDays();
            foreach ( Day day in dayList )
            {
                if ( !limits.HasInside( day ) )
                {
                    continue; // outside of the search scope
                }
                if ( day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday )
                {
                    periods.Add( day ); // // exclude weekend day
                }
            }

            TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>( new TimeCalendar() );
            ITimePeriodCollection gaps = gapCalculator.GetGaps( periods, limits );

            Assert.AreEqual( gaps.Count, 4 );
            Assert.IsTrue( gaps[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 4 ), Duration.Days( 1 ) ) ) );
            Assert.IsTrue( gaps[ 1 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 9 ), Duration.Days( 3 ) ) ) );
            Assert.IsTrue( gaps[ 2 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 14 ), Duration.Days( 2 ) ) ) );
            Assert.IsTrue( gaps[ 3 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 18 ), Duration.Days( 1 ) ) ) );
        }
        }                  // OnVisitMonth

        // ----------------------------------------------------------------------
        protected override bool OnVisitDay(Day day, CalendarPeriodCollectorContext context)
        {
            if (context.Scope != CalendarPeriodCollectorContext.CollectType.Hour)
            {
                return(true);                // continue
            }

            if (Filter.CollectingHours.Count == 0 && Filter.CollectingDayHours.Count == 0)
            {
                foreach (Hour hour in day.GetHours())
                {
                    if (IsMatchingHour(hour, context) && CheckLimits(hour))
                    {
                        periods.Add(hour);
                    }
                }
            }
            else
            {
                if (IsMatchingDay(day, context))
                {
                    List <HourRange> collectingHours = new List <HourRange>(Filter.CollectingHours);
                    foreach (DayHourRange collectingDayHour in Filter.CollectingDayHours)
                    {
                        if (collectingDayHour.Day == day.DayOfWeek)
                        {
                            collectingHours.Add(collectingDayHour);
                        }
                    }
                    foreach (HourRange collectingHour in collectingHours)
                    {
                        DateTime          start = collectingHour.Start.GetDateTime(day.Start);
                        DateTime          end   = collectingHour.End.GetDateTime(day.Start);
                        CalendarTimeRange hours = new CalendarTimeRange(start, end, day.Calendar);
                        if (CheckExcludePeriods(hours) && CheckLimits(hours))
                        {
                            periods.Add(hours);
                        }
                    }
                }
            }

            return(false); // abort
        }                  // OnVisitDay
        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 CalendarTest()
 {
     TimeCalendar calendar = new TimeCalendar();
     CalendarTimeRange calendarTimeRange = new CalendarTimeRange( TimeRange.Anytime, calendar );
     Assert.AreEqual( calendarTimeRange.Calendar, calendar );
 }
        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 ) ) ) );
        }
        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 ) ) ) );
        }
        } // ComputeHashCode

        // ----------------------------------------------------------------------
        private bool HasSameData(CalendarTimeRange comp)
        {
            return(calendar.Equals(comp.calendar));
        } // HasSameData
Esempio n. 11
0
 // ----------------------------------------------------------------------
 private bool HasSameData( CalendarTimeRange comp )
 {
     return calendar.Equals( comp.calendar );
 }
Esempio n. 12
0
        public void TimeGapCalculatorSample()
        {
            // simulation of some reservations
            TimePeriodCollection reservations = new TimePeriodCollection();
            reservations.Add( new Days( 2011, 3, 7, 2 ) );
            reservations.Add( new Days( 2011, 3, 16, 2 ) );

            // the overall search range
            CalendarTimeRange searchLimits = new CalendarTimeRange( new DateTime( 2011, 3, 4 ), new DateTime( 2011, 3, 21 ) );

            // search the largest free time block
            ICalendarTimeRange largestFreeTimeBlock = FindLargestFreeTimeBlock( reservations, searchLimits );
            Console.WriteLine( "Largest free time: " + largestFreeTimeBlock );
            // > Largest free time: 09.03.2011 00:00:00 - 11.03.2011 23:59:59 | 2.23:59
        }
Esempio n. 13
0
        // ----------------------------------------------------------------------
        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;
        }
Esempio n. 14
0
        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 );
            }
        }
Esempio n. 15
0
        public void CalculateBusinessHoursSample()
        {
            CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 5, 1 ) );
            Console.WriteLine( "period: {0}", testPeriod );
            // > period: 01.03.2011 00:00:00 - 30.04.2011 23:59:59 | 60.23:59

            TimePeriodCollection holidays = new TimePeriodCollection();
            Console.WriteLine( "business hours without holidays: {0}", CalculateBusinessHours( testPeriod, holidays ) );
            // > business hours without holidays: 396

            holidays.Add( new Day( 2011, 3, 9 ) );       // day 3/9/2011
            Console.WriteLine( "business hours with holidays: {0}", CalculateBusinessHours( testPeriod, holidays ) );
            // > business hours with holidays: 387

            holidays.Add( new Days( 2011, 3, 16, 2 ) );  // days 16/9/2011 and 17/9/2011
            Console.WriteLine( "business hours with more holidays: {0}", CalculateBusinessHours( testPeriod, holidays ) );
            // > business hours with more holidays: 369

            holidays.Add( new Week( 2011, 13 ) );        // w/c 13 2011
            Console.WriteLine( "business hours with even more holidays: {0}", CalculateBusinessHours( testPeriod, holidays ) );
            // > business hours with even more holidays: 324
        }
Esempio n. 16
0
        // ----------------------------------------------------------------------
        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;
        }