Example #1
0
        public void TestNonWorkingTime()
        {
            schedule = new WorkSchedule("Non Working Time", "Test non working time");
            LocalDate date = new LocalDate(2017, 1, 1);
            LocalTime time = new LocalTime(7, 0, 0);

            NonWorkingPeriod period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day",
                                                                       date.At(LocalTime.Midnight), Duration.FromHours(24));
            NonWorkingPeriod period2 = schedule.CreateNonWorkingPeriod("Day2", "First test day",
                                                                       date.PlusDays(7).At(time), Duration.FromHours(24));

            LocalDateTime from = date.At(time);
            LocalDateTime to   = date.At(time.PlusHours(1));

            // case #1
            Duration duration = schedule.CalculateNonWorkingTime(from, to);

            Assert.IsTrue(duration.Equals(Duration.FromHours(1)));

            // case #2
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.PlusDays(1).At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(24)));

            // case #3
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.Minus(Period.FromDays(1)).At(time.PlusHours(1));
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #4
            from     = date.PlusDays(1).At(time);
            to       = date.PlusDays(1).At(time.PlusHours(1));
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #5
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(7)));

            // case #6
            from     = date.At(time);
            to       = date.PlusDays(1).At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(17)));

            // case #7
            from     = date.At(LocalTime.Noon);
            to       = date.PlusDays(7).At(LocalTime.Noon);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(17)));

            // case #8
            from     = date.Minus(Period.FromDays(1)).At(LocalTime.Noon);
            to       = date.PlusDays(8).At(LocalTime.Noon);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(48)));

            // case #9
            schedule.DeleteNonWorkingPeriod(period1);
            schedule.DeleteNonWorkingPeriod(period2);
            from = date.At(time);
            to   = date.At(time.PlusHours(1));

            // case #10
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            Duration  shiftDuration = Duration.FromHours(8);
            LocalTime shiftStart    = new LocalTime(7, 0, 0);

            Shift shift = schedule.CreateShift("Work Shift1", "Working time shift", shiftStart, shiftDuration);

            Rotation rotation = new Rotation("Case 10", "Case10");

            rotation.AddSegment(shift, 1, 1);

            LocalDate startRotation = new LocalDate(2017, 1, 1);
            Team      team          = schedule.CreateTeam("Team", "Team", rotation, startRotation);

            team.RotationStart = startRotation;

            period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day", date.At(LocalTime.Midnight),
                                                      Duration.FromHours(24));

            LocalDate mark = date.PlusDays(rotation.GetDayCount());

            from = mark.At(time.Minus(Period.FromHours(2)));
            to   = mark.At(time.Minus(Period.FromHours(1)));

            // case #11
            duration = schedule.CalculateWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #12
            from = date.At(shiftStart);
            to   = date.At(time.PlusHours(8));

            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(8)));
        }
