Add() public method

public Add ( System.DateTime start, System.TimeSpan offset, SeekBoundaryMode seekBoundaryMode = SeekBoundaryMode.Next ) : DateTime?
start System.DateTime
offset System.TimeSpan
seekBoundaryMode SeekBoundaryMode
return DateTime?
Esempio n. 1
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.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 2, 0, 0, 0 ) ), test.Add( new TimeSpan( 2, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ), timeRange1.End );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 4, 0, 0, 0 ) ), timeRange1.End.Add( new TimeSpan( 1, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange2.End );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 7, 0, 0, 0 ) ), timeRange2.End.Add( new TimeSpan( 2, 0, 0, 0 ) ) );
        }
Esempio n. 2
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.IsNull( dateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ) );
        }
Esempio n. 3
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.IsNull( dateAdd.Add( test, TimeSpan.Zero ) );
        }
Esempio n. 4
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.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 2, 0, 0, 0 ) ), test.Add( new TimeSpan( 2, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ), timeRange.End );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 3, 0, 0, 0, 1 ) ), timeRange.End.Add( new TimeSpan( 0, 0, 0, 0, 1 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange.End.Add( new TimeSpan( 2, 0, 0, 0 ) ) );
        }
Esempio n. 5
0
        public void PeriodMomentTest()
        {
            DateTime test = new DateTime( 2011, 4, 12 );

            TimeRange timeRange = new TimeRange( test, test );
            DateAdd dateAdd = new DateAdd();
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            dateAdd.IncludePeriods.Add( timeRange );
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            dateAdd.ExcludePeriods.Add( timeRange );
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            dateAdd.IncludePeriods.Clear();
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            dateAdd.ExcludePeriods.Clear();
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
        }
Esempio n. 6
0
        public void NoPeriodsTest()
        {
            DateTime test = new DateTime( 2011, 4, 12 );

            DateAdd dateAdd = new DateAdd();
            Assert.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Add( new TimeSpan( 1, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( -1, 0, 0, 0 ) ), test.Add( new TimeSpan( -1, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Subtract( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Subtract( new TimeSpan( 1, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Subtract( test, new TimeSpan( -1, 0, 0, 0 ) ), test.Subtract( new TimeSpan( -1, 0, 0, 0 ) ) );
        }
Esempio n. 7
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.AreEqual( dateAdd.Add( test, new TimeSpan( 8, 0, 0, 0 ) ), timeRange1.End );
            Assert.IsNull( dateAdd.Add( test, new TimeSpan( 20, 0, 0, 0 ) ) );
        }
Esempio n. 8
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.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Add( new TimeSpan( 1, 0, 0, 0 ) ) );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange2.Start.Add( new TimeSpan( 2, 0, 0, 0 ) ) );
            Assert.IsNull( dateAdd.Add( test, new TimeSpan( 15, 0, 0, 0 ) ) );
        }
Esempio n. 9
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.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), test );
            Assert.AreEqual( dateAdd.Add( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Add( new TimeSpan( 1, 0, 0, 0 ) ) );
        }
Esempio n. 10
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.AreEqual( dateAdd.Add( periodStart, Duration.Hours( 1 ) ), new DateTime( 2011, 3, 19, 1, 0, 0 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 4 ) ), new DateTime( 2011, 3, 26, 0, 0, 0 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 17 ) ), new DateTime( 2011, 4, 14 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 20 ) ), new DateTime( 2011, 4, 18 ) );
            Assert.IsNull( dateAdd.Add( periodStart, Duration.Days( 22 ) ) );

            // negative
            DateTime periodEnd = new DateTime( 2011, 4, 18 );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Hours( -1 ) ), new DateTime( 2011, 4, 17, 23, 0, 0 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -4 ) ), new DateTime( 2011, 4, 13 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -17 ) ), new DateTime( 2011, 3, 22 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -20 ) ), new DateTime( 2011, 3, 19 ) );
            Assert.IsNull( dateAdd.Add( periodEnd, Duration.Days( -22 ) ) );
        }
Esempio n. 11
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.IsNull( dateAdd.Add( test, TimeSpan.Zero ) );
            Assert.IsNull( dateAdd.Add( test, new TimeSpan( 1, 0, 0, 0 ) ) );

            Assert.AreEqual( dateAdd.Subtract( test, TimeSpan.Zero ), new DateTime( 2011, 4, 10 ) );
            Assert.AreEqual( dateAdd.Subtract( test, new TimeSpan( 1, 0, 0, 0 ) ), new DateTime( 2011, 4, 9 ) );
        }
