aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-02-26 12:07:22 +0100
committerAlban Peignier2018-03-14 15:32:39 +0100
commit8aa258ab7bea85e423ed98b4d864ba7ab5481c13 (patch)
tree4868a91d8139133f91e721d3fbc1383f490ffd08
parent8abcd5a71a29f987c20f513f622ade7eced82176 (diff)
downloadchouette-core-8aa258ab7bea85e423ed98b4d864ba7ab5481c13.tar.bz2
Refs #6021 @1h; Allow journeys to span over 3 days
And fix the way offsets are computed to use the timezones
-rw-r--r--app/models/chouette/vehicle_journey_at_stop.rb23
-rw-r--r--app/models/chouette/vehicle_journey_at_stops_day_offset.rb33
-rw-r--r--spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb71
3 files changed, 101 insertions, 26 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb
index d875442ee..9ea918ca5 100644
--- a/app/models/chouette/vehicle_journey_at_stop.rb
+++ b/app/models/chouette/vehicle_journey_at_stop.rb
@@ -4,7 +4,7 @@ module Chouette
include Chouette::ForAlightingEnumerations
include ChecksumSupport
- DAY_OFFSET_MAX = 1
+ DAY_OFFSET_MAX = 2
belongs_to :stop_point
@@ -82,12 +82,12 @@ module Chouette
format_time arrival_time.utc
end
- def departure_local_time
- local_time departure_time
+ def departure_local_time offset=nil
+ local_time departure_time, offset
end
- def arrival_local_time
- local_time arrival_time
+ def arrival_local_time offset=nil
+ local_time arrival_time, offset
end
def departure_local
@@ -98,12 +98,15 @@ module Chouette
format_time arrival_local_time
end
+ def time_zone_offset
+ return 0 unless stop_point&.stop_area&.time_zone.present?
+ ActiveSupport::TimeZone[stop_point.stop_area.time_zone]&.utc_offset || 0
+ end
+
private
- def local_time time
- return unless time
- return time unless stop_point&.stop_area&.time_zone.present?
- return time unless ActiveSupport::TimeZone[stop_point.stop_area.time_zone].present?
- time + ActiveSupport::TimeZone[stop_point.stop_area.time_zone].utc_offset
+ def local_time time, offset=nil
+ return nil unless time
+ time + (offset || time_zone_offset)
end
def format_time time
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 7497cd72c..cfa0e8bfc 100644
--- a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb
+++ b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb
@@ -4,31 +4,32 @@ module Chouette
@at_stops = at_stops
end
- def calculate!
- arrival_offset = 0
- departure_offset = 0
+ def time_from_fake_date fake_date
+ fake_date - fake_date.to_date.to_time
+ end
+ def calculate!
+ offset = 0
+ tz_offset = @at_stops.first&.time_zone_offset
@at_stops.inject(nil) do |prior_stop, stop|
next stop if prior_stop.nil?
# we only compare time of the day, not actual times
- stop_arrival_time = stop.arrival_time - stop.arrival_time.to_date.to_time
- stop_departure_time = stop.departure_time - stop.departure_time.to_date.to_time
- prior_stop_arrival_time = prior_stop.arrival_time - prior_stop.arrival_time.to_date.to_time
- prior_stop_departure_time = prior_stop.departure_time - prior_stop.departure_time.to_date.to_time
-
- if stop_arrival_time < prior_stop_departure_time ||
- stop_arrival_time < prior_stop_arrival_time
- arrival_offset += 1
+ stop_arrival_time = time_from_fake_date stop.arrival_local_time(tz_offset)
+ stop_departure_time = time_from_fake_date stop.departure_local_time(tz_offset)
+ prior_stop_departure_time = time_from_fake_date prior_stop.departure_local_time(tz_offset)
+
+ if stop_arrival_time < prior_stop_departure_time
+ offset += 1
end
- if stop_departure_time < stop_arrival_time ||
- stop_departure_time < prior_stop_departure_time
- departure_offset += 1
+ stop.arrival_day_offset = offset
+
+ if stop_departure_time < stop_arrival_time
+ offset += 1
end
- stop.arrival_day_offset = arrival_offset
- stop.departure_day_offset = departure_offset
+ stop.departure_day_offset = offset
stop
end
diff --git a/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb b/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb
index 69a2d5cb9..91cbf9097 100644
--- a/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb
+++ b/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb
@@ -86,5 +86,76 @@ describe Chouette::VehicleJourneyAtStop do
expect(at_stops[3].arrival_day_offset).to eq(2)
expect(at_stops[3].departure_day_offset).to eq(2)
end
+
+ context "with stops in a different timezone" do
+ before do
+ allow_any_instance_of(Chouette::VehicleJourneyAtStop).to receive(:local_time).and_wrap_original {|m, t| m.call(t - 12.hours)}
+ end
+
+ it "should apply the TZ" do
+ at_stops = []
+ [
+ ['22:30', '22:35'],
+ ['01:02', '01:14'],
+ ['12:02', '12:14'],
+ ].each do |arrival_time, departure_time|
+ at_stops << build_stubbed(
+ :vehicle_journey_at_stop,
+ arrival_time: arrival_time,
+ departure_time: departure_time
+ )
+ end
+ offsetter = Chouette::VehicleJourneyAtStopsDayOffset.new(at_stops)
+
+ offsetter.calculate!
+
+ expect(at_stops[0].arrival_day_offset).to eq(0)
+ expect(at_stops[0].departure_day_offset).to eq(0)
+
+ expect(at_stops[1].arrival_day_offset).to eq(0)
+ expect(at_stops[1].departure_day_offset).to eq(0)
+
+ expect(at_stops[2].arrival_day_offset).to eq(1)
+ expect(at_stops[2].departure_day_offset).to eq(1)
+ end
+ end
+
+ context "with stops in different timezones" do
+
+ it "should apply the TZ" do
+ at_stops = []
+
+ stop_area = create(:stop_area, time_zone: "Atlantic Time (Canada)")
+ stop_point = create(:stop_point, stop_area: stop_area)
+ vehicle_journey_at_stop = build_stubbed(
+ :vehicle_journey_at_stop,
+ stop_point: stop_point,
+ arrival_time: '09:00',
+ departure_time: '09:05'
+ )
+
+ at_stops << vehicle_journey_at_stop
+
+ stop_area = create(:stop_area, time_zone: "Paris")
+ stop_point = create(:stop_point, stop_area: stop_area)
+ vehicle_journey_at_stop = build_stubbed(
+ :vehicle_journey_at_stop,
+ stop_point: stop_point,
+ arrival_time: '05:00',
+ departure_time: '05:05'
+ )
+ at_stops << vehicle_journey_at_stop
+
+ offsetter = Chouette::VehicleJourneyAtStopsDayOffset.new(at_stops)
+
+ offsetter.calculate!
+
+ expect(at_stops[0].arrival_day_offset).to eq(0)
+ expect(at_stops[0].departure_day_offset).to eq(0)
+
+ expect(at_stops[1].arrival_day_offset).to eq(1)
+ expect(at_stops[1].departure_day_offset).to eq(1)
+ end
+ end
end
end