Exemple #1
0
        public void IncludeExcludeTest()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 17), new DateTime(2011, 4, 20)));

            // setup some periods to exclude
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 3, 22), new DateTime(2011, 3, 25)));
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 4, 1), new DateTime(2011, 4, 7)));
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 4, 15), new DateTime(2011, 4, 16)));

            // positive
            DateTime periodStart = new DateTime(2011, 3, 19);

            Assert.Equal(dateAdd.Add(periodStart, Duration.Hours(1)), new DateTime(2011, 3, 19, 1, 0, 0));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(4)), new DateTime(2011, 3, 26, 0, 0, 0));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(17)), new DateTime(2011, 4, 14));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(20)), new DateTime(2011, 4, 18));
            Assert.Null(dateAdd.Add(periodStart, Duration.Days(22)));

            // negative
            DateTime periodEnd = new DateTime(2011, 4, 18);

            Assert.Equal(dateAdd.Add(periodEnd, Duration.Hours(-1)), new DateTime(2011, 4, 17, 23, 0, 0));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-4)), new DateTime(2011, 4, 13));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-17)), new DateTime(2011, 3, 22));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-20)), new DateTime(2011, 3, 19));
            Assert.Null(dateAdd.Add(periodEnd, Duration.Days(-22)));
        }         // IncludeExcludeTest
Exemple #2
0
        public void ExcludePeriodTest()
        {
            DateAdd dateAdd = new DateAdd();

            // include periods
            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2013, 1, 1, 8, 0, 0), new DateTime(2013, 1, 1, 12, 0, 0)));
            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2013, 1, 1, 13, 0, 0), new DateTime(2013, 1, 1, 17, 0, 0)));

            // exclude interval
            TimeInterval interval = new TimeInterval(TimeSpec.MinPeriodDate, new DateTime(2013, 1, 1, 14, 0, 0),
                                                     IntervalEdge.Closed, IntervalEdge.Open);

            dateAdd.ExcludePeriods.Add(interval);
            DateTime?result = dateAdd.Add(dateAdd.IncludePeriods.Start, TimeSpan.FromHours(2), SeekBoundaryMode.Fill);

            Assert.NotNull(result);
            Assert.Equal(result, new DateTime(2013, 1, 1, 16, 0, 0).AddTicks(-1));

            // exclude time range
            dateAdd.ExcludePeriods.Clear();
            dateAdd.ExcludePeriods.Add(new TimeRange(interval.StartInterval, interval.EndInterval));
            result = dateAdd.Add(dateAdd.IncludePeriods.Start, TimeSpan.FromHours(2), SeekBoundaryMode.Fill);
            Assert.NotNull(result);
            Assert.Equal(result, new DateTime(2013, 1, 1, 16, 0, 0));
        } // ExcludePeriodTest
Exemple #3
0
        public void NoPeriodsTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            DateAdd dateAdd = new DateAdd();

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), test.Add(new TimeSpan(1, 0, 0, 0)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(-1, 0, 0, 0)), test.Add(new TimeSpan(-1, 0, 0, 0)));
            Assert.Equal(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0)), test.Subtract(new TimeSpan(1, 0, 0, 0)));
            Assert.Equal(dateAdd.Subtract(test, new TimeSpan(-1, 0, 0, 0)), test.Subtract(new TimeSpan(-1, 0, 0, 0)));
        }         // NoPeriodsTest
Exemple #4
0
        public void IncludeTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(new DateTime(2011, 4, 1), DateTime.MaxValue);
            DateAdd   dateAdd   = new DateAdd();

            dateAdd.IncludePeriods.Add(timeRange);

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), test.Add(new TimeSpan(1, 0, 0, 0)));
        }         // IncludeTest
Exemple #5
0
        public void IncludeExclude6Test()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 10), new DateTime(2011, 3, 20)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 12)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 18), new DateTime(2011, 3, 30)));

            DateTime test = new DateTime(2011, 3, 10);

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), new DateTime(2011, 3, 12));
            Assert.Equal(dateAdd.Add(test, Duration.Days(1)), new DateTime(2011, 3, 13));
        }         // IncludeExclude6Test
