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 ) ) ) ); }
public void GetGapTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodCollection excludePeriods = new TimePeriodCollection(); TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>(); excludePeriods.AddAll( schoolDay ); Assert.AreEqual( gapCalculator.GetGaps( excludePeriods ).Count, 0 ); Assert.AreEqual( gapCalculator.GetGaps( excludePeriods, schoolDay ).Count, 0 ); excludePeriods.Clear(); excludePeriods.Add( schoolDay.Lesson1 ); excludePeriods.Add( schoolDay.Lesson2 ); excludePeriods.Add( schoolDay.Lesson3 ); excludePeriods.Add( schoolDay.Lesson4 ); ITimePeriodCollection gaps2 = gapCalculator.GetGaps( excludePeriods ); Assert.AreEqual( gaps2.Count, 3 ); Assert.IsTrue( gaps2[ 0 ].IsSamePeriod( schoolDay.Break1 ) ); Assert.IsTrue( gaps2[ 1 ].IsSamePeriod( schoolDay.Break2 ) ); Assert.IsTrue( gaps2[ 2 ].IsSamePeriod( schoolDay.Break3 ) ); TimeRange testRange3 = new TimeRange( schoolDay.Lesson1.Start, schoolDay.Lesson4.End ); ITimePeriodCollection gaps3 = gapCalculator.GetGaps( excludePeriods, testRange3 ); Assert.AreEqual( gaps3.Count, 3 ); Assert.IsTrue( gaps3[ 0 ].IsSamePeriod( schoolDay.Break1 ) ); Assert.IsTrue( gaps3[ 1 ].IsSamePeriod( schoolDay.Break2 ) ); Assert.IsTrue( gaps3[ 2 ].IsSamePeriod( schoolDay.Break3 ) ); TimeRange testRange4 = new TimeRange( schoolDay.Start.AddHours( -1 ), schoolDay.End.AddHours( 1 ) ); ITimePeriodCollection gaps4 = gapCalculator.GetGaps( excludePeriods, testRange4 ); Assert.AreEqual( gaps4.Count, 5 ); Assert.IsTrue( gaps4[ 0 ].IsSamePeriod( new TimeRange( testRange4.Start, schoolDay.Start ) ) ); Assert.IsTrue( gaps4[ 1 ].IsSamePeriod( schoolDay.Break1 ) ); Assert.IsTrue( gaps4[ 2 ].IsSamePeriod( schoolDay.Break2 ) ); Assert.IsTrue( gaps4[ 3 ].IsSamePeriod( schoolDay.Break3 ) ); Assert.IsTrue( gaps4[ 4 ].IsSamePeriod( new TimeRange( testRange4.End, testRange3.End ) ) ); excludePeriods.Clear(); excludePeriods.Add( schoolDay.Lesson1 ); ITimePeriodCollection gaps8 = gapCalculator.GetGaps( excludePeriods, schoolDay.Lesson1 ); Assert.AreEqual( gaps8.Count, 0 ); TimeRange testRange9 = new TimeRange( schoolDay.Lesson1.Start.Subtract( new TimeSpan( 1 ) ), schoolDay.Lesson1.End.Add( new TimeSpan( 1 ) ) ); ITimePeriodCollection gaps9 = gapCalculator.GetGaps( excludePeriods, testRange9 ); Assert.AreEqual( gaps9.Count, 2 ); Assert.AreEqual( gaps9[ 0 ].Duration, new TimeSpan( 1 ) ); Assert.AreEqual( gaps9[ 1 ].Duration, new TimeSpan( 1 ) ); }
public void TimeRangeCombinedPeriodsTest() { TimePeriodCollection periods = new TimePeriodCollection(); DateTime date1 = ClockProxy.Clock.Now; DateTime date2 = date1.AddDays( 1 ); DateTime date3 = date2.AddDays( 1 ); periods.Add( new TimeRange( date1, date2 ) ); periods.Add( new TimeRange( date2, date3 ) ); TimeLine<TimeRange> timeLine = new TimeLine<TimeRange>( periods, null, null ); ITimePeriodCollection combinedPeriods = timeLine.CombinePeriods(); Assert.AreEqual( 1, combinedPeriods.Count ); Assert.AreEqual( new TimeRange( date1, date3 ), combinedPeriods[ 0 ] ); }
public void AddTest() { TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.AreEqual( timePeriods.Count, 0 ); timePeriods.Add( new TimeRange() ); Assert.AreEqual( timePeriods.Count, 1 ); timePeriods.Add( new TimeRange() ); Assert.AreEqual( timePeriods.Count, 2 ); timePeriods.Clear(); Assert.AreEqual( timePeriods.Count, 0 ); }
} // IntersectPeriods // ---------------------------------------------------------------------- public ITimePeriodCollection CalculateGaps() { // exclude periods TimePeriodCollection gapPeriods = new TimePeriodCollection(); foreach (ITimePeriod period in periods) { if (limits.IntersectsWith(period)) { gapPeriods.Add(new TimeRange(period)); } } ITimeLineMomentCollection timeLineMoments = GetTimeLineMoments(gapPeriods); if (timeLineMoments.Count == 0) { return(new TimePeriodCollection { limits }); } T range = new T(); range.Setup(MapPeriodStart(limits.Start), MapPeriodEnd(limits.End)); return(CalculateGaps(range, timeLineMoments)); } // CalculateGaps
// ---------------------------------------------------------------------- public ITimePeriodCollection GetYears() { TimePeriodCollection years = new TimePeriodCollection(); for ( int i = 0; i < YearCount; i++ ) { years.Add( new Year( StartYear + i, Calendar ) ); } return years; }
// ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); for ( int i = 0; i < WeekCount; i++ ) { weeks.Add( new Week( Year, StartWeek + i, Calendar ) ); } return weeks; }
} // GetTimeLineMoments // ---------------------------------------------------------------------- private static ITimePeriodCollection CombinePeriods(ITimeLineMomentCollection timeLineMoments) { TimePeriodCollection periods = new TimePeriodCollection(); if (timeLineMoments.IsEmpty) { return(periods); } // search for periods int itemIndex = 0; while (itemIndex < timeLineMoments.Count) { ITimeLineMoment periodStart = timeLineMoments[itemIndex]; if (periodStart.StartCount == 0) { throw new InvalidOperationException(); } // search next period end // use balancing to handle overlapping periods int balance = periodStart.StartCount; ITimeLineMoment periodEnd = null; while (itemIndex < timeLineMoments.Count - 1 && balance > 0) { itemIndex++; periodEnd = timeLineMoments[itemIndex]; balance += periodEnd.StartCount; balance -= periodEnd.EndCount; } if (periodEnd == null) { throw new InvalidOperationException(); } if (periodEnd.StartCount > 0) // touching { itemIndex++; continue; } // found a period if (itemIndex < timeLineMoments.Count) { T period = new T(); period.Setup(periodStart.Moment, periodEnd.Moment); periods.Add(period); } itemIndex++; } return(periods); } // CombinePeriods
} // Year // ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); foreach (var week in Enumerable.Range(1, BroadcastCalendarTool.GetWeeksOfYear(year))) { weeks.Add(new BroadcastWeek(year, week)); } return(weeks); } // GetWeeks
} // Years // ---------------------------------------------------------------------- public ITimePeriodCollection GetYears() { TimePeriodCollection years = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, YearCount)) { years.Add(new Year(BaseYear + i, Calendar)); } return(years); } // GetYears
// ---------------------------------------------------------------------- public ITimePeriodCollection GetHours() { TimePeriodCollection hours = new TimePeriodCollection(); DateTime startHour = new DateTime( StartYear, StartMonth, StartDay, StartHour, 0, 0 ); for ( int i = 0; i < HourCount; i++ ) { hours.Add( new Hour( startHour.AddHours( i ), Calendar ) ); } return hours; }
} // Years // ---------------------------------------------------------------------- public ITimePeriodCollection GetYears() { TimePeriodCollection years = new TimePeriodCollection(); for (int i = 0; i < YearCount; i++) { years.Add(new Year(StartYear + i, Calendar)); } return(years); } // GetYears
} // GetWeeks // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for (int month = 1; month <= TimeSpec.MonthsPerYear; month++) { months.Add(new BroadcastMonth(year, (YearMonth)month)); } return(months); } // GetMonths
} // GetWeeks // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); foreach (var month in Enumerable.Range(1, TimeSpec.MonthsPerYear)) { months.Add(new BroadcastMonth(year, (YearMonth)month)); } return(months); } // GetMonths
// ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = new DateTime( Start.Year, Start.Month, Start.Day ); for ( int day = 0; day < TimeSpec.DaysPerWeek; day++ ) { days.Add( new Day( startDate.AddDays( day ), Calendar ) ); } return days; }
// ---------------------------------------------------------------------- public ITimePeriodCollection GetMinutes() { TimePeriodCollection minutes = new TimePeriodCollection(); DateTime startMinute = new DateTime( StartYear, StartMonth, StartDay, StartHour, StartMinute, 0 ); for ( int i = 0; i < MinuteCount; i++ ) { minutes.Add( new Minute( startMinute.AddMinutes( i ), Calendar ) ); } return minutes; }
// ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDay = new DateTime( StartYear, StartMonth, StartDay ); for ( int i = 0; i < DayCount; i++ ) { days.Add( new Day( startDay.AddDays( i ), Calendar ) ); } return days; }
} // Weeks // ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, WeekCount)) { weeks.Add(new Week(Year, StartWeek + i, Calendar)); } return(weeks); } // GetWeeks
} // Weeks // ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); for (int i = 0; i < WeekCount; i++) { weeks.Add(new Week(Year, StartWeek + i, Calendar)); } return(weeks); } // GetWeeks
} // Days // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDay = new DateTime(StartYear, StartMonth, StartDay); foreach (var i in Enumerable.Range(0, DayCount)) { days.Add(new Day(startDay.AddDays(i), Calendar)); } return(days); } // GetDays
} // Hours // ---------------------------------------------------------------------- public ITimePeriodCollection GetHours() { TimePeriodCollection hours = new TimePeriodCollection(); DateTime startHour = new DateTime(StartYear, StartMonth, StartDay, StartHour, 0, 0); for (int i = 0; i < HourCount; i++) { hours.Add(new Hour(startHour.AddHours(i), Calendar)); } return(hours); } // GetHours
} // Year // ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); int weekCount = BroadcastCalendarTool.GetWeeksOfYear(year); for (int week = 1; week <= weekCount; week++) { weeks.Add(new BroadcastWeek(year, week)); } return(weeks); } // GetWeeks
} // Minutes // ---------------------------------------------------------------------- public ITimePeriodCollection GetMinutes() { TimePeriodCollection minutes = new TimePeriodCollection(); DateTime startMinute = new DateTime(StartYear, StartMonth, StartDay, StartHour, StartMinute, 0); foreach (var i in Enumerable.Range(0, MinuteCount)) { minutes.Add(new Minute(startMinute.AddMinutes(i), Calendar)); } return(minutes); } // GetMinutes
} // Minutes // ---------------------------------------------------------------------- public ITimePeriodCollection GetMinutes() { TimePeriodCollection minutes = new TimePeriodCollection(); DateTime startMinute = new DateTime(StartYear, StartMonth, StartDay, StartHour, StartMinute, 0); for (int i = 0; i < MinuteCount; i++) { minutes.Add(new Minute(startMinute.AddMinutes(i), Calendar)); } return(minutes); } // GetMinutes
} // Hours // ---------------------------------------------------------------------- public ITimePeriodCollection GetHours() { TimePeriodCollection hours = new TimePeriodCollection(); DateTime startHour = new DateTime(StartYear, StartMonth, StartDay, StartHour, 0, 0); foreach (var i in Enumerable.Range(0, HourCount)) { hours.Add(new Hour(startHour.AddHours(i), Calendar)); } return(hours); } // GetHours
} // Days // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDay = new DateTime(StartYear, StartMonth, StartDay); for (int i = 0; i < DayCount; i++) { days.Add(new Day(startDay.AddDays(i), Calendar)); } return(days); } // GetDays
// ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); for ( int i = 0; i < QuarterCount; i++ ) { int year; YearQuarter quarter; TimeTool.AddQuarter( StartYear, StartQuarter, i, out year, out quarter ); quarters.Add( new Quarter( year, quarter, Calendar ) ); } return quarters; }
} // GetStartOfWeek // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType); int dayCount = weekCount * TimeSpec.DaysPerWeek; foreach (var i in Enumerable.Range(0, dayCount)) { days.Add(new Day(startDate.AddDays(i), Calendar)); } return(days); } // GetDays
} // Year // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime moment = Start.Date; while (moment <= End.Date) { days.Add(new Day(moment.Date, Calendar)); moment = moment.AddDays(1); } return(days); } // GetDays
// ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for ( int i = 0; i < MonthCount; i++ ) { int year; YearMonth month; TimeTool.AddMonth( StartYear, StartMonth, i, out year, out month ); months.Add( new Month( year, month, Calendar ) ); } return months; }
} // GetDays // ---------------------------------------------------------------------- public ITimePeriodCollection GetWeeks() { TimePeriodCollection weeks = new TimePeriodCollection(); DateTime moment = Start.Date; while (moment < End.Date) { weeks.Add(new BroadcastWeek(moment.Date, Calendar)); moment = moment.AddDays(TimeSpec.DaysPerWeek); } return(weeks); } // GetWeeks
} // EndWeekOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType); int dayCount = weekCount * TimeSpec.DaysPerWeek; for (int i = 0; i < dayCount; i++) { days.Add(new Day(startDate.AddDays(i), Calendar)); } return(days); } // GetDays
// ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for ( int i = 0; i < HalfyearCount; i++ ) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear( StartYear, StartHalfyear, i, out year, out halfyear ); halfyears.Add( new Halfyear( year, halfyear, Calendar ) ); } return halfyears; }
} // GetQuarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); DateTime startDate = new DateTime(startYear, (int)YearBaseMonth, 1); int monthCount = halfyearCount * TimeSpec.MonthsPerHalfyear; for (int i = 0; i < monthCount; i++) { months.Add(new Month(startDate.AddMonths(i), Calendar)); } return(months); } // GetMonths
} // Quarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); for (int i = 0; i < QuarterCount; i++) { int year; YearQuarter quarter; TimeTool.AddQuarter(BaseYear, StartQuarter, i, out year, out quarter); quarters.Add(new Quarter(year, quarter, Calendar)); } return(quarters); } // GetQuarters
} // EndYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for (int i = 0; i < yearCount; i++) { for (int halfyear = 0; halfyear < TimeSpec.HalfyearsPerYear; halfyear++) { halfyears.Add(new Halfyear(startYear + i, (YearHalfyear)(halfyear + 1), Calendar)); } } return(halfyears); } // GetHalfyears
} // Halfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); for (int i = 0; i < HalfyearCount; i++) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear(StartYear, StartHalfyear, i, out year, out halfyear); halfyears.Add(new Halfyear(year, halfyear, Calendar)); } return(halfyears); } // GetHalfyears
} // Quarters // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, QuarterCount)) { int year; YearQuarter quarter; TimeTool.AddQuarter(BaseYear, StartQuarter, i, out year, out quarter); quarters.Add(new Quarter(year, quarter, Calendar)); } return(quarters); } // GetQuarters
} // Months // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); for (int i = 0; i < MonthCount; i++) { int year; YearMonth month; TimeTool.AddMonth(StartYear, StartMonth, i, out year, out month); months.Add(new Month(year, month, Calendar)); } return(months); } // GetMonths
} // Months // ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, MonthCount)) { int year; YearMonth month; TimeTool.AddMonth(StartYear, StartMonth, i, out year, out month); months.Add(new Month(year, month, Calendar)); } return(months); } // GetMonths
} // GetHalfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); for (int i = 0; i < yearCount; i++) { for (int quarter = 0; quarter < TimeSpec.QuartersPerYear; quarter++) { quarters.Add(new Quarter(startYear + i, (YearQuarter)(quarter + 1), Calendar)); } } return(quarters); } // GetQuarters
} // Halfyears // ---------------------------------------------------------------------- public ITimePeriodCollection GetHalfyears() { TimePeriodCollection halfyears = new TimePeriodCollection(); foreach (var i in Enumerable.Range(0, HalfyearCount)) { int year; YearHalfyear halfyear; TimeTool.AddHalfyear(BaseYear, StartHalfyear, i, out year, out halfyear); halfyears.Add(new Halfyear(year, halfyear, Calendar)); } return(halfyears); } // GetHalfyears
} // EndHalfyearOfYearName // ---------------------------------------------------------------------- public ITimePeriodCollection GetQuarters() { TimePeriodCollection quarters = new TimePeriodCollection(); int quarterCount = HalfyearCount * TimeSpec.QuartersPerHalfyear; int startQuarter = ((int)startHalfyear - 1) * TimeSpec.QuartersPerHalfyear; for (int quarter = 0; quarter < quarterCount; quarter++) { int targetQuarter = startQuarter + quarter; int year = BaseYear + (targetQuarter / 4); quarters.Add(new Quarter(year, (YearQuarter)(targetQuarter + 1), Calendar)); } return(quarters); } // GetQuarters
// ---------------------------------------------------------------------- public IList<MyTimePeriod> GetGaps( IEnumerable<MyTimePeriod> periods ) { TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>(); TimePeriodCollection calcPeriods = new TimePeriodCollection(); foreach ( MyTimePeriod period in periods ) { calcPeriods.Add( new TimeRange( period.Start, period.End ) ); } List<MyTimePeriod> gaps = new List<MyTimePeriod>(); if ( calcPeriods.Count == 0 ) { return gaps; } ITimePeriodCollection calcCaps = gapCalculator.GetGaps( calcPeriods ); foreach ( TimeRange calcCap in calcCaps ) { gaps.Add( new MyTimePeriod { Start = calcCap.Start, End = calcCap.End } ); } return gaps; }
public void RemoveTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.IsFalse( timePeriods.Contains( schoolDay.Lesson1 ) ); timePeriods.Add( schoolDay.Lesson1 ); Assert.IsTrue( timePeriods.Contains( schoolDay.Lesson1 ) ); timePeriods.Remove( schoolDay.Lesson1 ); Assert.IsFalse( timePeriods.Contains( schoolDay.Lesson1 ) ); }
public void GetRelationTest() { DateTime now = ClockProxy.Clock.Now; TimeSpan offset = Duration.Second; TimeRangePeriodRelationTestData testData = new TimeRangePeriodRelationTestData( now, now.AddHours( 1 ), offset ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( testData.Reference ); Assert.AreEqual( timePeriods.GetRelation( testData.Before ), PeriodRelation.Before ); Assert.AreEqual( timePeriods.GetRelation( testData.StartTouching ), PeriodRelation.StartTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.StartInside ), PeriodRelation.StartInside ); Assert.AreEqual( timePeriods.GetRelation( testData.InsideStartTouching ), PeriodRelation.InsideStartTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.EnclosingStartTouching ), PeriodRelation.EnclosingStartTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.Enclosing ), PeriodRelation.Enclosing ); Assert.AreEqual( timePeriods.GetRelation( testData.EnclosingEndTouching ), PeriodRelation.EnclosingEndTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.ExactMatch ), PeriodRelation.ExactMatch ); Assert.AreEqual( timePeriods.GetRelation( testData.Inside ), PeriodRelation.Inside ); Assert.AreEqual( timePeriods.GetRelation( testData.InsideEndTouching ), PeriodRelation.InsideEndTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.EndInside ), PeriodRelation.EndInside ); Assert.AreEqual( timePeriods.GetRelation( testData.EndTouching ), PeriodRelation.EndTouching ); Assert.AreEqual( timePeriods.GetRelation( testData.After ), PeriodRelation.After ); }
// ---------------------------------------------------------------------- public ITimePeriodCollection GetMonths() { TimePeriodCollection months = new TimePeriodCollection(); DateTime startDate = new DateTime( startYear, (int)YearBaseMonth, 1 ); int monthCount = quarterCount * TimeSpec.MonthsPerQuarter; for ( int i = 0; i < monthCount; i++ ) { months.Add( new Month( startDate.AddMonths( i ), Calendar ) ); } return months; }
public void HasEndTest() { DateTime now = ClockProxy.Clock.Now; TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.IsFalse( timePeriods.HasEnd ); timePeriods.Add( new TimeBlock( Duration.Hour, TimeSpec.MaxPeriodDate ) ); Assert.IsFalse( timePeriods.HasEnd ); timePeriods.Clear(); timePeriods.Add( new TimeBlock( now, Duration.Hour ) ); Assert.IsTrue( timePeriods.HasEnd ); }
// ---------------------------------------------------------------------- protected DateTime? CalculateEnd( DateTime start, TimeSpan offset, SeekDirection seekDirection, SeekBoundaryMode seekBoundaryMode, out TimeSpan? remaining) { if ( offset < TimeSpan.Zero ) { throw new InvalidOperationException( "time span must be positive" ); } remaining = offset; // search periods TimePeriodCollection searchPeriods = new TimePeriodCollection( includePeriods ); // no search periods specified: search anytime if ( searchPeriods.Count == 0 ) { searchPeriods.Add( TimeRange.Anytime ); } // available periods ITimePeriodCollection availablePeriods = new TimePeriodCollection(); // no exclude periods specified: use all search periods if ( excludePeriods.Count == 0 ) { availablePeriods.AddAll( searchPeriods ); } else // remove exclude periods { TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>(); foreach ( ITimePeriod searchPeriod in searchPeriods ) { // no overlaps: use the entire search range if ( !excludePeriods.HasOverlapPeriods( searchPeriod ) ) { availablePeriods.Add( searchPeriod ); } else // add gaps of search period using the exclude periods { availablePeriods.AddAll( gapCalculator.GetGaps( excludePeriods, searchPeriod ) ); } } } // no periods available if ( availablePeriods.Count == 0 ) { return null; } // combine the available periods, ensure no overlapping // used for FindNextPeriod/FindPreviousPeriod if ( availablePeriods.Count > 1 ) { TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>(); availablePeriods = periodCombiner.CombinePeriods( availablePeriods ); } // find the starting search period ITimePeriod startPeriod = null; DateTime seekMoment = start; switch ( seekDirection ) { case SeekDirection.Forward: startPeriod = FindNextPeriod( start, availablePeriods, out seekMoment ); break; case SeekDirection.Backward: startPeriod = FindPreviousPeriod( start, availablePeriods, out seekMoment ); break; } // no starting period available if ( startPeriod == null ) { return null; } // no offset: use the search staring position // maybe moved to the next available search period if ( offset == TimeSpan.Zero ) { return seekMoment; } // setup destination search switch ( seekDirection ) { case SeekDirection.Forward: for ( int i = availablePeriods.IndexOf( startPeriod ); i < availablePeriods.Count; i++ ) { ITimePeriod gap = availablePeriods[ i ]; TimeSpan gapRemining = gap.End - seekMoment; bool isTargetPeriod = false; switch ( seekBoundaryMode ) { case SeekBoundaryMode.Fill: isTargetPeriod = gapRemining >= remaining; break; case SeekBoundaryMode.Next: isTargetPeriod = gapRemining > remaining; break; } if ( isTargetPeriod ) { DateTime end = seekMoment + remaining.Value; remaining = null; return end; } remaining = remaining - gapRemining; if ( i == availablePeriods.Count - 1 ) { return null; } seekMoment = availablePeriods[ i + 1 ].Start; // next period } break; case SeekDirection.Backward: for ( int i = availablePeriods.IndexOf( startPeriod ); i >= 0; i-- ) { ITimePeriod gap = availablePeriods[ i ]; TimeSpan gapRemining = seekMoment - gap.Start; bool isTargetPeriod = false; switch ( seekBoundaryMode ) { case SeekBoundaryMode.Fill: isTargetPeriod = gapRemining >= remaining; break; case SeekBoundaryMode.Next: isTargetPeriod = gapRemining > remaining; break; } if ( isTargetPeriod ) { DateTime end = seekMoment - remaining.Value; remaining = null; return end; } remaining = remaining - gapRemining; if ( i == 0 ) { return null; } seekMoment = availablePeriods[ i - 1 ].End; // previous period } break; } return null; }
public void InsertTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.AreEqual( timePeriods.Count, 0 ); timePeriods.Add( schoolDay.Lesson1 ); Assert.AreEqual( timePeriods.Count, 1 ); timePeriods.Add( schoolDay.Lesson3 ); Assert.AreEqual( timePeriods.Count, 2 ); timePeriods.Add( schoolDay.Lesson4 ); Assert.AreEqual( timePeriods.Count, 3 ); // between Assert.AreEqual( timePeriods[ 1 ], schoolDay.Lesson3 ); timePeriods.Insert( 1, schoolDay.Lesson2 ); Assert.AreEqual( timePeriods[ 1 ], schoolDay.Lesson2 ); // first Assert.AreEqual( timePeriods[ 0 ], schoolDay.Lesson1 ); timePeriods.Insert( 0, schoolDay.Break1 ); Assert.AreEqual( timePeriods[ 0 ], schoolDay.Break1 ); // last Assert.AreEqual( timePeriods[ timePeriods.Count - 1 ], schoolDay.Lesson4 ); timePeriods.Insert( timePeriods.Count, schoolDay.Break3 ); Assert.AreEqual( timePeriods[ timePeriods.Count - 1 ], schoolDay.Break3 ); }
public void HasStartTest() { TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.IsFalse( timePeriods.HasStart ); timePeriods.Add( new TimeBlock( TimeSpec.MinPeriodDate, Duration.Hour ) ); Assert.IsFalse( timePeriods.HasStart ); timePeriods.Clear(); timePeriods.Add( new TimeBlock( ClockProxy.Clock.Now, Duration.Hour ) ); Assert.IsTrue( timePeriods.HasStart ); }
public void IsAnytimeTest() { DateTime now = ClockProxy.Clock.Now; TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.IsTrue( timePeriods.IsAnytime ); timePeriods.Add( TimeRange.Anytime ); Assert.IsTrue( timePeriods.IsAnytime ); timePeriods.Clear(); Assert.IsTrue( timePeriods.IsAnytime ); timePeriods.Add( new TimeRange( TimeSpec.MinPeriodDate, now ) ); Assert.IsFalse( timePeriods.IsAnytime ); timePeriods.Add( new TimeRange( now, TimeSpec.MaxPeriodDate ) ); Assert.IsTrue( timePeriods.IsAnytime ); timePeriods.Clear(); Assert.IsTrue( timePeriods.IsAnytime ); }
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 ITimePeriodCollection GetDays() { TimePeriodCollection days = new TimePeriodCollection(); DateTime startDate = new DateTime( startYear, (int)startMonth, 1 ); for ( int month = 0; month < monthCount; month++ ) { DateTime monthStart = startDate.AddMonths( month ); int daysOfMonth = TimeTool.GetDaysInMonth( monthStart.Year, monthStart.Month ); for ( int day = 0; day < daysOfMonth; day++ ) { days.Add( new Day( monthStart.AddDays( day ), Calendar ) ); } } return days; }
public void IsMomentTest() { DateTime now = ClockProxy.Clock.Now; TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.IsFalse( timePeriods.IsMoment ); timePeriods.Add( TimeRange.Anytime ); Assert.IsFalse( timePeriods.IsMoment ); timePeriods.Clear(); Assert.IsFalse( timePeriods.IsMoment ); timePeriods.Add( new TimeRange( now ) ); Assert.IsTrue( timePeriods.IsMoment ); timePeriods.Add( new TimeRange( now ) ); Assert.IsTrue( timePeriods.IsMoment ); timePeriods.Clear(); Assert.IsTrue( timePeriods.IsAnytime ); }
public void StartTest() { DateTime now = ClockProxy.Clock.Now; TimePeriodCollection timePeriods = new TimePeriodCollection(); Assert.AreEqual( timePeriods.Start, TimeSpec.MinPeriodDate ); timePeriods.Add( new TimeBlock( now, Duration.Hour ) ); Assert.AreEqual( timePeriods.Start, now ); timePeriods.Clear(); Assert.AreEqual( timePeriods.Start, TimeSpec.MinPeriodDate ); }
public void SortByStartTest() { DateTime now = ClockProxy.Clock.Now; SchoolDay schoolDay = new SchoolDay( now ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( schoolDay.Lesson4 ); timePeriods.Add( schoolDay.Break3 ); timePeriods.Add( schoolDay.Lesson3 ); timePeriods.Add( schoolDay.Break2 ); timePeriods.Add( schoolDay.Lesson2 ); timePeriods.Add( schoolDay.Break1 ); timePeriods.Add( schoolDay.Lesson1 ); timePeriods.SortByStart(); Assert.AreEqual( timePeriods[ 0 ], schoolDay.Lesson1 ); Assert.AreEqual( timePeriods[ 1 ], schoolDay.Break1 ); Assert.AreEqual( timePeriods[ 2 ], schoolDay.Lesson2 ); Assert.AreEqual( timePeriods[ 3 ], schoolDay.Break2 ); Assert.AreEqual( timePeriods[ 4 ], schoolDay.Lesson3 ); Assert.AreEqual( timePeriods[ 5 ], schoolDay.Break3 ); Assert.AreEqual( timePeriods[ 6 ], schoolDay.Lesson4 ); }
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 OverlapsWithTest() { DateTime now = ClockProxy.Clock.Now; TimeSpan offset = Duration.Second; TimeRangePeriodRelationTestData testData = new TimeRangePeriodRelationTestData( now, now.AddHours( 1 ), offset ); TimePeriodCollection timePeriods = new TimePeriodCollection(); timePeriods.Add( testData.Reference ); Assert.IsFalse( timePeriods.OverlapsWith( testData.Before ) ); Assert.IsFalse( timePeriods.OverlapsWith( testData.StartTouching ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.StartInside ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.InsideStartTouching ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.EnclosingStartTouching ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.Enclosing ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.EnclosingEndTouching ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.ExactMatch ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.Inside ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.InsideEndTouching ) ); Assert.IsTrue( timePeriods.OverlapsWith( testData.EndInside ) ); Assert.IsFalse( timePeriods.OverlapsWith( testData.EndTouching ) ); Assert.IsFalse( timePeriods.OverlapsWith( testData.After ) ); }
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 ); }