@@ -7,7 +7,7 @@ const zlib = require('zlib');
77const gtfsrt = require ( 'gtfs-realtime-bindings' ) . transit_realtime ;
88const JSONStream = require ( 'JSONStream' ) ;
99const N3 = require ( 'n3' ) ;
10- const { format, addHours, addMinutes, addSeconds } = require ( 'date-fns' ) ;
10+ const { format, addHours, addMinutes, addSeconds, addDays } = require ( 'date-fns' ) ;
1111const Connections2JSONLD = require ( './Connections2JSONLD' ) ;
1212const Connections2CSV = require ( './Connections2CSV' ) ;
1313const Connections2Triples = require ( './Connections2Triples' ) ;
@@ -40,7 +40,7 @@ class Gtfsrt2LC {
4040 "routeLabel" : "routes.route_long_name.replace(/\\s/gi, '');" ,
4141 "tripLabel" : "routes.route_short_name + routes.route_id;" ,
4242 "tripStartTime" : "format(trips.startTime, \"yyyyMMdd'T'HHmm\");"
43- }
43+ }
4444 } ;
4545 }
4646
@@ -79,8 +79,7 @@ class Gtfsrt2LC {
7979 if ( entity . tripUpdate ) {
8080 let tripUpdate = entity . tripUpdate ;
8181 let tripId = tripUpdate . trip . tripId ;
82- let startDate = tripUpdate . trip . startDate ;
83- let serviceDay = new Date ( startDate . substr ( 0 , 4 ) , parseInt ( startDate . substr ( 4 , 2 ) ) - 1 , startDate . substr ( 6 , 2 ) ) ;
82+
8483 const timestamp = tripUpdate . timestamp || this . jsonData . header . timestamp ;
8584
8685 // Check if tripId is directly provided or has to be found from GTFS source,
@@ -89,11 +88,6 @@ class Gtfsrt2LC {
8988 let deduced = await this . deduceTripId ( tripUpdate . trip . routeId , tripUpdate . trip . startTime ,
9089 tripUpdate . trip . startDate , tripUpdate . trip . directionId ) ;
9190 tripId = deduced ? deduced [ 'trip_id' ] : null ;
92- // Correct startTime by adding 24 hours (error noticed for HSL Helsinki GTFS-RT)
93- if ( deduced && deduced [ 'startTime' ] ) {
94- tripUpdate . trip . startTime = deduced [ 'startTime' ] ;
95- this . correctTimes ( tripUpdate ) ;
96- }
9791 }
9892
9993 let r = null ;
@@ -115,11 +109,35 @@ class Gtfsrt2LC {
115109 return ;
116110 }
117111
112+ // Figure service date and trip start time
113+ let serviceDay = null ;
118114 let tripStartTime = null ;
119- if ( tripUpdate . trip . startTime ) {
120- tripStartTime = this . addDuration ( serviceDay , this . parseGTFSDuration ( tripUpdate . trip . startTime ) ) ;
115+
116+ if ( tripUpdate . trip . startDate ) {
117+ const rawStartDate = tripUpdate . trip . startDate ;
118+ serviceDay = new Date (
119+ rawStartDate . substr ( 0 , 4 ) ,
120+ parseInt ( rawStartDate . substr ( 4 , 2 ) ) - 1 ,
121+ rawStartDate . substr ( 6 , 2 )
122+ ) ;
123+ if ( tripUpdate . trip . startTime ) {
124+ tripStartTime = this . addDuration ( serviceDay , this . parseGTFSDuration ( tripUpdate . trip . startTime ) ) ;
125+ } else {
126+ // Extract from static data
127+ tripStartTime = this . addDuration (
128+ serviceDay ,
129+ this . parseGTFSDuration ( ( await this . getStopTimes ( tripId ) ) [ 0 ] [ 'departure_time' ] )
130+ ) ;
131+ }
121132 } else {
122- tripStartTime = this . addDuration ( serviceDay , this . parseGTFSDuration ( ( await this . getStopTimes ( tripId ) ) [ 0 ] [ 'departure_time' ] ) ) ;
133+ // Extract from static data
134+ const serviceObj = this . calendar . get ( t [ 'service_id' ] ) ;
135+ const depTime = ( await this . getStopTimes ( tripId ) ) [ 0 ] [ 'departure_time' ] ;
136+ serviceDay = this . findTripStartDate ( depTime , serviceObj ) ;
137+ tripStartTime = this . addDuration (
138+ serviceDay ,
139+ this . parseGTFSDuration ( ( await this . getStopTimes ( tripId ) ) [ 0 ] [ 'departure_time' ] )
140+ ) ;
123141 }
124142
125143 // Check if the trip has been canceled or not
@@ -179,7 +197,7 @@ class Gtfsrt2LC {
179197
180198 // Add start time to trip object
181199 t . startTime = tripStartTime ;
182-
200+
183201 // Build JSON Connection
184202 let json_connection = {
185203 type,
@@ -342,7 +360,7 @@ class Gtfsrt2LC {
342360 // Find the trip that runs on the same day
343361 let today = new Date ( startDate . substring ( 0 , 4 ) , parseInt ( startDate . substring ( 4 , 6 ) ) - 1 , startDate . substring ( 6 , 8 ) ) ;
344362 for ( let i = 0 ; i < trips . length ; i ++ ) {
345- const service = this . calendar . get ( trips [ i ] [ 'service_id' ] ) ;
363+ const service = await this . calendar . get ( trips [ i ] [ 'service_id' ] ) ;
346364 const exceptions = this . calendarDates . get ( trips [ i ] [ 'service_id' ] ) || { } ;
347365 const minDate = new Date ( service [ 'start_date' ] . substring ( 0 , 4 ) , parseInt ( service [ 'start_date' ] . substring ( 4 , 6 ) ) - 1 , service [ 'start_date' ] . substring ( 6 , 8 ) ) ;
348366 const maxDate = new Date ( service [ 'end_date' ] . substring ( 0 , 4 ) , parseInt ( service [ 'end_date' ] . substring ( 4 , 6 ) ) - 1 , service [ 'end_date' ] . substring ( 6 , 8 ) ) ;
@@ -370,12 +388,34 @@ class Gtfsrt2LC {
370388 }
371389 }
372390
373- correctTimes ( tripUpdate ) {
374- for ( let i = 0 ; i < tripUpdate . stopTimeUpdate . length ; i ++ ) {
375- let dep = parseInt ( tripUpdate . stopTimeUpdate [ i ] . departure . time ) + 86400 ;
376- let arr = parseInt ( tripUpdate . stopTimeUpdate [ i ] . arrival . time ) + 86400 ;
377- tripUpdate . stopTimeUpdate [ i ] . departure . time = dep ;
378- tripUpdate . stopTimeUpdate [ i ] . arrival . time = arr ;
391+ findTripStartDate ( depTime , service ) {
392+ const now = new Date ( ) ;
393+ const days = [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' , 'saturday' , 'sunday' ] ;
394+ const today = format ( now , 'EEEE' ) . toLowerCase ( ) ;
395+ const tomorrow = days [ days . indexOf ( today ) > 5 ? 0 : days . indexOf ( today ) + 1 ] ;
396+ const yesterday = days [ days . indexOf ( today ) < 1 ? 6 : days . indexOf ( today ) - 1 ] ;
397+
398+ const todayServiceDate = this . addDuration (
399+ new Date ( now . getUTCFullYear ( ) , now . getUTCMonth ( ) , now . getUTCDate ( ) ) ,
400+ this . parseGTFSDuration ( depTime )
401+ ) ;
402+ const tomorrowServiceDate = addDays ( todayServiceDate , 1 ) ;
403+ const yesterdayServiceDate = addDays ( todayServiceDate , - 1 ) ;
404+
405+ const todayDistance = service [ today ] === '1' ? Math . abs ( now - todayServiceDate ) : Number . POSITIVE_INFINITY ;
406+ const tomorrowDistance = service [ tomorrow ] === '1' ? Math . abs ( now - tomorrowServiceDate ) : Number . POSITIVE_INFINITY ;
407+ const yesterdayDistance = service [ yesterday ] === '1' ? Math . abs ( now - yesterdayServiceDate ) : Number . POSITIVE_INFINITY ;
408+
409+ if ( todayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
410+ return todayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
411+ }
412+
413+ if ( tomorrowDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
414+ return tomorrowServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
415+ }
416+
417+ if ( yesterdayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
418+ return yesterdayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
379419 }
380420 }
381421
@@ -785,6 +825,10 @@ class Gtfsrt2LC {
785825 return this . _stops ;
786826 }
787827
828+ set stops ( stops ) {
829+ this . _stops = stops ;
830+ }
831+
788832 get stop_times ( ) {
789833 return this . _stop_times ;
790834 }
0 commit comments