diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java index e161a88aa..c31340863 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java @@ -1,6 +1,6 @@ package org.opentripplanner.middleware.triptracker; -import java.time.ZonedDateTime; +import java.time.Instant; import java.util.Date; /** @@ -39,9 +39,7 @@ public TrackingLocation(Double lat, Double lon, Date timestamp) { } /** Used in testing **/ - public TrackingLocation(String dateTime, double lat, double lon) { - this.timestamp = new Date(ZonedDateTime.parse(dateTime).toInstant().toEpochMilli()); - this.lat = lat; - this.lon = lon; + public TrackingLocation(Instant instant, double lat, double lon) { + this(lat, lon, new Date(instant.toEpochMilli())); } } diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java index 30f4e9526..e9d27a2d7 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java @@ -24,9 +24,7 @@ import java.io.IOException; import java.time.Instant; -import java.time.ZoneId; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; @@ -68,9 +66,9 @@ public static void setUp() throws IOException { @ParameterizedTest @MethodSource("createTrace") - void canTrackTrip(String time, double lat, double lon, TripStatus expected, String message) { + void canTrackTrip(Instant instant, double lat, double lon, TripStatus expected, String message) { TrackedJourney trackedJourney = new TrackedJourney(); - TrackingLocation trackingLocation = new TrackingLocation(time, lat, lon); + TrackingLocation trackingLocation = new TrackingLocation(instant, lat, lon); trackedJourney.locations = List.of(trackingLocation); TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, busStopToJusticeCenterItinerary); TripStatus tripStatus = TripStatus.getTripStatus(travelerPosition); @@ -93,51 +91,53 @@ private static Stream createTrace() { 1000, calculateBearing(current.start, after.start) ); + Instant startInstant = startTime.toInstant(); + long currentSegmentStartOffsetSecs = (long) Math.floor(current.cumulativeTime - current.timeInSegment); return Stream.of( Arguments.of( - getDateTimeAsString(startTime, before.cumulativeTime - before.timeInSegment), + startInstant.plusSeconds((long) Math.floor(before.cumulativeTime - before.timeInSegment)), current.start.lat, current.start.lon, TripStatus.AHEAD_OF_SCHEDULE, "For the current location and time the traveler is ahead of schedule." ), Arguments.of( - getDateTimeAsString(startTime, current.cumulativeTime - current.timeInSegment), + startInstant.plusSeconds(currentSegmentStartOffsetSecs), current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time the traveler is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, after.cumulativeTime), + startInstant.plusSeconds((long) Math.floor(after.cumulativeTime)), current.start.lat, current.start.lon, TripStatus.BEHIND_SCHEDULE, "For the current location and time the traveler is behind schedule." ), Arguments.of( - getDateTimeAsString(startTime, (current.cumulativeTime - current.timeInSegment) - 10), + startInstant.plusSeconds(currentSegmentStartOffsetSecs - 10), current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time (with a slight deviation) the traveler is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, current.cumulativeTime), + startInstant.plusSeconds((long) Math.floor(current.cumulativeTime)), current.start.lat + 0.00001, current.start.lon + 0.00001, TripStatus.ON_SCHEDULE, "The current location, with a slight deviation, is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, 0), + startInstant, notOnTripCoordinates.lat, notOnTripCoordinates.lon, TripStatus.DEVIATED, "Arbitrary lat/lon values which aren't on the trip leg." ), Arguments.of( - getDateTimeAsString(busStopToJusticeCenterItinerary.endTime, 1), + busStopToJusticeCenterItinerary.endTime.toInstant().plusSeconds(1), deviatedCoordinates.lat, deviatedCoordinates.lon, TripStatus.DEVIATED, @@ -422,12 +422,6 @@ private static List createSegmentsForLeg() { return interpolatePoints(busStopToJusticeCenterItinerary.legs.get(0)); } - private static String getDateTimeAsString(Date date, double offset) { - Instant dateTime = date.toInstant().plusSeconds((long) offset); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.systemDefault());; - return formatter.format(dateTime); - } - private int getNumberOfExcludedPoints(List legPositions, Leg leg) { int excluded = 0; for (Position position : legPositions) {