Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,18 @@ public void clearOldTrips() {

static Stream<Arguments> 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 hasStopTime, String expectedDate) {
var trip = TimetableRepositoryForTest.trip(tripId).build();

var sixOclock = (int) Duration.ofHours(18).toSeconds();
Expand All @@ -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);
}
Expand All @@ -373,6 +380,17 @@ void inferServiceDateCloseToMidnight() {
assertEquals(LocalDate.parse("2022-04-04"), inferredDate);
}

Copy link
Contributor

@miklcct miklcct Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add a test case where the vehicle position is given in the morning after daytime routes have typically started, resolving to today rather than yesterday?

@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<StopTime> stopTimes) {
var stopPattern = new StopPattern(stopTimes);
var pattern = TripPattern.of(trip.getId())
Expand Down
Loading