Skip to content

Commit

Permalink
Added a method to get all shift instances with working time in a give…
Browse files Browse the repository at this point in the history
…n day
  • Loading branch information
point85 committed Mar 27, 2019
1 parent 8fc48b6 commit 4c32227
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 27 deletions.
17 changes: 17 additions & 0 deletions ShiftSharp/ShiftInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public int CompareTo(ShiftInstance other)
return StartDateTime.CompareTo(other.StartDateTime);
}

/// <summary>
/// Determine if this time falls within the shift instance period
/// </summary>
/// <param name="ldt">Date and time to check</param>
/// <returns>True if the specified time is in this shift instance</returns>
public Boolean IsInShiftInstance(LocalDateTime ldt)
{
if (ldt.CompareTo(StartDateTime) >= 0 && ldt.CompareTo(GetEndTime()) <= 0)
{
return true;
}
else
{
return false;
}
}

/// <summary>
/// Build a string representation of a shift instance
/// </summary>
Expand Down
40 changes: 34 additions & 6 deletions ShiftSharp/WorkSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class WorkSchedule : Named
internal static PropertyManager MessagesManager = new PropertyManager(MESSAGE_RESOURCE_NAME);

// cached time zone for working time calculations
private DateTimeZone ZONE_ID = DateTimeZone.Utc;
private readonly DateTimeZone ZONE_ID = DateTimeZone.Utc;

/// <summary>
/// list of teams
Expand Down Expand Up @@ -156,6 +156,32 @@ public List<ShiftInstance> GetShiftInstancesForDay(LocalDate day)
return workingShifts;
}

/// <summary>
/// Get the list of shift instances for the specified date that start in that date
/// or cross over from midnight the previous day
/// </summary>
/// <param name="day">Date</param>
/// <returns>List of shift instances</returns>
public List<ShiftInstance> GetAllShiftInstancesForDay(LocalDate day)
{
// starting in this day
List<ShiftInstance> workingShifts = GetShiftInstancesForDay(day);

// now check previous day
LocalDate yesterday = day.PlusDays(-1);

foreach (ShiftInstance instance in GetShiftInstancesForDay(yesterday)) {
if (instance.GetEndTime().Date.Equals(day)) {
// shift ends in this day
workingShifts.Add(instance);
}
}

workingShifts.Sort();

return workingShifts;
}

/// <summary>
/// Get the list of shift instances for the specified date and time of day
/// </summary>
Expand All @@ -165,17 +191,19 @@ public List<ShiftInstance> GetShiftInstancesForTime(LocalDateTime dateTime)
{
List<ShiftInstance> workingShifts = new List<ShiftInstance>();

// day
List<ShiftInstance> candidateShifts = GetShiftInstancesForDay(dateTime.Date);
// shifts from this date and yesterday
List<ShiftInstance> candidateShifts = GetAllShiftInstancesForDay(dateTime.Date);

// check time now
foreach (ShiftInstance instance in candidateShifts)
{
if (instance.Shift.IsInShift(dateTime.TimeOfDay))
if (instance.IsInShiftInstance(dateTime))
{
workingShifts.Add(instance);
}
}

workingShifts.Sort();

return workingShifts;
}

Expand Down Expand Up @@ -431,7 +459,7 @@ public Duration CalculateNonWorkingTime(LocalDateTime from, LocalDateTime to)
/// <param name="end">Ending date</param>
public void PrintShiftInstances(LocalDate start, LocalDate end)
{
if (start.CompareTo(end) < 0)
if (start.CompareTo(end) > 0)
{
string msg = String.Format(WorkSchedule.GetMessage("end.earlier.than.start"), start, end);
throw new Exception(msg);
Expand Down
6 changes: 3 additions & 3 deletions TestShiftSharp/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class BaseTest
protected LocalDate referenceDate = new LocalDate(2016, 10, 31);

// partial test flags
protected static bool testToString = false;
protected static bool testToString = true;

protected static bool testDeletions = true;

Expand Down Expand Up @@ -257,7 +257,7 @@ private void TestShiftInstances(WorkSchedule ws, LocalDate instanceReference)
}
}

