Inheritance: ITimePeriodCollection
コード例 #1
0
ファイル: TimeLine.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #2
0
        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 ) ) ) );
        }
コード例 #3
0
ファイル: Weeks.cs プロジェクト: jwg4/date-difference
 // ----------------------------------------------------------------------
 public ITimePeriodCollection GetWeeks()
 {
     TimePeriodCollection weeks = new TimePeriodCollection();
     for ( int i = 0; i < WeekCount; i++ )
     {
         weeks.Add( new Week( Year, StartWeek + i, Calendar ) );
     }
     return weeks;
 }
コード例 #4
0
ファイル: Years.cs プロジェクト: jwg4/date-difference
 // ----------------------------------------------------------------------
 public ITimePeriodCollection GetYears()
 {
     TimePeriodCollection years = new TimePeriodCollection();
     for ( int i = 0; i < YearCount; i++ )
     {
         years.Add( new Year( StartYear + i, Calendar ) );
     }
     return years;
 }
コード例 #5
0
ファイル: TimeLine.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #6
0
        }         // 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
コード例 #7
0
        }         // 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
コード例 #8
0
ファイル: Hours.cs プロジェクト: jwg4/date-difference
 // ----------------------------------------------------------------------
 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;
 }
コード例 #9
0
        }                                    // 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
コード例 #10
0
ファイル: Weeks.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #11
0
        }                                               // 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
コード例 #12
0
ファイル: Minutes.cs プロジェクト: HoLoveSalt/showhotel
 // ----------------------------------------------------------------------
 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;
 }
コード例 #13
0
ファイル: CustomWeek.cs プロジェクト: jwg4/date-difference
 // ----------------------------------------------------------------------
 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;
 }
コード例 #14
0
        } // 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
コード例 #15
0
        } // 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
コード例 #16
0
ファイル: Days.cs プロジェクト: HoLoveSalt/showhotel
 // ----------------------------------------------------------------------
 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;
 }
コード例 #17
0
ファイル: Days.cs プロジェクト: unquietwiki/TimePeriodLibrary
        }                                                                  // 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
コード例 #18
0
        }                                                                         // 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
コード例 #19
0
ファイル: Hours.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #20
0
        }         // 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
コード例 #21
0
        }         // 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
コード例 #22
0
ファイル: Minutes.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #23
0
        }                                                                                             // 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
コード例 #24
0
        }         // 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
コード例 #25
0
        } // 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
コード例 #26
0
ファイル: Quarters.cs プロジェクト: HoLoveSalt/showhotel
 // ----------------------------------------------------------------------
 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;
 }
コード例 #27
0
        }         // 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
コード例 #28
0
ファイル: Months.cs プロジェクト: HoLoveSalt/showhotel
 // ----------------------------------------------------------------------
 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;
 }
コード例 #29
0
        } // 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
コード例 #30
0
ファイル: Halfyears.cs プロジェクト: HoLoveSalt/showhotel
 // ----------------------------------------------------------------------
 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;
 }
コード例 #31
0
        }         // 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
コード例 #32
0
ファイル: Halfyears.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #33
0
ファイル: Months.cs プロジェクト: BlueBasher/TfsTimeSheet
        }         // 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
コード例 #34
0
        }                                                                  // 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
コード例 #35
0
        }         // 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
コード例 #36
0
        }                                                          // 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
コード例 #37
0
        }         // 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
コード例 #38
0
        }                                                                // 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
コード例 #39
0
        }         // 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
コード例 #40
0
        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 ) );
        }
コード例 #41
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 );
        }
コード例 #42
0
ファイル: TimeLineTest.cs プロジェクト: jwg4/date-difference
        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 ] );
        }
コード例 #43
0
        }         // 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
コード例 #44
0
        public void AddAllTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            SchoolDay schoolDay = new SchoolDay( now );
            TimePeriodCollection timePeriods = new TimePeriodCollection();

            Assert.AreEqual( timePeriods.Count, 0 );

            timePeriods.AddAll( schoolDay );
            Assert.AreEqual( timePeriods.Count, schoolDay.Count );

            timePeriods.Clear();
            Assert.AreEqual( timePeriods.Count, 0 );
        }
コード例 #45
0
        // ----------------------------------------------------------------------
        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;
        }
コード例 #46
0
 // ----------------------------------------------------------------------
 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;
 }
コード例 #47
0
        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 );
        }
