public void TestManufacturingShifts() { // manufacturing company schedule = new WorkSchedule("Manufacturing Company - four twelves", "Four 12 hour alternating day/night shifts"); // day shift, start at 07:00 for 12 hours Shift day = schedule.CreateShift("Day", "Day shift", new LocalTime(7, 0, 0), Duration.FromHours(12)); // night shift, start at 19:00 for 12 hours Shift night = schedule.CreateShift("Night", "Night shift", new LocalTime(19, 0, 0), Duration.FromHours(12)); // 7 days ON, 7 OFF Rotation dayRotation = schedule.CreateRotation("Day", "Day"); dayRotation.AddSegment(day, 7, 7); // 7 nights ON, 7 OFF Rotation nightRotation = schedule.CreateRotation("Night", "Night"); nightRotation.AddSegment(night, 7, 7); schedule.CreateTeam("A", "A day shift", dayRotation, new LocalDate(2014, 1, 2)); schedule.CreateTeam("B", "B night shift", nightRotation, new LocalDate(2014, 1, 2)); 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)); }
private static void RemoveEachDataTable <TCache>(TCache cache, int iteration) where TCache : ICache { var cacheName = cache.Settings.CacheName; Console.WriteLine(Spacer); Logger.Info($"[{cacheName}] Removing each data table, iteration {iteration}..."); foreach (var table in _tables) { cache.AddTimed(table.TableName, table, cache.Clock.GetCurrentInstant() + Duration.FromHours(1)); } var stopwatch = new Stopwatch(); stopwatch.Start(); foreach (var table in _tables) { cache.Remove(table.TableName); } stopwatch.Stop(); Debug.Assert(cache.Count() == 0); Debug.Assert(cache.LongCount() == 0L); Logger.Info($"[{cacheName}] Data tables removed in: {stopwatch.Elapsed}"); Logger.Info($"[{cacheName}] Current cache size: {cache.GetCacheSizeInBytes() / (1024.0 * 1024.0):0.0} MB"); Logger.Info($"[{cacheName}] Approximate speed (MB/sec): {_tableListSize / stopwatch.Elapsed.TotalSeconds:0.0}"); }
/// <summary> /// EWF use only. /// </summary> public static void SetWarningsHiddenCookie() => CookieStatics.SetCookie(warningsHiddenCookieName, "", SystemClock.Instance.GetCurrentInstant() + Duration.FromHours(1), false, false);
public void DurationMultipliedByElectricCurrentEqualsElectricCharge() { ElectricCharge ah = Duration.FromHours(5) * ElectricCurrent.FromAmperes(4); Assert.Equal(20, ah.AmpereHours); }
public void ElectricChargeDividedByDurationEqualsElectricCurrent() { ElectricCurrent i = ElectricCharge.FromAmpereHours(20) / Duration.FromHours(4); Assert.Equal(5, i.Amperes); }
/// <inheritdoc cref="Duration.FromHours(double)"/> public static Duration Hours(this decimal value) => Duration.FromHours(Convert.ToDouble(value));
/// <inheritdoc cref="Duration.FromHours(double?)"/> public static Duration?Hours(this long?value) => Duration.FromHours(value);
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 = schedule.CreateRotation("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))); }
public void TestPostalServiceShifts() { // United States Postal Service schedule = new WorkSchedule("USPS", "Six 9 hr shifts, rotating every 42 days"); // shift, start at 08:00 for 9 hours Shift day = schedule.CreateShift("Day", "day shift", new LocalTime(8, 0, 0), Duration.FromHours(9)); Rotation rotation = schedule.CreateRotation("Day", "Day"); rotation.AddSegment(day, 3, 7); rotation.AddSegment(day, 1, 7); rotation.AddSegment(day, 1, 7); rotation.AddSegment(day, 1, 7); rotation.AddSegment(day, 1, 7); LocalDate rotationStart = new LocalDate(2017, 1, 27); // day teams schedule.CreateTeam("Team A", "A team", rotation, rotationStart); schedule.CreateTeam("Team B", "B team", rotation, rotationStart.Minus(Period.FromDays(7))); schedule.CreateTeam("Team C", "C team", rotation, rotationStart.Minus(Period.FromDays(14))); schedule.CreateTeam("Team D", "D team", rotation, rotationStart.Minus(Period.FromDays(21))); 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); }
public void TestShiftWorkingTime() { schedule = new WorkSchedule("Working Time1", "Test working time"); // shift does not cross midnight Duration shiftDuration = Duration.FromHours(8); LocalTime shiftStart = new LocalTime(7, 0, 0); Shift shift = schedule.CreateShift("Work Shift1", "Working time shift", shiftStart, shiftDuration); LocalTime shiftEnd = shift.GetEnd(); // case #1 Duration time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2))); Assert.IsTrue(time.TotalSeconds == 0); time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3))); Assert.IsTrue(time.TotalSeconds == 0); // case #2 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1)); Assert.IsTrue(time.TotalSeconds == 3600); // case #3 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2)); Assert.IsTrue(time.TotalSeconds == 3600); // case #4 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1)); Assert.IsTrue(time.TotalSeconds == 3600); // case #5 time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2)); Assert.IsTrue(time.TotalSeconds == 0); time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1)); Assert.IsTrue(time.TotalSeconds == 0); // case #6 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1)); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #7 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1)); Assert.IsTrue(time.TotalSeconds == 0); // case #8 time = shift.CalculateWorkingTime(shiftStart, shiftEnd); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #9 time = shift.CalculateWorkingTime(shiftStart, shiftStart); Assert.IsTrue(time.TotalSeconds == 0); // case #10 time = shift.CalculateWorkingTime(shiftEnd, shiftEnd); Assert.IsTrue(time.TotalSeconds == 0); // case #11 time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1)); Assert.IsTrue(time.TotalSeconds == 1); // case #12 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd); Assert.IsTrue(time.TotalSeconds == 1); // 8 hr shift crossing midnight shiftStart = new LocalTime(22, 0, 0); shift = schedule.CreateShift("Work Shift2", "Working time shift", shiftStart, shiftDuration); shiftEnd = shift.GetEnd(); // case #1 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2)), true); Assert.IsTrue(time.TotalSeconds == 0); time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3)), true); Assert.IsTrue(time.TotalSeconds == 0); // case #2 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #3 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #4 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), false); Assert.IsTrue(time.TotalSeconds == 3600); // case #5 time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2), true); Assert.IsTrue(time.TotalSeconds == 0); time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 0); // case #6 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #7 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 0); // case #8 time = shift.CalculateWorkingTime(shiftStart, shiftEnd, true); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #9 time = shift.CalculateWorkingTime(shiftStart, shiftStart, true); Assert.IsTrue(time.TotalSeconds == 0); // case #10 time = shift.CalculateWorkingTime(shiftEnd, shiftEnd, true); Assert.IsTrue(time.TotalSeconds == 0); // case #11 time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1), true); Assert.IsTrue(time.TotalSeconds == 1); // case #12 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd, false); Assert.IsTrue(time.TotalSeconds == 1); // 24 hr shift crossing midnight shiftDuration = Duration.FromHours(24); shiftStart = new LocalTime(7, 0, 0); shift = schedule.CreateShift("Work Shift3", "Working time shift", shiftStart, shiftDuration); shiftEnd = shift.GetEnd(); // case #1 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2)), false); Assert.IsTrue(time.TotalSeconds == 3600); time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3)), true); Assert.IsTrue(time.TotalSeconds == 0); // case #2 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #3 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #4 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #5 time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2), true); Assert.IsTrue(time.TotalSeconds == 3600); time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 0); // case #6 time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 3600); // case #7 time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1), true); Assert.IsTrue(time.TotalSeconds == 0); // case #8 time = shift.CalculateWorkingTime(shiftStart, shiftEnd, true); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #9 time = shift.CalculateWorkingTime(shiftStart, shiftStart, true); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #10 time = shift.CalculateWorkingTime(shiftEnd, shiftEnd, true); Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds); // case #11 time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1), true); Assert.IsTrue(time.TotalSeconds == 1); // case #12 time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd, false); Assert.IsTrue(time.TotalSeconds == 1); }
public void TestTeamWorkingTime() { schedule = new WorkSchedule("Team Working Time", "Test team working time"); Duration shiftDuration = Duration.FromHours(12); Duration halfShift = Duration.FromHours(6); LocalTime shiftStart = new LocalTime(7, 0, 0); Shift shift = schedule.CreateShift("Team Shift1", "Team shift 1", shiftStart, shiftDuration); Rotation rotation = schedule.CreateRotation("Team", "Rotation"); rotation.AddSegment(shift, 1, 1); LocalDate startRotation = new LocalDate(2017, 1, 1); Team team = schedule.CreateTeam("Team", "Team", rotation, startRotation); team.RotationStart = startRotation; // case #1 LocalDateTime from = startRotation.PlusDays(rotation.GetDayCount()).At(shiftStart); LocalDateTime to = from.PlusDays(1); Duration time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #2 to = from.PlusDays(2); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #3 to = from.PlusDays(3); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); // case #4 to = from.PlusDays(4); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); // case #5 from = startRotation.PlusDays(rotation.GetDayCount()).At(shiftStart.PlusHours(6)); to = from.PlusDays(1); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(halfShift)); // case #6 to = from.PlusDays(2); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #7 to = from.PlusDays(3); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(halfShift))); // case #8 to = from.PlusDays(4); time = team.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); // now crossing midnight shiftStart = new LocalTime(18, 0, 0); Shift shift2 = schedule.CreateShift("Team Shift2", "Team shift 2", shiftStart, shiftDuration); Rotation rotation2 = schedule.CreateRotation("Case 8", "Case 8"); rotation2.AddSegment(shift2, 1, 1); Team team2 = schedule.CreateTeam("Team2", "Team 2", rotation2, startRotation); team2.RotationStart = startRotation; // case #1 from = startRotation.PlusDays(rotation.GetDayCount()).At(shiftStart); to = from.PlusDays(1); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #2 to = from.PlusDays(2); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #3 to = from.PlusDays(3); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); // case #4 to = from.PlusDays(4); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); // case #5 from = startRotation.PlusDays(rotation.GetDayCount()).At(LocalTime.MaxValue); to = from.PlusDays(1); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(halfShift)); // case #6 to = from.PlusDays(2); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration)); // case #7 to = from.PlusDays(3); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(halfShift))); // case #8 to = from.PlusDays(4); time = team2.CalculateWorkingTime(from, to); Assert.IsTrue(time.Equals(shiftDuration.Plus(shiftDuration))); }
public void TestNursingICUShifts() { // ER nursing schedule schedule = new WorkSchedule("Nursing ICU", "Two 12 hr back-to-back shifts, rotating every 14 days"); // day shift, starts at 06:00 for 12 hours Shift day = schedule.CreateShift("Day", "Day shift", new LocalTime(6, 0, 0), Duration.FromHours(12)); // night shift, starts at 18:00 for 12 hours Shift night = schedule.CreateShift("Night", "Night shift", new LocalTime(18, 0, 0), Duration.FromHours(12)); // day rotation Rotation dayRotation = schedule.CreateRotation("Day", "Day"); dayRotation.AddSegment(day, 3, 4); dayRotation.AddSegment(day, 4, 3); // inverse day rotation (day + 3 days) Rotation inverseDayRotation = schedule.CreateRotation("Inverse Day", "Inverse Day"); inverseDayRotation.AddSegment(day, 0, 3); inverseDayRotation.AddSegment(day, 4, 4); inverseDayRotation.AddSegment(day, 3, 0); // night rotation Rotation nightRotation = schedule.CreateRotation("Night", "Night"); nightRotation.AddSegment(night, 4, 3); nightRotation.AddSegment(night, 3, 4); // inverse night rotation Rotation inverseNightRotation = schedule.CreateRotation("Inverse Night", "Inverse Night"); inverseNightRotation.AddSegment(night, 0, 4); inverseNightRotation.AddSegment(night, 3, 3); inverseNightRotation.AddSegment(night, 4, 0); LocalDate rotationStart = new LocalDate(2014, 1, 6); schedule.CreateTeam("A", "Day shift", dayRotation, rotationStart); schedule.CreateTeam("B", "Day inverse shift", inverseDayRotation, rotationStart); schedule.CreateTeam("C", "Night shift", nightRotation, rotationStart); schedule.CreateTeam("D", "Night inverse shift", inverseNightRotation, rotationStart); RunBaseTest(schedule, Duration.FromHours(84), Duration.FromDays(14), rotationStart); }
public void TestExceptions() { schedule = new WorkSchedule("Exceptions", "Test exceptions"); Duration shiftDuration = Duration.FromHours(24); LocalTime shiftStart = new LocalTime(7, 0, 0); NonWorkingPeriod period = schedule.CreateNonWorkingPeriod("Non-working", "Non-working period", new LocalDateTime(2017, 1, 1, 0, 0, 0), Duration.FromHours(24)); try { period.Duration = Duration.FromSeconds(0); Assert.Fail(); } catch (Exception) { } try { // same period schedule.CreateNonWorkingPeriod("Non-working", "Non-working period", new LocalDateTime(2017, 1, 1, 0, 0, 0), Duration.FromHours(24)); Assert.Fail(); } catch (Exception) { } // shift Shift shift = schedule.CreateShift("Test", "Test shift", shiftStart, shiftDuration); try { // crosses midnight shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shift.GetEnd().PlusHours(1)); Assert.Fail(); } catch (Exception) { } try { shift.Duration = Duration.FromSeconds(0); Assert.Fail(); } catch (Exception) { } try { shift.Duration = Duration.FromSeconds(48 * 3600); Assert.Fail(); } catch (Exception) { } try { // same shift shift = schedule.CreateShift("Test", "Test shift", shiftStart, shiftDuration); Assert.Fail(); } catch (Exception) { } Rotation rotation = schedule.CreateRotation("Rotation", "Rotation"); rotation.AddSegment(shift, 5, 2); LocalDate startRotation = new LocalDate(2016, 12, 31); Team team = schedule.CreateTeam("Team", "Team", rotation, startRotation); // ok schedule.CalculateWorkingTime(new LocalDateTime(2017, 1, 1, 7, 0, 0), new LocalDateTime(2017, 2, 1, 0, 0, 0)); try { // end before start schedule.CalculateWorkingTime(new LocalDateTime(2017, 1, 2, 0, 0, 0), new LocalDateTime(2017, 1, 1, 0, 0, 0)); Assert.Fail(); } catch (Exception) { } try { // same team team = schedule.CreateTeam("Team", "Team", rotation, startRotation); Assert.Fail(); } catch (Exception) { } try { // date before start team.GetDayInRotation(new LocalDate(2016, 1, 1)); Assert.Fail(); } catch (Exception) { } try { // end before start schedule.PrintShiftInstances(new LocalDate(2017, 1, 2), new LocalDate(2017, 1, 1)); Assert.Fail(); } catch (Exception) { } try { // delete in-use shift schedule.DeleteShift(shift); Assert.Fail(); } catch (Exception) { } // breaks Break lunch = shift.CreateBreak("Lunch", "Lunch", new LocalTime(12, 0, 0), Duration.FromMinutes(60)); lunch.Duration = Duration.FromMinutes(30); lunch.StartTime = new LocalTime(11, 30, 0); shift.RemoveBreak(lunch); shift.RemoveBreak(lunch); Shift shift2 = schedule.CreateShift("Test2", "Test shift2", shiftStart, shiftDuration); Assert.IsFalse(shift.Equals(shift2)); Break lunch2 = shift2.CreateBreak("Lunch2", "Lunch", new LocalTime(12, 0, 0), Duration.FromMinutes(60)); shift.RemoveBreak(lunch2); // ok to delete WorkSchedule schedule2 = new WorkSchedule("Exceptions2", "Test exceptions2"); schedule2.Name = "Schedule 2"; schedule2.Description = "a description"; schedule2.DeleteShift(shift); schedule2.DeleteTeam(team); schedule2.DeleteNonWorkingPeriod(period); // nulls try { schedule.CreateShift(null, "1", shiftStart, Duration.FromMinutes(60)); Assert.Fail(); } catch (Exception) { } Assert.IsFalse(shift.Equals(rotation)); // hashcode() team.GetHashCode(); String name = team.Name; Dictionary <String, Team> teams = new Dictionary <String, Team>(); teams[name] = team; Team t = teams[name]; }
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 = schedule.CreateRotation("Shift1", "Shift1"); rotation1.AddSegment(shift1, 5, 2); // shift 2, 5 days ON, 2 OFF Rotation rotation2 = schedule.CreateRotation("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)); }
private async Task <object> Convert(CommandMessage message, string arg, Type type) { #pragma warning disable SA1121, IDE0049 if (type == typeof(string)) { if (!arg.Contains("\"")) { throw new Exception("strings must be wrapped in quotations"); } return(arg.Replace("\"", string.Empty)); } else if (type == typeof(double)) { return(double.Parse(arg)); } else if (type == typeof(int)) { return(int.Parse(arg)); } else if (type == typeof(uint)) { return(uint.Parse(arg)); } else if (type == typeof(UInt64)) { return(UInt64.Parse(arg)); } else if (type == typeof(bool)) { return(bool.Parse(arg)); } else if (type == typeof(SocketTextChannel)) { string str = arg; str = str.Replace("<", string.Empty); str = str.Replace(">", string.Empty); str = str.Replace("#", string.Empty); ulong id = ulong.Parse(str); SocketChannel channel = Program.DiscordClient.GetChannel(id); if (channel is SocketTextChannel) { return(channel); } else if (channel is null) { throw new Exception("Invalid channel ID: " + id); } else { throw new Exception("Channel is not a Text Channel"); } } else if (type == typeof(IEmote)) { IEmote emote = EmoteUtility.Parse(arg); if (emote is Emote emoteActual && !emoteActual.IsAvailable()) { throw new UserException("I'm sorry, I don't have that emote."); } return(emote); } else if (type == typeof(IUser)) { string str = arg; str = str.Replace("<", string.Empty); str = str.Replace(">", string.Empty); str = str.Replace("@", string.Empty); str = str.Replace("!", string.Empty); ulong id = ulong.Parse(str); IUser user = Program.DiscordClient.GetUser(id); if (user == null) { throw new Exception("Invalid user Id: " + arg); } return(user); } else if (type == typeof(IGuildUser)) { string str = arg; str = str.Replace("<", string.Empty); str = str.Replace(">", string.Empty); str = str.Replace("@", string.Empty); str = str.Replace("!", string.Empty); ulong id = ulong.Parse(str); IGuildUser user = await message.Guild.GetUserAsync(id); if (user == null) { throw new Exception("Invalid user Id: " + arg); } return(user); } else if (type == typeof(IRole)) { // <@&663326776696504352> string str = arg; str = str.Replace("<@&", string.Empty); str = str.Replace(">", string.Empty); ulong id = ulong.Parse(str); IRole role = message.Guild.GetRole(id); if (role == null) { throw new Exception("Invalid role Id: " + arg); } return(role); } else if (type == typeof(Duration)) { string str = arg.ToLower(); string[] parts = Regex.Split(str, @"(?<=[dhms])"); Duration duration = Duration.FromSeconds(0); foreach (string part in parts) { if (part.Contains('d')) { int val = int.Parse(part.Replace('d', '\0')); duration += Duration.FromDays(val); } else if (part.Contains('h')) { int val = int.Parse(part.Replace('h', '\0')); duration += Duration.FromHours(val); } else if (part.Contains('m')) { int val = int.Parse(part.Replace('m', '\0')); duration += Duration.FromMinutes(val); } else if (part.Contains('s')) { int val = int.Parse(part.Replace('s', '\0')); duration += Duration.FromSeconds(val); } } return(duration); } throw new Exception("Unsupported parameter type: \"" + type + "\""); #pragma warning restore }
public void TestTeamWorkingTime2() { schedule = new WorkSchedule("4 Team Plan", "test schedule"); // Day shift #1, starts at 07:00 for 15.5 hours Shift crossover = schedule.CreateShift("Crossover", "Day shift #1 cross-over", new LocalTime(7, 0, 0), Duration.FromHours(15).Plus(Duration.FromMinutes(30))); // Day shift #2, starts at 07:00 for 14 hours Shift day = schedule.CreateShift("Day", "Day shift #2", new LocalTime(7, 0, 0), Duration.FromHours(14)); // Night shift, starts at 22:00 for 14 hours Shift night = schedule.CreateShift("Night", "Night shift", new LocalTime(22, 0, 0), Duration.FromHours(14)); // Team 4-day rotation Rotation rotation = schedule.CreateRotation("4 Team", "4 Team"); rotation.AddSegment(day, 1, 0); rotation.AddSegment(crossover, 1, 0); rotation.AddSegment(night, 1, 1); Team team1 = schedule.CreateTeam("Team 1", "First team", rotation, referenceDate); // partial in Day 1 LocalTime am7 = new LocalTime(7, 0, 0); LocalDate testStart = referenceDate.PlusDays(rotation.GetDayCount()); LocalDateTime from = testStart.At(am7); LocalDateTime to = testStart.At(am7.PlusHours(1)); //------------------------------------------------------------------ // from first day in rotation for Team1 from = testStart.At(LocalTime.Midnight); to = testStart.At(LocalTime.MaxValue); Duration duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(14))); to = testStart.PlusDays(1).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(29).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(2).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(31).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(3).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(43).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(4).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(57).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(5).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(73))); to = testStart.PlusDays(6).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(75))); to = testStart.PlusDays(7).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(87))); // from third day in rotation for Team1 from = testStart.PlusDays(2).At(LocalTime.Midnight); to = testStart.PlusDays(2).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(2))); to = testStart.PlusDays(3).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(14))); to = testStart.PlusDays(4).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(28))); to = testStart.PlusDays(5).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(43).Plus(Duration.FromMinutes(30)))); to = testStart.PlusDays(6).At(LocalTime.MaxValue); duration = team1.CalculateWorkingTime(from, to); Assert.IsTrue(duration.Equals(Duration.FromHours(45).Plus(Duration.FromMinutes(30)))); }
public async Task SnapshotWithPriceHistoryInBaseCurrency() { YahooQuotes yahooQuotes = new YahooQuotesBuilder() .HistoryStarting(Instant.FromUtc(2020, 7, 15, 0, 0)) .WithCaching(snapshotDuration: Duration.FromMinutes(30), historyDuration: Duration.FromHours(6)) .Build(); Security security = await yahooQuotes .GetAsync("TSLA", HistoryFlags.PriceHistory, historyBase : "JPY=X") ?? throw new ArgumentException("Unknown symbol: TSLA."); Assert.Equal("Tesla, Inc.", security.ShortName); Assert.Equal("USD", security.Currency); Assert.Equal("America/New_York", security.ExchangeTimezone?.Id); PriceTick tick = security.PriceHistory.Value[0]; Assert.Equal(new LocalDate(2020, 7, 15), tick.Date); Assert.Equal(309.202, tick.Close); // in USD var instant = new LocalDateTime(2020, 7, 15, 16, 0, 0) .InZoneLeniently(security.ExchangeTimezone !).ToInstant(); ValueTick tickBase = security.PriceHistoryBase.Value[0]; Assert.Equal(instant, tickBase.Date); Assert.Equal(33139, tickBase.Value, 0); // in JPY }
/// <summary> /// Advances the clock by the given number of hours. /// </summary> /// <param name="hours">The number of hours to advance the clock by (or if negative, the number to move it /// back by).</param> public void AdvanceHours(int hours) { Advance(Duration.FromHours(hours)); }
/// <inheritdoc cref="Duration.FromHours(double?)"/> public static Duration?Hours(this decimal?value) => Duration.FromHours(value == null ? (double?)null : Convert.ToDouble(value.Value));
public void NumberToHoursTest() => Assert.Equal(Duration.FromHours(2), 2.Hours());
/// <inheritdoc cref="Duration.FromHours(double?)"/> public static Duration?Hours(this double?value) => Duration.FromHours(value);
public void Duration_WholeSeconds() { AssertConversions(Duration.FromHours(48), "48:00:00", NodaSerializerDefinitions.DurationSerializer); }
public void SafePlus_NormalTime() { var localInstant = NodaConstants.UnixEpoch.SafePlus(Offset.FromHours(1)); Assert.AreEqual(Duration.FromHours(1), localInstant.TimeSinceLocalEpoch); }
public void Duration_ParsePartialFractionalSecondsWithTrailingZeroes() { var parsed = NodaSerializerDefinitions.DurationSerializer.Deserialize("25:10:00.1234000"); Assert.Equal(Duration.FromHours(25) + Duration.FromMinutes(10) + Duration.FromTicks(1234000), parsed); }
public static Duration Hours <T>(this T value) => Duration.FromHours(Convert.ToDouble(value));
public void Duration_WholeSeconds() { AssertConversions(Duration.FromHours(48), "\"48:00:00\"", NodaConverters.DurationConverter); }
private static async Task RemoveEachDataTableAsync <TCache>(TCache cache, int iteration) where TCache : IAsyncCache { var cacheName = cache.Settings.CacheName; Console.WriteLine(Spacer); Logger.Info($"[{cacheName}] Removing each data table asynchronously, iteration {iteration}..."); await _tables.ParallelForEachAsync(async table => { await cache.AddTimedAsync(table.TableName, table, cache.Clock.GetCurrentInstant() + Duration.FromHours(1)); }, maxDegreeOfParalellism : Environment.ProcessorCount * 2); var stopwatch = new Stopwatch(); stopwatch.Start(); await _tables.ParallelForEachAsync(async table => { await cache.RemoveAsync(table.TableName); }, maxDegreeOfParalellism : Environment.ProcessorCount * 2); stopwatch.Stop(); Debug.Assert(await cache.CountAsync() == 0); Debug.Assert(await cache.LongCountAsync() == 0L); Logger.Info($"[{cacheName}] Data tables removed in: {stopwatch.Elapsed}"); Logger.Info($"[{cacheName}] Current cache size: {await cache.GetCacheSizeInBytesAsync() / (1024.0 * 1024.0):0.0} MB"); Logger.Info($"[{cacheName}] Approximate speed (MB/sec): {_tableListSize / stopwatch.Elapsed.TotalSeconds:0.0}"); }
public void Duration_ParsePartialFractionalSecondsWithTrailingZeroes() { var parsed = JsonConvert.DeserializeObject <Duration>("\"25:10:00.1234000\"", NodaConverters.DurationConverter); Assert.AreEqual(Duration.FromHours(25) + Duration.FromMinutes(10) + Duration.FromTicks(1234000), parsed); }
/// <inheritdoc cref="Duration.FromHours(UnitsNet.QuantityValue)" /> public static Duration Hours(this float value) => Duration.FromHours(value);
public void TestFirefighterShifts1() { // Kern Co, CA schedule = new WorkSchedule("Kern Co.", "Three 24 hour alternating shifts"); // shift, start 07:00 for 24 hours Shift shift = schedule.CreateShift("24 Hour", "24 hour shift", new LocalTime(7, 0, 0), Duration.FromHours(24)); // 2 days ON, 2 OFF, 2 ON, 2 OFF, 2 ON, 8 OFF Rotation rotation = schedule.CreateRotation("24 Hour", "2 days ON, 2 OFF, 2 ON, 2 OFF, 2 ON, 8 OFF"); rotation.AddSegment(shift, 2, 2); rotation.AddSegment(shift, 2, 2); rotation.AddSegment(shift, 2, 8); Team platoon1 = schedule.CreateTeam("Red", "A Shift", rotation, new LocalDate(2017, 1, 8)); Team platoon2 = schedule.CreateTeam("Black", "B Shift", rotation, new LocalDate(2017, 2, 1)); Team platoon3 = schedule.CreateTeam("Green", "C Shift", rotation, new LocalDate(2017, 1, 2)); List <ShiftInstance> instances = schedule.GetShiftInstancesForDay(new LocalDate(2017, 3, 1)); Assert.IsTrue(instances.Count == 1); Assert.IsTrue(instances[0].Team.Equals(platoon3)); instances = schedule.GetShiftInstancesForDay(new LocalDate(2017, 3, 3)); Assert.IsTrue(instances.Count == 1); Assert.IsTrue(instances[0].Team.Equals(platoon1)); instances = schedule.GetShiftInstancesForDay(new LocalDate(2017, 3, 9)); 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)); }