diff --git a/application/src/main/java/org/opentripplanner/updater/vehicle_position/RealtimeVehiclePatternMatcher.java b/application/src/main/java/org/opentripplanner/updater/vehicle_position/RealtimeVehiclePatternMatcher.java index ddcc5a64a4d..51d2cf82af7 100644 --- a/application/src/main/java/org/opentripplanner/updater/vehicle_position/RealtimeVehiclePatternMatcher.java +++ b/application/src/main/java/org/opentripplanner/updater/vehicle_position/RealtimeVehiclePatternMatcher.java @@ -146,12 +146,21 @@ private LocalDate inferServiceDate(Trip trip) { * {@see https://github.com/opentripplanner/OpenTripPlanner/issues/4058} */ protected static LocalDate inferServiceDate( - TripTimes staticTripTimes, + TripTimes staticTripTimes, ZoneId zoneId, Instant now ) { - var start = staticTripTimes.getScheduledDepartureTime(0); - var end = staticTripTimes.getScheduledDepartureTime(staticTripTimes.getNumStops() - 1); + int start; + int end; + if (staticTripTimes != null) { + start = staticTripTimes.getScheduledDepartureTime(0); + end = staticTripTimes.getScheduledDepartureTime(staticTripTimes.getNumStops() - 1); + } else { + // Create a fake trip starting at 6am and ending around midnight to make the logic below + // gravitate toward the most likely day, including late night trips past midnight. + start = (int) Duration.ofHours(6).toSeconds(); + end = (int) Duration.ofHours(24).toSeconds(); + } var today = now.atZone(zoneId).toLocalDate(); var yesterday = today.minusDays(1); diff --git a/application/src/test/java/org/opentripplanner/updater/vehicle_position/RealtimeVehicleMatcherTest.java b/application/src/test/java/org/opentripplanner/updater/vehicle_position/RealtimeVehicleMatcherTest.java index 43507735f5b..4a71eb5016a 100644 --- a/application/src/test/java/org/opentripplanner/updater/vehicle_position/RealtimeVehicleMatcherTest.java +++ b/application/src/test/java/org/opentripplanner/updater/vehicle_position/RealtimeVehicleMatcherTest.java @@ -324,15 +324,18 @@ public void clearOldTrips() { static Stream inferenceTestCases() { return Stream.of( - Arguments.of("2022-04-05T15:26:04+02:00", "2022-04-05"), - Arguments.of("2022-04-06T00:26:04+02:00", "2022-04-05"), - Arguments.of("2022-04-06T10:26:04+02:00", "2022-04-06") + Arguments.of("2022-04-05T15:26:04+02:00", true, "2022-04-05"), + Arguments.of("2022-04-06T00:26:04+02:00", true, "2022-04-05"), + Arguments.of("2022-04-06T10:26:04+02:00", true, "2022-04-06"), + Arguments.of("2022-04-05T15:26:04+02:00", false, "2022-04-05"), + Arguments.of("2022-04-06T00:26:04+02:00", false, "2022-04-05"), + Arguments.of("2022-04-06T10:26:04+02:00", false, "2022-04-06") ); } - @ParameterizedTest(name = "{0} should resolve to {1}") + @ParameterizedTest(name = "{0} + staticTripTimes included={1} should resolve to {2}") @MethodSource("inferenceTestCases") - void inferServiceDayOfTripAt6(String time, String expectedDate) { + void inferServiceDayOfTripAt6(String time, boolean hasTripTimes, String expectedDate) { var trip = TimetableRepositoryForTest.trip(tripId).build(); var sixOclock = (int) Duration.ofHours(18).toSeconds(); @@ -346,7 +349,11 @@ void inferServiceDayOfTripAt6(String time, String expectedDate) { var tripTimes = TripTimesFactory.tripTimes(trip, stopTimes, new Deduplicator()); var instant = OffsetDateTime.parse(time).toInstant(); - var inferredDate = RealtimeVehiclePatternMatcher.inferServiceDate(tripTimes, zoneId, instant); + var inferredDate = RealtimeVehiclePatternMatcher.inferServiceDate( + hasStopTime ? tripTimes : null, + zoneId, + instant + ); assertEquals(LocalDate.parse(expectedDate), inferredDate); } @@ -373,6 +380,17 @@ void inferServiceDateCloseToMidnight() { assertEquals(LocalDate.parse("2022-04-04"), inferredDate); } + @Test + void inferServiceDateCloseToMidnightIfNoStaticSchedule() { + var time = OffsetDateTime.parse("2022-04-05T00:04:00+02:00").toInstant(); + + // because the trip "crosses" midnight and we are already on the next day, we infer the service date to be + // yesterday + var inferredDate = RealtimeVehiclePatternMatcher.inferServiceDate(null, zoneId, time); + + assertEquals(LocalDate.parse("2022-04-04"), inferredDate); + } + private static TripPattern tripPattern(Trip trip, List stopTimes) { var stopPattern = new StopPattern(stopTimes); var pattern = TripPattern.of(trip.getId())