Example #2
0
        public void TestGenericShift()
        {
            // regular work week with holidays and breaks
            schedule = new WorkSchedule("Regular 40 hour work week", "9 to 5");

            // holidays
            NonWorkingPeriod memorialDay = schedule.CreateNonWorkingPeriod("MEMORIAL DAY", "Memorial day", new LocalDateTime(2016, 5, 30, 0, 0, 0),
                                                                           Duration.FromHours(24));

            schedule.CreateNonWorkingPeriod("INDEPENDENCE DAY", "Independence day", new LocalDateTime(2016, 7, 4, 0, 0, 0),
                                            Duration.FromHours(24));
            schedule.CreateNonWorkingPeriod("LABOR DAY", "Labor day", new LocalDateTime(2016, 9, 5, 0, 0, 0),
                                            Duration.FromHours(24));
            schedule.CreateNonWorkingPeriod("THANKSGIVING", "Thanksgiving day and day after",
                                            new LocalDateTime(2016, 11, 24, 0, 0, 0), Duration.FromHours(48));
            schedule.CreateNonWorkingPeriod("CHRISTMAS SHUTDOWN", "Christmas week scheduled maintenance",
                                            new LocalDateTime(2016, 12, 25, 0, 30, 0), Duration.FromHours(168));

            // each shift duration
            Duration  shiftDuration = Duration.FromHours(8);
            LocalTime shift1Start   = new LocalTime(7, 0, 0);
            LocalTime shift2Start   = new LocalTime(15, 0, 0);

            // shift 1
            Shift shift1 = schedule.CreateShift("Shift1", "Shift #1", shift1Start, shiftDuration);

            // breaks
            shift1.CreateBreak("10AM", "10 am break", new LocalTime(10, 0, 0), Duration.FromMinutes(15));
            shift1.CreateBreak("LUNCH", "lunch", new LocalTime(12, 0, 0), Duration.FromHours(1));
            shift1.CreateBreak("2PM", "2 pm break", new LocalTime(14, 0, 0), Duration.FromMinutes(15));

            // shift 2
            Shift shift2 = schedule.CreateShift("Shift2", "Shift #2", shift2Start, shiftDuration);

            // shift 1, 5 days ON, 2 OFF
            Rotation rotation1 = new Rotation("Shift1", "Shift1");

            rotation1.AddSegment(shift1, 5, 2);

            // shift 2, 5 days ON, 2 OFF
            Rotation rotation2 = new Rotation("Shift2", "Shift2");

            rotation2.AddSegment(shift2, 5, 2);

            LocalDate startRotation = new LocalDate(2016, 1, 1);
            Team      team1         = schedule.CreateTeam("Team1", "Team #1", rotation1, startRotation);
            Team      team2         = schedule.CreateTeam("Team2", "Team #2", rotation2, startRotation);

            // same day
            LocalDateTime from = startRotation.PlusDays(7).At(shift1Start);
            LocalDateTime to;

            Duration totalWorking = Duration.Zero;

            // 21 days, team1
            Duration d = Duration.Zero;

            for (int i = 0; i < 21; i++)
            {
                to           = from.PlusDays(i);
                totalWorking = team1.CalculateWorkingTime(from, to);
                int dir = team1.GetDayInRotation(to.Date);

                Assert.IsTrue(totalWorking.Equals(d));

                TimePeriod period = rotation1.GetPeriods()[dir - 1];
                if (period is Shift)
                {
                    d = d.Plus(shiftDuration);
                }
            }
            Duration totalSchedule = totalWorking;

            // 21 days, team2
            from = startRotation.PlusDays(7).At(shift2Start);
            d    = Duration.Zero;

            for (int i = 0; i < 21; i++)
            {
                to           = from.PlusDays(i);
                totalWorking = team2.CalculateWorkingTime(from, to);
                int dir = team2.GetDayInRotation(to.Date);

                Assert.IsTrue(totalWorking.Equals(d));

                if (rotation1.GetPeriods()[dir - 1] is Shift)
                {
                    d = d.Plus(shiftDuration);
                }
            }
            totalSchedule = totalSchedule.Plus(totalWorking);

            Duration scheduleDuration   = schedule.CalculateWorkingTime(from, from.PlusDays(21));
            Duration nonWorkingDuration = schedule.CalculateNonWorkingTime(from, from.PlusDays(21));

            Assert.IsTrue(scheduleDuration.Plus(nonWorkingDuration).Equals(totalSchedule));

            // breaks
            Duration allBreaks = Duration.FromMinutes(90);

            Assert.IsTrue(shift1.CalculateBreakTime().Equals(allBreaks));

            // misc
            WorkSchedule schedule2 = new WorkSchedule();

            Shift shift3 = new Shift();

            shift3.Name = "Shift3";
            Assert.IsTrue(shift3.WorkSchedule == null);
            Assert.IsTrue(shift3.CompareTo(shift3) == 0);

            Team team3 = new Team();

            Assert.IsTrue(team3.WorkSchedule == null);

            RotationSegment segment = new RotationSegment();

            segment.Sequence      = 1;
            segment.StartingShift = shift2;
            segment.DaysOn        = 5;
            segment.DaysOff       = 2;
            Assert.IsTrue(segment.Rotation == null);

            Rotation rotation3 = new Rotation();

            rotation3.Name = "Rotation3";
            Assert.IsTrue(rotation3.CompareTo(rotation3) == 0);
            Assert.IsTrue(rotation3.RotationSegments.Count == 0);

            NonWorkingPeriod nwp = new NonWorkingPeriod();

            Assert.IsTrue(nwp.WorkSchedule == null);


            Assert.IsTrue(team1.WorkSchedule.Equals(schedule));


            Assert.IsTrue(!team1.IsDayOff(startRotation));


            Assert.IsTrue(team1.CompareTo(team1) == 0);
            team3.Rotation = rotation1;


            Assert.IsTrue(!memorialDay.IsInPeriod(new LocalDate(2016, 1, 1)));

            runBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), new LocalDate(2016, 1, 1));
        }