TSK-1765: WorkingTimeCalculator now works without time zone with correct working hours.

This commit is contained in:
Holger Hagen 2021-11-04 11:49:16 +01:00 committed by holgerhagen
parent ac952ee3ec
commit af0703e2af
3 changed files with 35 additions and 31 deletions

View File

@ -31,4 +31,9 @@ public class LocalTimeInterval {
public void setEnd(LocalTime end) {
this.end = end;
}
@Override
public String toString() {
return "LocalTimeInterval [begin=" + begin + ", end=" + end + "]";
}
}

View File

@ -15,22 +15,21 @@ import pro.taskana.common.api.exceptions.InvalidArgumentException;
public class WorkingTimeCalculator {
private static final Map<DayOfWeek, LocalTimeInterval> WORKING_TIME;
public static final Map<DayOfWeek, LocalTimeInterval> WORKING_TIME;
static {
WORKING_TIME = new EnumMap<>(DayOfWeek.class);
WORKING_TIME.put(
DayOfWeek.MONDAY, new LocalTimeInterval(LocalTime.of(9, 0), LocalTime.of(17, 0)));
DayOfWeek.MONDAY, new LocalTimeInterval(LocalTime.of(6, 0), LocalTime.of(18, 0)));
WORKING_TIME.put(
DayOfWeek.TUESDAY, new LocalTimeInterval(LocalTime.of(9, 0), LocalTime.of(17, 0)));
DayOfWeek.TUESDAY, new LocalTimeInterval(LocalTime.of(6, 0), LocalTime.of(18, 0)));
WORKING_TIME.put(
DayOfWeek.WEDNESDAY, new LocalTimeInterval(LocalTime.of(9, 0), LocalTime.of(17, 0)));
DayOfWeek.WEDNESDAY, new LocalTimeInterval(LocalTime.of(6, 0), LocalTime.of(18, 0)));
WORKING_TIME.put(
DayOfWeek.THURSDAY, new LocalTimeInterval(LocalTime.of(9, 0), LocalTime.of(17, 0)));
DayOfWeek.THURSDAY, new LocalTimeInterval(LocalTime.of(6, 0), LocalTime.of(18, 0)));
WORKING_TIME.put(
DayOfWeek.FRIDAY, new LocalTimeInterval(LocalTime.of(9, 0), LocalTime.of(17, 0)));
WORKING_TIME.put(
DayOfWeek.SATURDAY, new LocalTimeInterval(LocalTime.of(10, 0), LocalTime.of(15, 0)));
DayOfWeek.FRIDAY, new LocalTimeInterval(LocalTime.of(6, 0), LocalTime.of(18, 0)));
WORKING_TIME.put(DayOfWeek.SATURDAY, null);
WORKING_TIME.put(DayOfWeek.SUNDAY, null);
}

View File