Esempio n. 12
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.AreEqual( dateAdd.Add( test, TimeSpan.Zero ), new DateTime( 2011, 3, 12 ) );
            Assert.AreEqual( dateAdd.Add( test, Duration.Days( 1 ) ), new DateTime( 2011, 3, 13 ) );
        }
Esempio n. 13
0
        public void IncludeExclude5Test()
        {
            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, 15 ) ) );
            dateAdd.ExcludePeriods.Add( new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 30 ) ) );

            DateTime test = new DateTime( 2011, 3, 10 );
            Assert.IsNull( dateAdd.Add( test, TimeSpan.Zero ) );
            Assert.IsNull( dateAdd.Add( test, new TimeSpan( 1 ) ) );
            Assert.IsNull( dateAdd.Add( test, new TimeSpan( -1 ) ) );
            Assert.IsNull( dateAdd.Subtract( test, TimeSpan.Zero ) );
            Assert.IsNull( dateAdd.Subtract( test, new TimeSpan( 1 ) ) );
            Assert.IsNull( dateAdd.Subtract( test, new TimeSpan( -1 ) ) );
        }
Esempio n. 14
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.IsNull( dateAdd.Add( test, TimeSpan.Zero ) );
            Assert.IsNull( dateAdd.Add( test, Duration.Days( 1 ) ) );
            Assert.IsNull( dateAdd.Add( test, Duration.Days( 5 ) ) );
        }
Esempio n. 15
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.AreEqual( dateAdd.Add( periodStart, Duration.Days( 1 ) ), new DateTime( 2011, 3, 2 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 4 ) ), new DateTime( 2011, 3, 10 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 5 ) ), new DateTime( 2011, 3, 11 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 9 ) ), new DateTime( 2011, 3, 20 ) );
            Assert.AreEqual( dateAdd.Add( periodStart, Duration.Days( 10 ) ), new DateTime( 2011, 3, 21 ) );
            Assert.IsNull( dateAdd.Add( periodStart, Duration.Days( 15 ) ) );
            // add from end
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -1 ) ), new DateTime( 2011, 3, 24 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd,Duration.Days( -5 ) ), new DateTime( 2011, 3, 15 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -6 ) ), new DateTime( 2011, 3, 14 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -10 ) ), new DateTime( 2011, 3, 5 ) );
            Assert.AreEqual( dateAdd.Add( periodEnd, Duration.Days( -11 ) ), new DateTime( 2011, 3, 4 ) );
            Assert.IsNull( dateAdd.Add( periodEnd, Duration.Days( -15 ) ) );

            // subtract form end
            Assert.AreEqual( dateAdd.Subtract( periodEnd, Duration.Days( 1 ) ), new DateTime( 2011, 3, 24 ) );
            Assert.AreEqual( dateAdd.Subtract( periodEnd, Duration.Days( 5 ) ), new DateTime( 2011, 3, 15 ) );
            Assert.AreEqual( dateAdd.Subtract( periodEnd, Duration.Days( 6 ) ), new DateTime( 2011, 3, 14 ) );
            Assert.AreEqual( dateAdd.Subtract( periodEnd, Duration.Days( 10 ) ), new DateTime( 2011, 3, 5 ) );
            Assert.AreEqual( dateAdd.Subtract( periodEnd, Duration.Days( 11 ) ), new DateTime( 2011, 3, 4 ) );
            Assert.IsNull( dateAdd.Subtract( periodStart, Duration.Days( 15 ) ) );
            // subtract form start
            Assert.AreEqual( dateAdd.Subtract( periodStart, Duration.Days( -1 ) ), new DateTime( 2011, 3, 2 ) );
            Assert.AreEqual( dateAdd.Subtract( periodStart, Duration.Days( -4 ) ), new DateTime( 2011, 3, 10 ) );
            Assert.AreEqual( dateAdd.Subtract( periodStart, Duration.Days( -5 ) ), new DateTime( 2011, 3, 11 ) );
            Assert.AreEqual( dateAdd.Subtract( periodStart, Duration.Days( -9 ) ), new DateTime( 2011, 3, 20 ) );
            Assert.AreEqual( dateAdd.Subtract( periodStart, Duration.Days( -10 ) ), new DateTime( 2011, 3, 21 ) );
            Assert.IsNull( dateAdd.Subtract( periodStart, Duration.Days( -15 ) ) );
        }
Esempio n. 16
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
        }