コード例 #48
0
ファイル: DateAdd.cs プロジェクト: jwg4/date-difference
        // ----------------------------------------------------------------------
        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;
        }
コード例 #49
0
        public void IsSamePeriodTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            SchoolDay schoolDay = new SchoolDay( now );
            TimePeriodCollection timePeriods = new TimePeriodCollection( schoolDay );

            Assert.IsTrue( timePeriods.IsSamePeriod( timePeriods ) );
            Assert.IsTrue( timePeriods.IsSamePeriod( schoolDay ) );

            Assert.IsTrue( schoolDay.IsSamePeriod( schoolDay ) );
            Assert.IsTrue( schoolDay.IsSamePeriod( timePeriods ) );

            Assert.IsFalse( timePeriods.IsSamePeriod( TimeBlock.Anytime ) );
            Assert.IsFalse( schoolDay.IsSamePeriod( TimeBlock.Anytime ) );

            timePeriods.RemoveAt( 0 );
            Assert.IsFalse( timePeriods.IsSamePeriod( schoolDay ) );
        }
コード例 #50
0
        public void MoveTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            SchoolDay schoolDay = new SchoolDay( now );
            TimePeriodCollection timePeriods = new TimePeriodCollection( schoolDay );

            DateTime startDate = schoolDay.Start;
            DateTime endDate = schoolDay.End;
            TimeSpan startDuration = timePeriods.Duration;

            TimeSpan duration = Duration.Hour;
            timePeriods.Move( duration );

            Assert.AreEqual( timePeriods.Start, startDate.Add( duration ) );
            Assert.AreEqual( timePeriods.End, endDate.Add( duration ) );
            Assert.AreEqual( timePeriods.Duration, startDuration );
        }
コード例 #51
0
        public void ItemIndexTest()
        {
            TimePeriodCollection timePeriods = new TimePeriodCollection();
            SchoolDay schoolDay = new SchoolDay();
            timePeriods.AddAll( schoolDay );

            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 );
        }
コード例 #52
0
        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 ) );
        }
コード例 #53
0
        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 );
        }
コード例 #54
0
        public void RemoveAtTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            SchoolDay schoolDay = new SchoolDay( now );
            TimePeriodCollection timePeriods = new TimePeriodCollection( schoolDay );

            // inside
            Assert.AreEqual( timePeriods[ 2 ], schoolDay.Lesson2 );
            timePeriods.RemoveAt( 2 );
            Assert.AreEqual( timePeriods[ 2 ], schoolDay.Break2 );

            // first
            Assert.AreEqual( timePeriods[ 0 ], schoolDay.Lesson1 );
            timePeriods.RemoveAt( 0 );
            Assert.AreEqual( timePeriods[ 0 ], schoolDay.Break1 );

            // last
            Assert.AreEqual( timePeriods[ timePeriods.Count - 1 ], schoolDay.Lesson4 );
            timePeriods.RemoveAt( timePeriods.Count - 1 );
            Assert.AreEqual( timePeriods[ timePeriods.Count - 1 ], schoolDay.Break3 );
        }
コード例 #55
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 );
        }
コード例 #56
0
 // ----------------------------------------------------------------------
 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;
 }
コード例 #57
0
        public void ClearTest()
        {
            TimePeriodCollection timePeriods = new TimePeriodCollection();
            Assert.AreEqual( timePeriods.Count, 0 );
            timePeriods.Clear();
            Assert.AreEqual( timePeriods.Count, 0 );

            timePeriods.AddAll( new SchoolDay() );
            Assert.AreEqual( timePeriods.Count, 7 );
            timePeriods.Clear();
            Assert.AreEqual( timePeriods.Count, 0 );
        }
コード例 #58
0
        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 );
        }
コード例 #59
0
        public void StartMoveTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            SchoolDay schoolDay = new SchoolDay( now );
            TimePeriodCollection timePeriods = new TimePeriodCollection( schoolDay );

            timePeriods.Start = now.AddHours( 0 );
            Assert.AreEqual( timePeriods.Start, now );
            timePeriods.Start = now.AddHours( 1 );
            Assert.AreEqual( timePeriods.Start, now.AddHours( 1 ) );
            timePeriods.Start = now.AddHours( -1 );
            Assert.AreEqual( timePeriods.Start, now.AddHours( -1 ) );
        }
コード例 #60
0
        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 ) );
        }