Exemple #6
0
        public void PeriodLimitsAddTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange1 = new TimeRange(new DateTime(2011, 4, 20), new DateTime(2011, 4, 25));
            TimeRange timeRange2 = new TimeRange(new DateTime(2011, 4, 30), DateTime.MaxValue);
            DateAdd   dateAdd    = new DateAdd();

            dateAdd.ExcludePeriods.Add(timeRange1);
            dateAdd.ExcludePeriods.Add(timeRange2);

            Assert.Equal(dateAdd.Add(test, new TimeSpan(8, 0, 0, 0)), timeRange1.End);
            Assert.Null(dateAdd.Add(test, new TimeSpan(20, 0, 0, 0)));
        }         // PeriodLimitsAddTest
Exemple #7
0
        public void IncludeExclude4Test()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 10), new DateTime(2011, 3, 20)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 10), new DateTime(2011, 3, 15)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 15), new DateTime(2011, 3, 20)));

            DateTime test = new DateTime(2011, 3, 10);

            Assert.Null(dateAdd.Add(test, TimeSpan.Zero));
            Assert.Null(dateAdd.Add(test, Duration.Days(1)));
            Assert.Null(dateAdd.Add(test, Duration.Days(5)));
        }         // IncludeExclude4Test
Exemple #8
0
        public void IncludeOutsideMaxTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(new DateTime(2011, 4, 20), DateTime.MaxValue);
            DateAdd   dateAdd   = new DateAdd();

            dateAdd.IncludePeriods.Add(timeRange);

            Assert.AreEqual(dateAdd.Add(test, TimeSpan.Zero), new DateTime(2011, 4, 20));
            Assert.AreEqual(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), new DateTime(2011, 4, 21));

            Assert.IsNull(dateAdd.Subtract(test, TimeSpan.Zero));
            Assert.IsNull(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0)));
        }         // IncludeOutsideMaxTest
Exemple #9
0
        public void ExcludeTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(new DateTime(2011, 4, 15), new DateTime(2011, 4, 20));
            DateAdd   dateAdd   = new DateAdd();

            dateAdd.ExcludePeriods.Add(timeRange);

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(2, 0, 0, 0)), test.Add(new TimeSpan(2, 0, 0, 0)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(3, 0, 0, 0)), timeRange.End);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(3, 0, 0, 0, 1)), timeRange.End.Add(new TimeSpan(0, 0, 0, 0, 1)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(5, 0, 0, 0)), timeRange.End.Add(new TimeSpan(2, 0, 0, 0)));
        }         // ExcludeTest
Exemple #10
0
        public void IncludeOutsideMinTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(DateTime.MinValue, new DateTime(2011, 4, 10));
            DateAdd   dateAdd   = new DateAdd();

            dateAdd.IncludePeriods.Add(timeRange);

            Assert.Null(dateAdd.Add(test, TimeSpan.Zero));
            Assert.Null(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)));

            Assert.Equal(dateAdd.Subtract(test, TimeSpan.Zero), new DateTime(2011, 4, 10));
            Assert.Equal(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0)), new DateTime(2011, 4, 9));
        }         // IncludeOutsideMinTest
Exemple #11
0
        public void IncludeSplitTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange1 = new TimeRange(new DateTime(2011, 4, 1), new DateTime(2011, 4, 15));
            TimeRange timeRange2 = new TimeRange(new DateTime(2011, 4, 20), new DateTime(2011, 4, 24));
            DateAdd   dateAdd    = new DateAdd();

            dateAdd.IncludePeriods.Add(timeRange1);
            dateAdd.IncludePeriods.Add(timeRange2);

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), test.Add(new TimeSpan(1, 0, 0, 0)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(5, 0, 0, 0)), timeRange2.Start.Add(new TimeSpan(2, 0, 0, 0)));
            Assert.Null(dateAdd.Add(test, new TimeSpan(15, 0, 0, 0)));
        }         // IncludeSplitTest
Exemple #12
0
        public void IncludeEqualsExcludeTest()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10)));

            DateTime test = new DateTime(2011, 3, 5);

            Assert.Null(dateAdd.Add(test, TimeSpan.Zero));
            Assert.Null(dateAdd.Add(test, new TimeSpan(1)));
            Assert.Null(dateAdd.Add(test, new TimeSpan(-1)));
            Assert.Null(dateAdd.Subtract(test, TimeSpan.Zero));
            Assert.Null(dateAdd.Subtract(test, new TimeSpan(1)));
            Assert.Null(dateAdd.Subtract(test, new TimeSpan(-1)));
        }         // IncludeEqualsExcludeTest
