Skip to content

Commit 4c32227

Browse files
committed
Added a method to get all shift instances with working time in a given day
1 parent 8fc48b6 commit 4c32227

6 files changed

Lines changed: 72 additions & 27 deletions

File tree

ShiftSharp/ShiftInstance.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ public int CompareTo(ShiftInstance other)
7474
return StartDateTime.CompareTo(other.StartDateTime);
7575
}
7676

77+
/// <summary>
78+
/// Determine if this time falls within the shift instance period
79+
/// </summary>
80+
/// <param name="ldt">Date and time to check</param>
81+
/// <returns>True if the specified time is in this shift instance</returns>
82+
public Boolean IsInShiftInstance(LocalDateTime ldt)
83+
{
84+
if (ldt.CompareTo(StartDateTime) >= 0 && ldt.CompareTo(GetEndTime()) <= 0)
85+
{
86+
return true;
87+
}
88+
else
89+
{
90+
return false;
91+
}
92+
}
93+
7794
/// <summary>
7895
/// Build a string representation of a shift instance
7996
/// </summary>

ShiftSharp/WorkSchedule.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class WorkSchedule : Named
4242
internal static PropertyManager MessagesManager = new PropertyManager(MESSAGE_RESOURCE_NAME);
4343

4444
// cached time zone for working time calculations
45-
private DateTimeZone ZONE_ID = DateTimeZone.Utc;
45+
private readonly DateTimeZone ZONE_ID = DateTimeZone.Utc;
4646

4747
/// <summary>
4848
/// list of teams
@@ -156,6 +156,32 @@ public List<ShiftInstance> GetShiftInstancesForDay(LocalDate day)
156156
return workingShifts;
157157
}
158158

159+
/// <summary>
160+
/// Get the list of shift instances for the specified date that start in that date
161+
/// or cross over from midnight the previous day
162+
/// </summary>
163+
/// <param name="day">Date</param>
164+
/// <returns>List of shift instances</returns>
165+
public List<ShiftInstance> GetAllShiftInstancesForDay(LocalDate day)
166+
{
167+
// starting in this day
168+
List<ShiftInstance> workingShifts = GetShiftInstancesForDay(day);
169+
170+
// now check previous day
171+
LocalDate yesterday = day.PlusDays(-1);
172+
173+
foreach (ShiftInstance instance in GetShiftInstancesForDay(yesterday)) {
174+
if (instance.GetEndTime().Date.Equals(day)) {
175+
// shift ends in this day
176+
workingShifts.Add(instance);
177+
}
178+
}
179+
180+
workingShifts.Sort();
181+
182+
return workingShifts;
183+
}
184+
159185
/// <summary>
160186
/// Get the list of shift instances for the specified date and time of day
161187
/// </summary>
@@ -165,17 +191,19 @@ public List<ShiftInstance> GetShiftInstancesForTime(LocalDateTime dateTime)
165191
{
166192
List<ShiftInstance> workingShifts = new List<ShiftInstance>();
167193

168-
// day
169-
List<ShiftInstance> candidateShifts = GetShiftInstancesForDay(dateTime.Date);
194+
// shifts from this date and yesterday
195+
List<ShiftInstance> candidateShifts = GetAllShiftInstancesForDay(dateTime.Date);
170196

171-
// check time now
172197
foreach (ShiftInstance instance in candidateShifts)
173198
{
174-
if (instance.Shift.IsInShift(dateTime.TimeOfDay))
199+
if (instance.IsInShiftInstance(dateTime))
175200
{
176201
workingShifts.Add(instance);
177202
}
178203
}
204+
205+
workingShifts.Sort();
206+
179207
return workingShifts;
180208
}
181209

@@ -431,7 +459,7 @@ public Duration CalculateNonWorkingTime(LocalDateTime from, LocalDateTime to)
431459
/// <param name="end">Ending date</param>
432460
public void PrintShiftInstances(LocalDate start, LocalDate end)
433461
{
434-
if (start.CompareTo(end) < 0)
462+
if (start.CompareTo(end) > 0)
435463
{
436464
string msg = String.Format(WorkSchedule.GetMessage("end.earlier.than.start"), start, end);
437465
throw new Exception(msg);

TestShiftSharp/BaseTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class BaseTest
4040
protected LocalDate referenceDate = new LocalDate(2016, 10, 31);
4141

4242
// partial test flags
43-
protected static bool testToString = false;
43+
protected static bool testToString = true;
4444

4545
protected static bool testDeletions = true;
4646

@@ -257,7 +257,7 @@ private void TestShiftInstances(WorkSchedule ws, LocalDate instanceReference)
257257
}
258258
}
259259