protected void runBaseTest(WorkSchedule ws, Duration hoursPerRotation, Duration rotationDays,
protected void RunBaseTest(WorkSchedule ws, Duration hoursPerRotation, Duration rotationDays,
LocalDate instanceReference)
{

Expand All @@ -279,7 +279,7 @@ protected void runBaseTest(WorkSchedule ws, Duration hoursPerRotation, Duration
TestTeams(ws, hoursPerRotation, rotationDays);

// shift instances
TestShiftInstances(ws, instanceReference);
TestShiftInstances(ws, instanceReference.PlusDays(rotationDays.Days));

if (testDeletions)
{
Expand Down
22 changes: 11 additions & 11 deletions TestShiftSharp/TestSnapSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void TestLowNight()
schedule.CreateTeam("Team5", "Fifth team", rotation, referenceDate.PlusDays(-14));
schedule.CreateTeam("Team6", "Sixth team", rotation, referenceDate.PlusDays(-35));

runBaseTest(schedule, Duration.FromHours(224), Duration.FromDays(42), referenceDate);
RunBaseTest(schedule, Duration.FromHours(224), Duration.FromDays(42), referenceDate);
}

[TestMethod]
Expand All @@ -87,7 +87,7 @@ public void Test3TeamFixed24()
schedule.CreateTeam("Team2", "Second team", rotation, referenceDate.PlusDays(-3));
schedule.CreateTeam("Team3", "Third team", rotation, referenceDate.PlusDays(-6));

runBaseTest(schedule, Duration.FromHours(72), Duration.FromDays(9), referenceDate);
RunBaseTest(schedule, Duration.FromHours(72), Duration.FromDays(9), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -116,7 +116,7 @@ public void Test549()
schedule.CreateTeam("Team1", "First team", rotation, referenceDate);
schedule.CreateTeam("Team2", "Second team", rotation, referenceDate.PlusDays(-14));

runBaseTest(schedule, Duration.FromHours(160), Duration.FromDays(28), referenceDate);
RunBaseTest(schedule, Duration.FromHours(160), Duration.FromDays(28), referenceDate);
}

[TestMethod]
Expand All @@ -136,7 +136,7 @@ public void Test9to5()
// 1 team, 1 shift
schedule.CreateTeam("Team", "One team", rotation, referenceDate);

runBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), referenceDate);
RunBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -178,7 +178,7 @@ public void Test8Plus12()
schedule.CreateTeam("Team 3", "Third team", rotation, referenceDate.PlusDays(-14));
schedule.CreateTeam("Team 4", "Fourth team", rotation, referenceDate.PlusDays(-21));

runBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
RunBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -211,7 +211,7 @@ public void TestICUInterns()
schedule.CreateTeam("Team 3", "Third team", rotation, referenceDate.PlusDays(-2));
schedule.CreateTeam("Team 4", "Forth team", rotation, referenceDate.PlusDays(-1));

runBaseTest(schedule, Duration.FromMinutes(2610), Duration.FromDays(4), referenceDate);
RunBaseTest(schedule, Duration.FromMinutes(2610), Duration.FromDays(4), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -243,7 +243,7 @@ public void TestDupont()
schedule.CreateTeam("Team 3", "Third team", rotation, referenceDate.PlusDays(-14));
schedule.CreateTeam("Team 4", "Forth team", rotation, referenceDate.PlusDays(-21));

runBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
RunBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -274,7 +274,7 @@ public void TestDNO()
Duration duration = schedule.CalculateWorkingTime(from, from.PlusDays(3));
Assert.IsTrue(duration.Equals(Duration.FromHours(72)));

runBaseTest(schedule, Duration.FromHours(24), Duration.FromDays(3), referenceDate);
RunBaseTest(schedule, Duration.FromHours(24), Duration.FromDays(3), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -351,7 +351,7 @@ public void Test21TeamFixed()
schedule.CreateTeam("Team 20", "6th night team", nightRotation, referenceDate.PlusDays(35));
schedule.CreateTeam("Team 21", "7th night team", nightRotation, referenceDate.PlusDays(42));

runBaseTest(schedule, Duration.FromHours(280), Duration.FromDays(49), referenceDate.PlusDays(49));
RunBaseTest(schedule, Duration.FromHours(280), Duration.FromDays(49), referenceDate.PlusDays(49));

}

Expand Down Expand Up @@ -380,7 +380,7 @@ public void TestTwoTeam()
schedule.CreateTeam("Team 1", "First team", team1Rotation, referenceDate);
schedule.CreateTeam("Team 2", "Second team", team2Rotation, referenceDate);

runBaseTest(schedule, Duration.FromHours(12), Duration.FromDays(1), referenceDate);
RunBaseTest(schedule, Duration.FromHours(12), Duration.FromDays(1), referenceDate);
}

[TestMethod]
Expand Down Expand Up @@ -427,7 +427,7 @@ public void TestPanama()
schedule.CreateTeam("Team 3", "Third team", rotation, referenceDate.PlusDays(-7));
schedule.CreateTeam("Team 4", "Fourth team", rotation, referenceDate.PlusDays(-35));

runBaseTest(schedule, Duration.FromHours(336), Duration.FromDays(56), referenceDate);
RunBaseTest(schedule, Duration.FromHours(336), Duration.FromDays(56), referenceDate);
}
}
}
2 changes: 1 addition & 1 deletion TestShiftSharp/TestSnippet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TestSnippet : BaseTest
[TestMethod]
public void TestPartial()
{

}
}
}