@ -21,8 +21,8 @@ class WorkingTimeCalculatorTest {
@Test
void should_throwInvalidArgumentException_WhenFromTimeIsAfterUntilTime() {
Instant from = Instant.parse("2021-09-30T13:00:00.000Z");
Instant to = Instant.parse("2021-09-30T10:00:00.000Z");
Instant from = Instant.parse("2021-09-30T12:00:00.000Z");
Instant to = Instant.parse("2021-09-30T09:00:00.000Z");
assertThatThrownBy(() -> calculator.workingTimeBetweenTwoTimestamps(from, to))
.isInstanceOf(InvalidArgumentException.class)
@ -31,7 +31,7 @@ class WorkingTimeCalculatorTest {
@Test
void should_throwInvalidArgumentException_WhenFromIsNull() {
Instant to = Instant.parse("2021-09-30T10:00:00.000Z");
Instant to = Instant.parse("2021-09-30T09:00:00.000Z");
assertThatThrownBy(() -> calculator.workingTimeBetweenTwoTimestamps(null, to))
.isInstanceOf(InvalidArgumentException.class)
@ -40,8 +40,8 @@ class WorkingTimeCalculatorTest {
@Test
void should_ReturnWorkingTime_When_InstantsWithinSameHour() throws Exception {
Instant from = Instant.parse("2021-09-30T10:02:00.000Z");
Instant to = Instant.parse("2021-09-30T10:38:00.000Z");
Instant from = Instant.parse("2021-09-30T09:02:00.000Z");
Instant to = Instant.parse("2021-09-30T09:38:00.000Z");
Duration duration = calculator.workingTimeBetweenTwoTimestamps(from, to);
@ -50,8 +50,8 @@ class WorkingTimeCalculatorTest {
@Test
void should_ReturnWorkingTime_When_InstantsWithinTheSameDay() throws Exception {
Instant thursdayMorning = Instant.parse("2021-09-30T10:00:00.000Z");
Instant thursdayEvening = Instant.parse("2021-09-30T15:00:00.000Z");
Instant thursdayMorning = Instant.parse("2021-09-30T09:00:00.000Z");
Instant thursdayEvening = Instant.parse("2021-09-30T14:00:00.000Z");
Duration duration =
calculator.workingTimeBetweenTwoTimestamps(thursdayMorning, thursdayEvening);
@ -61,13 +61,13 @@ class WorkingTimeCalculatorTest {
@Test
void should_ReturnWorkingTime_When_InstantsWithinTheSameDayStartBeforeHours() throws Exception {
Instant thursdayMorning = Instant.parse("2021-09-30T08:00:00.000Z");
Instant thursdayEvening = Instant.parse("2021-09-30T15:00:00.000Z");
Instant thursdayMorning = Instant.parse("2021-09-30T07:00:00.000Z");
Instant thursdayEvening = Instant.parse("2021-09-30T14:00:00.000Z");
Duration duration =
calculator.workingTimeBetweenTwoTimestamps(thursdayMorning, thursdayEvening);
assertThat(duration).isEqualTo(Duration.of(6, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(7, ChronoUnit.HOURS));
}
@Test
@ -78,7 +78,7 @@ class WorkingTimeCalculatorTest {
Duration duration =
calculator.workingTimeBetweenTwoTimestamps(thursdayMorning, thursdayEvening);
assertThat(duration).isEqualTo(Duration.of(7, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(8, ChronoUnit.HOURS));
}
@Test
@ -118,22 +118,22 @@ class WorkingTimeCalculatorTest {
@Test
void should_ReturnWorkingTime_When_InstantsWithinTheSameWeek() throws Exception {
Instant fromMonday = Instant.parse("2021-09-27T10:00:00.000Z");
Instant toSaturday = Instant.parse("2021-10-02T15:00:00.000Z");
Instant fromMonday = Instant.parse("2021-09-27T09:00:00.000Z");
Instant toSaturday = Instant.parse("2021-10-02T14:00:00.000Z");
Duration duration = calculator.workingTimeBetweenTwoTimestamps(fromMonday, toSaturday);
assertThat(duration).isEqualTo(Duration.of(44, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(57, ChronoUnit.HOURS));
}
@Test
void should_ReturnWorkingTime_When_UntilInstantIsBeforeWorkingHours() throws Exception {
Instant thursday = Instant.parse("2021-09-30T10:00:00.000Z");
Instant friday = Instant.parse("2021-10-01T06:00:00.000Z");
Instant friday = Instant.parse("2021-10-01T05:00:00.000Z");
Duration duration = calculator.workingTimeBetweenTwoTimestamps(thursday, friday);
assertThat(duration).isEqualTo(Duration.of(7, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(8, ChronoUnit.HOURS));
}
@Test
@ -143,17 +143,17 @@ class WorkingTimeCalculatorTest {
Duration duration = calculator.workingTimeBetweenTwoTimestamps(thursday, friday);
assertThat(duration).isEqualTo(Duration.of(15, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(20, ChronoUnit.HOURS));
}
@Test
void should_ReturnWorkingTime_When_FromInstantIsBeforeWorkingHours() throws Exception {
Instant thursday = Instant.parse("2021-09-30T06:00:00.000Z");
Instant thursday = Instant.parse("2021-09-30T05:00:00.000Z");
Instant friday = Instant.parse("2021-10-01T10:00:00.000Z");
Duration duration = calculator.workingTimeBetweenTwoTimestamps(thursday, friday);
assertThat(duration).isEqualTo(Duration.of(9, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(16, ChronoUnit.HOURS));
}
@Test
@ -163,7 +163,7 @@ class WorkingTimeCalculatorTest {
Duration duration = calculator.workingTimeBetweenTwoTimestamps(thursday, friday);
assertThat(duration).isEqualTo(Duration.of(1, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(4, ChronoUnit.HOURS));
}
@Test
@ -173,7 +173,7 @@ class WorkingTimeCalculatorTest {
Duration duration = calculator.workingTimeBetweenTwoTimestamps(fromFriday, toMonday);
assertThat(duration).isEqualTo(Duration.of(8, ChronoUnit.HOURS));
assertThat(duration).isEqualTo(Duration.of(7, ChronoUnit.HOURS));
}
@Test
@ -197,7 +197,7 @@ class WorkingTimeCalculatorTest {
}
@Test
void should_ReturMultipleWorkingTimes_When_CalculatorUsedMultipleTimes() throws Exception {
void should_ReturnMultipleWorkingTimes_When_CalculatorUsedMultipleTimes() throws Exception {
Instant from1 = Instant.parse("2021-09-30T10:02:00.000Z");
Instant to1 = Instant.parse("2021-09-30T10:38:00.000Z");
@ -210,6 +210,6 @@ class WorkingTimeCalculatorTest {
Duration duration2 = calculator.workingTimeBetweenTwoTimestamps(from2, to2);
assertThat(duration2).isEqualTo(Duration.of(44, ChronoUnit.HOURS));
assertThat(duration2).isEqualTo(Duration.of(56, ChronoUnit.HOURS));
}
}