260-
protected void runBaseTest(WorkSchedule ws, Duration hoursPerRotation, Duration rotationDays,
260+
protected void RunBaseTest(WorkSchedule ws, Duration hoursPerRotation, Duration rotationDays,
261261
LocalDate instanceReference)
262262
{
263263

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

281281
// shift instances
282-
TestShiftInstances(ws, instanceReference);
282+
TestShiftInstances(ws, instanceReference.PlusDays(rotationDays.Days));
283283

284284
if (testDeletions)
285285
{

TestShiftSharp/TestSnapSchedule.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void TestLowNight()
6363
schedule.CreateTeam("Team5", "Fifth team", rotation, referenceDate.PlusDays(-14));
6464
schedule.CreateTeam("Team6", "Sixth team", rotation, referenceDate.PlusDays(-35));
6565

66-
runBaseTest(schedule, Duration.FromHours(224), Duration.FromDays(42), referenceDate);
66+
RunBaseTest(schedule, Duration.FromHours(224), Duration.FromDays(42), referenceDate);
6767
}
6868

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

90-
runBaseTest(schedule, Duration.FromHours(72), Duration.FromDays(9), referenceDate);
90+
RunBaseTest(schedule, Duration.FromHours(72), Duration.FromDays(9), referenceDate);
9191
}
9292

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

119-
runBaseTest(schedule, Duration.FromHours(160), Duration.FromDays(28), referenceDate);
119+
RunBaseTest(schedule, Duration.FromHours(160), Duration.FromDays(28), referenceDate);
120120
}
121121

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

139-
runBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), referenceDate);
139+
RunBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), referenceDate);
140140
}
141141

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

181-
runBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
181+
RunBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
182182
}
183183

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

214-
runBaseTest(schedule, Duration.FromMinutes(2610), Duration.FromDays(4), referenceDate);
214+
RunBaseTest(schedule, Duration.FromMinutes(2610), Duration.FromDays(4), referenceDate);
215215
}
216216

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

246-
runBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
246+
RunBaseTest(schedule, Duration.FromHours(168), Duration.FromDays(28), referenceDate);
247247
}
248248

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

277-
runBaseTest(schedule, Duration.FromHours(24), Duration.FromDays(3), referenceDate);
277+
RunBaseTest(schedule, Duration.FromHours(24), Duration.FromDays(3), referenceDate);
278278
}
279279

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

354-
runBaseTest(schedule, Duration.FromHours(280), Duration.FromDays(49), referenceDate.PlusDays(49));
354+
RunBaseTest(schedule, Duration.FromHours(280), Duration.FromDays(49), referenceDate.PlusDays(49));
355355

356356
}
357357

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

383-
runBaseTest(schedule, Duration.FromHours(12), Duration.FromDays(1), referenceDate);
383+
RunBaseTest(schedule, Duration.FromHours(12), Duration.FromDays(1), referenceDate);
384384
}
385385

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

430-
runBaseTest(schedule, Duration.FromHours(336), Duration.FromDays(56), referenceDate);
430+
RunBaseTest(schedule, Duration.FromHours(336), Duration.FromDays(56), referenceDate);
431431
}
432432
}
433433
}

TestShiftSharp/TestSnippet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class TestSnippet : BaseTest
3737
[TestMethod]
3838
public void TestPartial()
3939
{
40-
4140
}
4241
}
4342
}
43+

TestShiftSharp/TestWorkSchedule.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void TestNursingICUShifts()
7676
schedule.CreateTeam("C", "Night shift", nightRotation, rotationStart);
7777
schedule.CreateTeam("D", "Night inverse shift", inverseNightRotation, rotationStart);
7878

79-
runBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), rotationStart);
79+
RunBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), rotationStart);
8080
}
8181

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

108-
runBaseTest(schedule, Duration.FromHours(63), Duration.FromDays(42), rotationStart);
108+
RunBaseTest(schedule, Duration.FromHours(63), Duration.FromDays(42), rotationStart);
109109
}
110110

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

130-
runBaseTest(schedule, Duration.FromHours(48), Duration.FromDays(8), new LocalDate(2014, 2, 4));
130+
RunBaseTest(schedule, Duration.FromHours(48), Duration.FromDays(8), new LocalDate(2014, 2, 4));
131131
}
132132

133133
[TestMethod]
@@ -161,7 +161,7 @@ public void TestFirefighterShifts1()
161161
Assert.IsTrue(instances.Count == 1);
162162
Assert.IsTrue(instances[0].Team.Equals(platoon2));
163163

164-
runBaseTest(schedule, Duration.FromHours(144), Duration.FromDays(18), new LocalDate(2017, 2, 1));
164+
RunBaseTest(schedule, Duration.FromHours(144), Duration.FromDays(18), new LocalDate(2017, 2, 1));
165165
}
166166

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

193-
runBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), new LocalDate(2014, 1, 9));
193+
RunBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), new LocalDate(2014, 1, 9));
194194
}
195195

196196
[TestMethod]
@@ -330,7 +330,7 @@ public void TestGenericShift()
330330

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

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

335335
}
336336

0 commit comments

Comments
 (0)