public JsonResult Availabilities(int hotelId, IEnumerable<int> roomTypeIds, DateTime start, DateTime end, DateTime? startTime, DateTime? endTime, IEnumerable<Query> queries) { var startDT = start.MergeTime(startTime); var endDT = end.MergeTime(endTime); var manager = this.GetManagerFor<IRoomBookingManager>(); var results = manager.Search(hotelId, roomTypeIds, startDT, endDT).ToList(); var periods = new TimeRange(startDT, endDT, true); var collector = new CalendarPeriodCollector(new CalendarPeriodCollectorFilter(), periods); collector.CollectDays(); var days = collector.Periods.Select(d => d.Start).ToList(); days.Add(endDT); days.Insert(0, startDT); var tManager = this.GetManagerFor<IRoomTypeManager>(); var roomTypes = tManager.GetByIds(roomTypeIds).ToList(); var resultDTOs = results.Select(t => new RoomBookingDTO(queries, t)); var json = new AvailabilityResultDTO { DTSlots = days, RoomTypes = roomTypes.Select(t => new RoomTypeDTO { Id = t.Id, Name = t.Name, Rooms = t.Rooms.Where(r => r.HotelId == hotelId).Select(r => new RoomDTO(r)).ToList() }).ToList(), Bookings = resultDTOs }; return Json(json); }
public void MomentTest() { TimeRange period = new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 1 ) ); TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>(); ITimePeriodCollection periods = periodCombiner.CombinePeriods( new TimePeriodCollection { period } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( period ) ); }
public void MomentTest() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 5 ), new DateTime( 2011, 3, 05 ) ); TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( new TimePeriodCollection { period1, period2 } ); Assert.AreEqual( periods.Count, 0 ); }
public void MomentTest() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 5 ), new DateTime( 2011, 3, 05 ) ); TimePeriodSubtractor<TimeRange> periodSubtractor = new TimePeriodSubtractor<TimeRange>(); ITimePeriodCollection periods = periodSubtractor.SubtractPeriods( new TimePeriodCollection { period1 }, new TimePeriodCollection { period2 } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( period1 ) ); }
public void CombineTest() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 05 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 10 ) ); TimePeriodSubtractor<TimeRange> periodSubtractor = new TimePeriodSubtractor<TimeRange>(); ITimePeriodCollection periods = periodSubtractor.SubtractPeriods( new TimePeriodCollection { period1, period2 }, new TimePeriodCollection() ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( period1.Start, period2.End ) ) ); }
public void CopyConstructorTest() { TimeRange source = new TimeRange( start, start.AddHours( 1 ), true ); TimeRange copy = new TimeRange( source ); Assert.AreEqual( source.Start, copy.Start ); Assert.AreEqual( source.End, copy.End ); Assert.AreEqual( source.IsReadOnly, copy.IsReadOnly ); Assert.AreEqual( source, copy ); }
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 ) ); }
public void AddTimeRangeTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodChain timePeriods = new TimePeriodChain( schoolDay ); TimeRange timeRange = new TimeRange( schoolDay.Lesson1.Start, schoolDay.Lesson1.End ); timePeriods.Add( timeRange ); Assert.AreEqual( timePeriods.Last, timeRange ); }
public void Pattern3Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 05 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 15 ) ); TimeRange period4 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 20 ) ); TimeRange period5 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 25 ) ); TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>(); ITimePeriodCollection periods = periodCombiner.CombinePeriods( new TimePeriodCollection { period1, period2, period3, period4, period5 } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 25 ) ) ) ); }
public void IntersectionPeriodsTest() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 4, 01 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 10 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 25 ) ); TimePeriodSubtractor<TimeRange> periodSubtractor = new TimePeriodSubtractor<TimeRange>(); ITimePeriodCollection periods = periodSubtractor.SubtractPeriods( new TimePeriodCollection { period1 }, new TimePeriodCollection { period2, period3 } ); Assert.AreEqual( periods.Count, 3 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 05 ) ) ) ); Assert.IsTrue( periods[ 1 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 20 ) ) ) ); Assert.IsTrue( periods[ 2 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 25 ), new DateTime( 2011, 4, 01 ) ) ) ); }
public void MultipeTouchingIntersection1Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 20 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 25 ) ); TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( new TimePeriodCollection { period1, period2, period3 } ); Assert.AreEqual( periods.Count, 2 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 10 ) ) ) ); Assert.IsTrue( periods[ 1 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 20 ) ) ) ); }
public void MultipeTouchingIntersection2Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 10 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 06 ), new DateTime( 2011, 3, 10 ) ); TimeRange period4 = new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 14 ) ); TimeRange period5 = new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 16 ) ); TimeRange period6 = new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 20 ) ); TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( new TimePeriodCollection { period1, period2, period3, period4, period5, period6 } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 16 ) ) ) ); }
public void TimePeriodCanSerialize() { System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL"); var range = new Itenso.TimePeriod.TimeRange(); // range.m // "01/01/0001 00:00:00 - 31/12/9999 23:59:59 | 3652058.23:59" Assert.Equal("1-1-0001 00:00:00 - 31-12-9999 23:59:59 | 3652058.23:59", range.ToString()); var s = range.ToString(); var dateTimeStrings = s.Split('|')[0].Split(" - "); var l = new TimeRange(DateTime.Parse(dateTimeStrings[0], new CultureInfo("nl-NL")), DateTime.Parse(dateTimeStrings[1], new CultureInfo("nl-NL"))); Assert.Equal(new DateTime(1, 1, 1), l.Start); Assert.Equal(new DateTime(9999, 12, 31, 23, 59, 59), l.End); }
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 ) ) ); }
// ---------------------------------------------------------------------- public TimeRangePeriodRelationTestData( DateTime start, DateTime end, TimeSpan offset ) { if ( offset <= TimeSpan.Zero ) { throw new ArgumentOutOfRangeException( "offset" ); } Reference = new TimeRange( start, end, true ); DateTime beforeEnd = start.Subtract( offset ); DateTime beforeStart = beforeEnd.Subtract( Reference.Duration ); DateTime insideStart = start.Add( offset ); DateTime insideEnd = end.Subtract( offset ); DateTime afterStart = end.Add( offset ); DateTime afterEnd = afterStart.Add( Reference.Duration ); After = new TimeRange( beforeStart, beforeEnd, true ); StartTouching = new TimeRange( beforeStart, start, true ); StartInside = new TimeRange( beforeStart, insideStart, true ); InsideStartTouching = new TimeRange( start, afterStart, true ); EnclosingStartTouching = new TimeRange( start, insideEnd, true ); Enclosing = new TimeRange( insideStart, insideEnd, true ); EnclosingEndTouching = new TimeRange( insideStart, end, true ); ExactMatch = new TimeRange( start, end, true ); Inside = new TimeRange( beforeStart, afterEnd, true ); InsideEndTouching = new TimeRange( beforeStart, end, true ); EndInside = new TimeRange( insideEnd, afterEnd, true ); EndTouching = new TimeRange( end, afterEnd, true ); Before = new TimeRange( afterStart, afterEnd, true ); allPeriods.Add( Reference ); allPeriods.Add( After ); allPeriods.Add( StartTouching ); allPeriods.Add( StartInside ); allPeriods.Add( InsideStartTouching ); allPeriods.Add( EnclosingStartTouching ); allPeriods.Add( Enclosing ); allPeriods.Add( EnclosingEndTouching ); allPeriods.Add( ExactMatch ); allPeriods.Add( Inside ); allPeriods.Add( InsideEndTouching ); allPeriods.Add( EndInside ); allPeriods.Add( EndTouching ); allPeriods.Add( Before ); }
public void CopyTest() { TimeRange readOnlyTimeRange = new TimeRange( start, end ); Assert.AreEqual( readOnlyTimeRange.Copy( TimeSpan.Zero ), readOnlyTimeRange ); TimeRange timeRange = new TimeRange( start, end ); Assert.AreEqual( timeRange.Start, start ); Assert.AreEqual( timeRange.End, end ); ITimeRange noMoveTimeRange = timeRange.Copy( TimeSpan.Zero ); Assert.AreEqual( noMoveTimeRange.Start, start ); Assert.AreEqual( noMoveTimeRange.End, end ); Assert.AreEqual( noMoveTimeRange.Duration, duration ); TimeSpan forwardOffset = new TimeSpan( 2, 30, 15 ); ITimeRange forwardTimeRange = timeRange.Copy( forwardOffset ); Assert.AreEqual( forwardTimeRange.Start, start.Add( forwardOffset ) ); Assert.AreEqual( forwardTimeRange.End, end.Add( forwardOffset ) ); TimeSpan backwardOffset = new TimeSpan( -1, 10, 30 ); ITimeRange backwardTimeRange = timeRange.Copy( backwardOffset ); Assert.AreEqual( backwardTimeRange.Start, start.Add( backwardOffset ) ); Assert.AreEqual( backwardTimeRange.End, end.Add( backwardOffset ) ); }
public void NotCombinedIntersection4Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 05 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 06 ) ); TimeRange period4 = new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 10 ) ); TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( new TimePeriodCollection { period1, period2, period3, period4 }, false ); Assert.AreEqual( periods.Count, 4 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 04 ) ) ) ); Assert.IsTrue( periods[ 1 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 05 ) ) ) ); Assert.IsTrue( periods[ 2 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 06 ) ) ) ); Assert.IsTrue( periods[ 3 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 06 ), new DateTime( 2011, 3, 10 ) ) ) ); }
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 ); }
public void TwoPeriodsTouchingTest() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 20 ) ); TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>(); ITimePeriodCollection periods = periodCombiner.CombinePeriods( new TimePeriodCollection { period1, period2 } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 1 ), new DateTime( 2011, 3, 20 ) ) ) ); }
public void Pattern6Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 08 ) ); TimeRange period3 = new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 18 ) ); TimeRange period4 = new TimeRange( new DateTime( 2011, 3, 18 ), new DateTime( 2011, 3, 22 ) ); TimeRange period5 = new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 24 ) ); TimeRange period6 = new TimeRange( new DateTime( 2011, 3, 26 ), new DateTime( 2011, 3, 30 ) ); TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>(); ITimePeriodCollection periods = periodCombiner.CombinePeriods( new TimePeriodCollection { period1, period2, period3, period4, period5, period6 } ); Assert.AreEqual( periods.Count, 3 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ) ) ); Assert.IsTrue( periods[ 1 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 24 ) ) ) ); Assert.IsTrue( periods[ 2 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 26 ), new DateTime( 2011, 3, 30 ) ) ) ); }
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 ) ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange1 ); calendarDateAdd.ExcludePeriods.Add( timeRange2 ); Assert.AreEqual( calendarDateAdd.Subtract( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange1.Start ); Assert.IsNull( calendarDateAdd.Subtract( test, new TimeSpan( 20, 0, 0, 0 ) ) ); }
public void IntersectionPeriodsDateTimeTest() { DateTime now = ClockProxy.Clock.Now; TimeRange timeRange1 = new TimeRange( new DateTime( now.Year, now.Month, 8 ), new DateTime( now.Year, now.Month, 18 ) ); TimeRange timeRange2 = new TimeRange( new DateTime( now.Year, now.Month, 10 ), new DateTime( now.Year, now.Month, 11 ) ); TimeRange timeRange3 = new TimeRange( new DateTime( now.Year, now.Month, 13 ), new DateTime( now.Year, now.Month, 15 ) ); TimeRange timeRange4 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 14 ) ); TimeRange timeRange5 = new TimeRange( new DateTime( now.Year, now.Month, 16 ), new DateTime( now.Year, now.Month, 17 ) ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( timeRange1 ); timePeriods.Add( timeRange2 ); timePeriods.Add( timeRange3 ); timePeriods.Add( timeRange4 ); timePeriods.Add( timeRange5 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange1.Start ).Count, 1 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange1.End ).Count, 1 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange2.Start ).Count, 3 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange2.End ).Count, 3 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange3.Start ).Count, 3 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange3.End ).Count, 2 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange4.Start ).Count, 2 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange4.End ).Count, 3 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange5.Start ).Count, 2 ); Assert.AreEqual( timePeriods.IntersectionPeriods( timeRange5.End ).Count, 2 ); DateTime test1 = timeRange1.Start.AddMilliseconds( -1 ); ITimePeriodCollection insidePeriods1 = timePeriods.IntersectionPeriods( test1 ); Assert.AreEqual( insidePeriods1.Count, 0 ); DateTime test2 = timeRange1.End.AddMilliseconds( 1 ); ITimePeriodCollection insidePeriods2 = timePeriods.IntersectionPeriods( test2 ); Assert.AreEqual( insidePeriods2.Count, 0 ); DateTime test3 = new DateTime( now.Year, now.Month, 12 ); ITimePeriodCollection insidePeriods3 = timePeriods.IntersectionPeriods( test3 ); Assert.AreEqual( insidePeriods3.Count, 2 ); DateTime test4 = new DateTime( now.Year, now.Month, 14 ); ITimePeriodCollection insidePeriods4 = timePeriods.IntersectionPeriods( test4 ); Assert.AreEqual( insidePeriods4.Count, 3 ); }
public void ContainsPeriodTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodCollection timePeriods = new TimePeriodCollection( schoolDay ); TimeRange timeRange = new TimeRange( schoolDay.Lesson1.Start, schoolDay.Lesson1.End ); Assert.IsFalse( timePeriods.Contains( timeRange ) ); Assert.IsTrue( timePeriods.ContainsPeriod( timeRange ) ); timePeriods.Add( timeRange ); Assert.IsTrue( timePeriods.Contains( timeRange ) ); Assert.IsTrue( timePeriods.ContainsPeriod( timeRange ) ); }
public void ExcludeTest() { DateTime test = new DateTime( 2011, 4, 12 ); TimeRange timeRange = new TimeRange( new DateTime( 2011, 4, 15 ), new DateTime( 2011, 4, 20 ) ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange ); Assert.AreEqual( calendarDateAdd.Add( test, TimeSpan.Zero ), test ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 2, 0, 0, 0 ) ), test.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ), timeRange.End ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 3, 0, 0, 0, 1 ) ), timeRange.End.Add( new TimeSpan( 0, 0, 0, 0, 1 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange.End.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); }
// ---------------------------------------------------------------------- protected virtual void StartEvaluation() { if ( periods.Count > 0 ) { TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection(); timeLineMoments.AddAll( periods ); if ( timeLineMoments.Count > 1 ) { int periodCount = 0; for ( int i = 0; i < timeLineMoments.Count - 1; i++ ) { ITimeLineMoment start = timeLineMoments[ i ]; ITimeLineMoment end = timeLineMoments[ i + 1 ]; if ( i == 0 ) { periodCount += start.StartCount; periodCount -= start.EndCount; } ITimePeriod period = new TimeRange( MapPeriodStart( start.Moment ), MapPeriodEnd( end.Moment ) ); if ( !( IgnoreEmptyPeriods && period.IsMoment ) ) { if ( EvaluatePeriod( period, periodCount ) == false ) { break; } } periodCount += end.StartCount; periodCount -= end.EndCount; } } } }
public void SingleIntersection3Test() { TimeRange period1 = new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 20 ) ); TimeRange period2 = new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 20 ) ); TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( new TimePeriodCollection { period1, period2 } ); Assert.AreEqual( periods.Count, 1 ); Assert.IsTrue( periods[ 0 ].IsSamePeriod( new TimeRange( new DateTime( 2011, 3, 10 ), new DateTime( 2011, 3, 20 ) ) ) ); }
public void OverlapPeriodsTest() { DateTime now = ClockProxy.Clock.Now; TimeRange timeRange1 = new TimeRange( new DateTime( now.Year, now.Month, 8 ), new DateTime( now.Year, now.Month, 18 ) ); TimeRange timeRange2 = new TimeRange( new DateTime( now.Year, now.Month, 10 ), new DateTime( now.Year, now.Month, 11 ) ); TimeRange timeRange3 = new TimeRange( new DateTime( now.Year, now.Month, 13 ), new DateTime( now.Year, now.Month, 15 ) ); TimeRange timeRange4 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 13 ) ); TimeRange timeRange5 = new TimeRange( new DateTime( now.Year, now.Month, 15 ), new DateTime( now.Year, now.Month, 17 ) ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( timeRange1 ); timePeriods.Add( timeRange2 ); timePeriods.Add( timeRange3 ); timePeriods.Add( timeRange4 ); timePeriods.Add( timeRange5 ); Assert.AreEqual( timePeriods.OverlapPeriods( timeRange1 ).Count, 5 ); Assert.AreEqual( timePeriods.OverlapPeriods( timeRange2 ).Count, 3 ); Assert.AreEqual( timePeriods.OverlapPeriods( timeRange3 ).Count, 2 ); Assert.AreEqual( timePeriods.OverlapPeriods( timeRange4 ).Count, 3 ); Assert.AreEqual( timePeriods.OverlapPeriods( timeRange5 ).Count, 2 ); ITimeRange test1 = timeRange1.Copy( new TimeSpan( 100, 0, 0, 0 ).Negate() ); ITimePeriodCollection insidePeriods1 = timePeriods.OverlapPeriods( test1 ); Assert.AreEqual( insidePeriods1.Count, 0 ); ITimeRange test2 = timeRange1.Copy( new TimeSpan( 100, 0, 0, 0 ) ); ITimePeriodCollection insidePeriods2 = timePeriods.OverlapPeriods( test2 ); Assert.AreEqual( insidePeriods2.Count, 0 ); TimeRange test3 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 11 ) ); ITimePeriodCollection insidePeriods3 = timePeriods.OverlapPeriods( test3 ); Assert.AreEqual( insidePeriods3.Count, 3 ); TimeRange test4 = new TimeRange( new DateTime( now.Year, now.Month, 14 ), new DateTime( now.Year, now.Month, 17 ) ); ITimePeriodCollection insidePeriods4 = timePeriods.OverlapPeriods( test4 ); Assert.AreEqual( insidePeriods4.Count, 3 ); }
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 ) ) ); }
public void RelationPeriodsTest() { DateTime now = ClockProxy.Clock.Now; TimeRange timeRange1 = new TimeRange( new DateTime( now.Year, now.Month, 8 ), new DateTime( now.Year, now.Month, 18 ) ); TimeRange timeRange2 = new TimeRange( new DateTime( now.Year, now.Month, 10 ), new DateTime( now.Year, now.Month, 11 ) ); TimeRange timeRange3 = new TimeRange( new DateTime( now.Year, now.Month, 13 ), new DateTime( now.Year, now.Month, 15 ) ); TimeRange timeRange4 = new TimeRange( new DateTime( now.Year, now.Month, 9 ), new DateTime( now.Year, now.Month, 14 ) ); TimeRange timeRange5 = new TimeRange( new DateTime( now.Year, now.Month, 16 ), new DateTime( now.Year, now.Month, 17 ) ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( timeRange1 ); timePeriods.Add( timeRange2 ); timePeriods.Add( timeRange3 ); timePeriods.Add( timeRange4 ); timePeriods.Add( timeRange5 ); Assert.AreEqual( timePeriods.RelationPeriods( timeRange1, PeriodRelation.ExactMatch ).Count, 1 ); Assert.AreEqual( timePeriods.RelationPeriods( timeRange2, PeriodRelation.ExactMatch ).Count, 1 ); Assert.AreEqual( timePeriods.RelationPeriods( timeRange3, PeriodRelation.ExactMatch ).Count, 1 ); Assert.AreEqual( timePeriods.RelationPeriods( timeRange4, PeriodRelation.ExactMatch ).Count, 1 ); Assert.AreEqual( timePeriods.RelationPeriods( timeRange5, PeriodRelation.ExactMatch ).Count, 1 ); // all Assert.AreEqual( timePeriods.RelationPeriods( new TimeRange( new DateTime( now.Year, now.Month, 7 ), new DateTime( now.Year, now.Month, 19 ) ), PeriodRelation.Enclosing ).Count, 5 ); // timerange3 Assert.AreEqual( timePeriods.RelationPeriods( new TimeRange( new DateTime( now.Year, now.Month, 11 ), new DateTime( now.Year, now.Month, 16 ) ), PeriodRelation.Enclosing ).Count, 1 ); }
public void IsSamePeriodTest() { SchoolDay schoolDay = new SchoolDay(); TimeRange manualRange = new TimeRange( schoolDay.Start, schoolDay.End ); Assert.IsTrue( schoolDay.IsSamePeriod( schoolDay ) ); Assert.IsTrue( schoolDay.IsSamePeriod( manualRange ) ); Assert.IsTrue( manualRange.IsSamePeriod( schoolDay ) ); Assert.IsFalse( schoolDay.IsSamePeriod( TimeBlock.Anytime ) ); Assert.IsFalse( manualRange.IsSamePeriod( TimeBlock.Anytime ) ); schoolDay.RemoveAt( 0 ); Assert.IsFalse( schoolDay.IsSamePeriod( manualRange ) ); Assert.IsFalse( manualRange.IsSamePeriod( schoolDay ) ); }
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 ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange1 ); calendarDateAdd.ExcludePeriods.Add( timeRange2 ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 8, 0, 0, 0 ) ), timeRange1.End ); Assert.IsNull( calendarDateAdd.Add( test, new TimeSpan( 20, 0, 0, 0 ) ) ); }