diff options
| author | Alban Peignier | 2018-04-03 14:58:47 +0200 | 
|---|---|---|
| committer | Alban Peignier | 2018-04-03 14:58:47 +0200 | 
| commit | b7d4892741f5b98a45a6a7660e6f174106b02dd7 (patch) | |
| tree | e8c88207833b5bffad630b34c9abab25a64c9c57 | |
| parent | ee0b5b354445f23940e6a17a8d94f3d65a0b8f7d (diff) | |
| download | chouette-core-b7d4892741f5b98a45a6a7660e6f174106b02dd7.tar.bz2 | |
Add GTFS::Time and use it to compute day offset for VehicleJourneys. Refs #6368
| -rw-r--r-- | app/models/import/gtfs.rb | 10 | ||||
| -rw-r--r-- | lib/gtfs/time.rb | 28 | ||||
| -rw-r--r-- | spec/lib/gtfs/time_spec.rb | 27 | 
3 files changed, 63 insertions, 2 deletions
| diff --git a/app/models/import/gtfs.rb b/app/models/import/gtfs.rb index bc737c524..70f448132 100644 --- a/app/models/import/gtfs.rb +++ b/app/models/import/gtfs.rb @@ -225,8 +225,14 @@ class Import::Gtfs < Import::Base              # JourneyPattern#vjas_add creates automaticaly VehicleJourneyAtStop              vehicle_journey_at_stop = journey_pattern.vehicle_journey_at_stops.find_by(stop_point_id: stop_point.id) -            vehicle_journey_at_stop.departure_time = stop_time.departure_time -            vehicle_journey_at_stop.arrival_time = stop_time.arrival_time + +            departure_time = GTFS::Time.parse(stop_time.departure_time) +            arrival_time = GTFS::Time.parse(stop_time.arrival_time) + +            vehicle_journey_at_stop.departure_time = departure_time.time +            vehicle_journey_at_stop.arrival_time = arrival_time.time +            vehicle_journey_at_stop.departure_day_offset = departure_time.day_offset +            vehicle_journey_at_stop.arrival_day_offset = arrival_time.day_offset              # TODO offset diff --git a/lib/gtfs/time.rb b/lib/gtfs/time.rb new file mode 100644 index 000000000..49546532a --- /dev/null +++ b/lib/gtfs/time.rb @@ -0,0 +1,28 @@ +module GTFS +  class Time +    attr_reader :hours, :minutes, :seconds +    def initialize(hours, minutes, seconds) +      @hours, @minutes, @seconds = hours, minutes, seconds +    end + +    def real_hours +      hours.modulo(24) +    end + +    def time +      @time ||= ::Time.new(2000, 1, 1, real_hours, minutes, seconds, "+00:00") +    end + +    def day_offset +      hours / 24 +    end + +    FORMAT = /(\d{1,2}):(\d{2}):(\d{2})/ + +    def self.parse(definition) +      if definition.to_s =~ FORMAT +        new *[$1, $2, $3].map(&:to_i) +      end +    end +  end +end diff --git a/spec/lib/gtfs/time_spec.rb b/spec/lib/gtfs/time_spec.rb new file mode 100644 index 000000000..540d7cc79 --- /dev/null +++ b/spec/lib/gtfs/time_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe GTFS::Time do + +  it "returns an UTC Time with given H:M:S" do +    expect(GTFS::Time.parse("14:29:00").time).to eq(Time.parse("2000-01-01 14:29:00 +00")) +  end + +  it "support hours with a single number" do +    expect(GTFS::Time.parse("4:29:00").time).to eq(Time.parse("2000-01-01 04:29:00 +00")) +  end + +  it "return nil for invalid format" do +    expect(GTFS::Time.parse("abc")).to be_nil +  end + +  it "removes 24 hours after 23:59:59" do +    expect(GTFS::Time.parse("25:29:00").time).to eq(Time.parse("2000-01-01 01:29:00 +00")) +  end + +  it "returns a day_offset for each 24 hours turn" do +    expect(GTFS::Time.parse("10:00:00").day_offset).to eq(0) +    expect(GTFS::Time.parse("30:00:00").day_offset).to eq(1) +    expect(GTFS::Time.parse("50:00:00").day_offset).to eq(2) +  end + +end | 