Exemple #13
0
        public void PeriodMomentTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(test, test);
            DateAdd   dateAdd   = new DateAdd();

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            dateAdd.IncludePeriods.Add(timeRange);
            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            dateAdd.ExcludePeriods.Add(timeRange);
            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            dateAdd.IncludePeriods.Clear();
            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            dateAdd.ExcludePeriods.Clear();
            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
        }         // PeriodMomentTest
Exemple #14
0
        // ----------------------------------------------------------------------
        public void DateAddSample()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 17), new DateTime(2011, 4, 20)));

            // setup some periods to exclude
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 3, 22), new DateTime(2011, 3, 25)));
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 4, 1), new DateTime(2011, 4, 7)));
            dateAdd.ExcludePeriods.Add(new TimeRange(
                                           new DateTime(2011, 4, 15), new DateTime(2011, 4, 16)));

            // positive
            DateTime dateDiffPositive = new DateTime(2011, 3, 19);
            DateTime?positive1        = dateAdd.Add(dateDiffPositive, Duration.Hours(1));

            Console.WriteLine("DateAdd Positive1: {0}", positive1);
            // > DateAdd Positive1: 19.03.2011 01:00:00
            DateTime?positive2 = dateAdd.Add(dateDiffPositive, Duration.Days(4));

            Console.WriteLine("DateAdd Positive2: {0}", positive2);
            // > DateAdd Positive2: 26.03.2011 00:00:00
            DateTime?positive3 = dateAdd.Add(dateDiffPositive, Duration.Days(17));

            Console.WriteLine("DateAdd Positive3: {0}", positive3);
            // > DateAdd Positive3: 14.04.2011 00:00:00
            DateTime?positive4 = dateAdd.Add(dateDiffPositive, Duration.Days(20));

            Console.WriteLine("DateAdd Positive4: {0}", positive4);
            // > DateAdd Positive4: 18.04.2011 00:00:00

            // negative
            DateTime dateDiffNegative = new DateTime(2011, 4, 18);
            DateTime?negative1        = dateAdd.Add(dateDiffNegative, Duration.Hours(-1));

            Console.WriteLine("DateAdd Negative1: {0}", negative1);
            // > DateAdd Negative1: 17.04.2011 23:00:00
            DateTime?negative2 = dateAdd.Add(dateDiffNegative, Duration.Days(-4));

            Console.WriteLine("DateAdd Negative2: {0}", negative2);
            // > DateAdd Negative2: 13.04.2011 00:00:00
            DateTime?negative3 = dateAdd.Add(dateDiffNegative, Duration.Days(-17));

            Console.WriteLine("DateAdd Negative3: {0}", negative3);
            // > DateAdd Negative3: 22.03.2011 00:00:00
            DateTime?negative4 = dateAdd.Add(dateDiffNegative, Duration.Days(-20));

            Console.WriteLine("DateAdd Negative4: {0}", negative4);
            // > DateAdd Negative4: 19.03.2011 00:00:00
        }         // DateAddSample
Exemple #15
0
        public void AllExcluded3Test()
        {
            DateTime test = new DateTime(2011, 4, 12);

            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 4, 10), new DateTime(2011, 4, 20)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 4, 15), new DateTime(2011, 4, 20)));

            Assert.Null(dateAdd.Add(test, new TimeSpan(3, 0, 0, 0)));
        }         // AllExcluded3Test
        }         // Week

        // ----------------------------------------------------------------------
        public DateTime CalculateNextStateChange(DateTime moment, WorkingState state)
        {
            DateTime?nextDate = null;

            while (nextDate.HasValue == false)
            {
                ITimePeriod           weekPeriod         = week.GetWeekPeriod(moment);
                ITimePeriodCollection weekExcludePeriods = GetWeekExcludePeriods(moment);

                // setup moment for the next search
                ITimePeriod nextExcludePeriod = FindNextPeriod(weekExcludePeriods, moment);
                if (nextExcludePeriod == null)
                {
                    moment = weekPeriod.End;
                    continue;
                }
                moment = nextExcludePeriod.End;

                // search next availabe date by adding zero
                DateAdd dateAdd = new DateAdd();
                dateAdd.IncludePeriods.Add(weekPeriod);
                dateAdd.ExcludePeriods.AddAll(weekExcludePeriods);

                // calculate the next avialable time
                nextDate = dateAdd.Add(moment, TimeSpan.Zero);
                if (!nextDate.HasValue)                   // not found in this week
                {
                    moment = weekPeriod.End.AddTicks(1);
                    continue;
                }

                switch (state)
                {
                case WorkingState.On:
                    break;

                case WorkingState.Off:
                    ITimePeriod nextPeriod = FindSuccessorPeriod(weekExcludePeriods, nextDate.Value);
                    if (nextPeriod == null)                               // search in next week
                    {
                        nextDate = null;
                        moment   = weekPeriod.End.AddTicks(1);
                    }
                    else
                    {
                        nextDate = nextPeriod.Start;
                    }
                    break;
                }
            }

            return(nextDate.Value);
        }         // CalculateNextStateChange
