From b7d4892741f5b98a45a6a7660e6f174106b02dd7 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Tue, 3 Apr 2018 14:58:47 +0200 Subject: Add GTFS::Time and use it to compute day offset for VehicleJourneys. Refs #6368 --- lib/gtfs/time.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/gtfs/time.rb (limited to 'lib') 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 -- cgit v1.2.3