aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorTeddy Wing2017-05-30 15:14:45 +0200
committerTeddy Wing2017-05-31 17:57:36 +0200
commit43c14bc8c916d00a9c478f1e78d5860977c56bbe (patch)
tree7d65e70711c10bfc75303bb963a130f7b2cb5d9f /app
parentc6d622d662d7f787787d8ee3d89f5d900dd8ca59 (diff)
downloadchouette-core-43c14bc8c916d00a9c478f1e78d5860977c56bbe.tar.bz2
VehicleJourneyAtStopsDayOffset: Calculate day offsets
Add a rough implementation that determines the day offsets of a sequence of VehicleJourneyAtStops. It currently passes the first (and only test) associated with it. My first go at it assumed that the dates meant something, but really they're just there for decoration. All the dates will be on '2000-01-01' regardless of their day offset. This is because they need to be stored as dates, so the application has standardised on a single placeholder date. That go didn't work because the after-midnight times were still on '2000-01-01'. The new version bases the day offset on the times. It does this by checking whether the current time is earlier in the day than a previous time. If it is, we assume that the day has changed. This works when the gap between times is never 24 hours or more. It will not work if, for example, we have a "current day" time at 10:00 and a "next day" time at 10:00. We have no way to infer that they're on different days. This implementation appears to work, at least for the case the test is checking, but I don't like it because it doesn't read well. Hoping to change it in addition to adding my other test cases. Refs #3596
Diffstat (limited to 'app')
-rw-r--r--app/models/chouette/vehicle_journey_at_stops_day_offset.rb48
1 files changed, 47 insertions, 1 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb
index 3825fa263..490b22499 100644
--- a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb
+++ b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb
@@ -7,7 +7,53 @@ module Chouette
# def update
def calculate!
- @at_stops
+ start_date = @at_stops.first.arrival_time.to_date
+ offset = 0
+
+ arrival_offset = 0
+ departure_offset = 0
+
+ # @at_stops.each do |at_stop|
+ # if at_stop.arrival_time.to_date != start_date
+ # offset = at_stop.arrival_time.to_date - start_date
+ # end
+ #
+ # if at_stop.departure_time.to_date != start_date
+ # offset = at_stop.departure_time.to_date - start_date
+ # end
+ #
+ # at_stop.arrival_day_offset = offset
+ # at_stop.departure_day_offset = offset
+ # end
+
+ # Check that the current time is later than the previous time.
+ # If earlier, it's a new day offset
+ # Otherwise, we keep the current day offset
+
+ # TODO: Don't like this
+ previous_at_stop = @at_stops.first
+
+ @at_stops.each_with_index do |at_stop, index|
+ next if index == 0
+
+ if previous_at_stop.departure_time > at_stop.arrival_time ||
+ at_stop.arrival_time < previous_at_stop.arrival_time
+ arrival_offset += 1
+ end
+
+ # Departure time is earlier than arrival time
+ # or
+ # Departure time is earlier than previous departure time
+ if at_stop.departure_time < at_stop.arrival_time ||
+ at_stop.departure_time < previous_at_stop.departure_time
+ departure_offset += 1
+ end
+
+ at_stop.arrival_day_offset = arrival_offset
+ at_stop.departure_day_offset = departure_offset
+
+ previous_at_stop = at_stop
+ end
end
end
end