Exemple #17
0
        public void AllExcludedTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange = new TimeRange(new DateTime(2011, 4, 10), new DateTime(2011, 4, 20));
            DateAdd   dateAdd   = new DateAdd();

            dateAdd.IncludePeriods.Add(timeRange);
            dateAdd.ExcludePeriods.Add(timeRange);

            Assert.Null(dateAdd.Add(test, TimeSpan.Zero));
        }         // AllExcludedTest
Exemple #18
0
        public void ExcludeSplitTest()
        {
            DateTime test = new DateTime(2011, 4, 12);

            TimeRange timeRange1 = new TimeRange(new DateTime(2011, 4, 15), new DateTime(2011, 4, 20));
            TimeRange timeRange2 = new TimeRange(new DateTime(2011, 4, 22), new DateTime(2011, 4, 25));
            DateAdd   dateAdd    = new DateAdd();

            dateAdd.ExcludePeriods.Add(timeRange1);
            dateAdd.ExcludePeriods.Add(timeRange2);

            Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(2, 0, 0, 0)), test.Add(new TimeSpan(2, 0, 0, 0)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(3, 0, 0, 0)), timeRange1.End);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(4, 0, 0, 0)), timeRange1.End.Add(new TimeSpan(1, 0, 0, 0)));
            Assert.Equal(dateAdd.Add(test, new TimeSpan(5, 0, 0, 0)), timeRange2.End);
            Assert.Equal(dateAdd.Add(test, new TimeSpan(7, 0, 0, 0)), timeRange2.End.Add(new TimeSpan(2, 0, 0, 0)));
        }         // ExcludeSplitTest
Exemple #19
0
        public void IncludeExclude2Test()
        {
            DateAdd dateAdd = new DateAdd();

            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 1), new DateTime(2011, 3, 5)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10)));
            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 10), new DateTime(2011, 3, 15)));
            dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 15), new DateTime(2011, 3, 20)));
            dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 20), new DateTime(2011, 3, 25)));

            DateTime periodStart = new DateTime(2011, 3, 1);
            DateTime periodEnd   = new DateTime(2011, 3, 25);

            // add from start
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(1)), new DateTime(2011, 3, 2));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(4)), new DateTime(2011, 3, 10));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(5)), new DateTime(2011, 3, 11));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(9)), new DateTime(2011, 3, 20));
            Assert.Equal(dateAdd.Add(periodStart, Duration.Days(10)), new DateTime(2011, 3, 21));
            Assert.Null(dateAdd.Add(periodStart, Duration.Days(15)));
            // add from end
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-1)), new DateTime(2011, 3, 24));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-5)), new DateTime(2011, 3, 15));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-6)), new DateTime(2011, 3, 14));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-10)), new DateTime(2011, 3, 5));
            Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-11)), new DateTime(2011, 3, 4));
            Assert.Null(dateAdd.Add(periodEnd, Duration.Days(-15)));

            // subtract form end
            Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(1)), new DateTime(2011, 3, 24));
            Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(5)), new DateTime(2011, 3, 15));
            Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(6)), new DateTime(2011, 3, 14));
            Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(10)), new DateTime(2011, 3, 5));
            Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(11)), new DateTime(2011, 3, 4));
            Assert.Null(dateAdd.Subtract(periodStart, Duration.Days(15)));
            // subtract form start
            Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-1)), new DateTime(2011, 3, 2));
            Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-4)), new DateTime(2011, 3, 10));
            Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-5)), new DateTime(2011, 3, 11));
            Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-9)), new DateTime(2011, 3, 20));
            Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-10)), new DateTime(2011, 3, 21));
            Assert.Null(dateAdd.Subtract(periodStart, Duration.Days(-15)));
        }         // IncludeExclude2Test