12 changes: 6 additions & 6 deletions TestShiftSharp/TestWorkSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void TestNursingICUShifts()
schedule.CreateTeam("C", "Night shift", nightRotation, rotationStart);
schedule.CreateTeam("D", "Night inverse shift", inverseNightRotation, rotationStart);

runBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), rotationStart);
RunBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), rotationStart);
}

[TestMethod]
Expand Down Expand Up @@ -105,7 +105,7 @@ public void TestPostalServiceShifts()
schedule.CreateTeam("Team E", "E team", rotation, rotationStart.Minus(Period.FromDays(28)));
schedule.CreateTeam("Team F", "F team", rotation, rotationStart.Minus(Period.FromDays(35)));

runBaseTest(schedule, Duration.FromHours(63), Duration.FromDays(42), rotationStart);
RunBaseTest(schedule, Duration.FromHours(63), Duration.FromDays(42), rotationStart);
}

[TestMethod]
Expand All @@ -127,7 +127,7 @@ public void TestFirefighterShifts2()
schedule.CreateTeam("C", "Platoon3", rotation, new LocalDate(2014, 1, 31));
schedule.CreateTeam("D", "Platoon4", rotation, new LocalDate(2014, 1, 29));

runBaseTest(schedule, Duration.FromHours(48), Duration.FromDays(8), new LocalDate(2014, 2, 4));
RunBaseTest(schedule, Duration.FromHours(48), Duration.FromDays(8), new LocalDate(2014, 2, 4));
}

[TestMethod]
Expand Down Expand Up @@ -161,7 +161,7 @@ public void TestFirefighterShifts1()
Assert.IsTrue(instances.Count == 1);
Assert.IsTrue(instances[0].Team.Equals(platoon2));

runBaseTest(schedule, Duration.FromHours(144), Duration.FromDays(18), new LocalDate(2017, 2, 1));
RunBaseTest(schedule, Duration.FromHours(144), Duration.FromDays(18), new LocalDate(2017, 2, 1));
}

[TestMethod]
Expand Down Expand Up @@ -190,7 +190,7 @@ public void TestManufacturingShifts()
schedule.CreateTeam("C", "C day shift", dayRotation, new LocalDate(2014, 1, 9));
schedule.CreateTeam("D", "D night shift", nightRotation, new LocalDate(2014, 1, 9));

runBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), new LocalDate(2014, 1, 9));
RunBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), new LocalDate(2014, 1, 9));
}

[TestMethod]
Expand Down Expand Up @@ -330,7 +330,7 @@ public void TestGenericShift()

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

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

}

Expand Down

0 comments on commit 4c32227

Please sign in to comment.