Subtract() public method

public Subtract ( 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 PeriodLimitsSubtractTest()
        {
            DateTime test = new DateTime( 2011, 4, 30 );

            TimeRange timeRange1 = new TimeRange( new DateTime( 2011, 4, 20 ), new DateTime( 2011, 4, 25 ) );
            TimeRange timeRange2 = new TimeRange( DateTime.MinValue, new DateTime( 2011, 4, 10 ) );
            DateAdd dateAdd = new DateAdd();
            dateAdd.ExcludePeriods.Add( timeRange1 );
            dateAdd.ExcludePeriods.Add( timeRange2 );

            Assert.AreEqual( dateAdd.Subtract( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange1.Start );
            Assert.IsNull( dateAdd.Subtract( test, new TimeSpan( 20, 0, 0, 0 ) ) );
        }
Esempio n. 2
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. 3
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. 4
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. 5
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 ) ) );
        }