@@ -112,7 +112,7 @@ class Gtfsrt2LC {
112112 // Figure service date and trip start time
113113 let serviceDay = null ;
114114 let tripStartTime = null ;
115-
115+
116116 if ( tripUpdate . trip . startDate ) {
117117 const rawStartDate = tripUpdate . trip . startDate ;
118118 serviceDay = new Date (
@@ -401,21 +401,21 @@ class Gtfsrt2LC {
401401 ) ;
402402 const tomorrowServiceDate = addDays ( todayServiceDate , 1 ) ;
403403 const yesterdayServiceDate = addDays ( todayServiceDate , - 1 ) ;
404-
404+
405405 const todayDistance = service [ today ] === '1' ? Math . abs ( now - todayServiceDate ) : Number . POSITIVE_INFINITY ;
406406 const tomorrowDistance = service [ tomorrow ] === '1' ? Math . abs ( now - tomorrowServiceDate ) : Number . POSITIVE_INFINITY ;
407407 const yesterdayDistance = service [ yesterday ] === '1' ? Math . abs ( now - yesterdayServiceDate ) : Number . POSITIVE_INFINITY ;
408408
409- if ( todayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
410- return todayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
409+ if ( todayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
410+ return todayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
411411 }
412412
413- if ( tomorrowDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
414- return tomorrowServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
413+ if ( tomorrowDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
414+ return tomorrowServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
415415 }
416416
417- if ( yesterdayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
418- return yesterdayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
417+ if ( yesterdayDistance === Math . min ( todayDistance , tomorrowDistance , yesterdayDistance ) ) {
418+ return yesterdayServiceDate . setUTCHours ( 0 , 0 , 0 , 0 ) ;
419419 }
420420 }
421421
@@ -557,6 +557,21 @@ class Gtfsrt2LC {
557557 update [ 'departure' ] [ 'time' ] = ( this . addDuration ( serviceDay , this . parseGTFSDuration ( staticData [ 'departure_time' ] ) ) . getTime ( ) / 1000 ) + update [ 'departure' ] [ 'delay' ] ;
558558 }
559559 }
560+ } else {
561+ // If this stop is not the last of the trip and the stop update is missing departure info
562+ // add it manually taking into account the arrival delay at this stop
563+ if ( staticIndex < staticLength - 1 && update [ 'arrival' ] ) {
564+ update [ 'departure' ] = {
565+ 'delay' : update [ 'arrival' ] [ 'delay' ] | 0 ,
566+ 'time' : ( this . addDuration ( serviceDay , this . parseGTFSDuration ( staticData [ 'departure_time' ] ) ) . getTime ( ) / 1000 ) + update [ 'arrival' ] [ 'delay' ]
567+ }
568+ } else {
569+ // Fallback to static data
570+ update [ 'departure' ] = {
571+ 'delay' : 0 ,
572+ 'time' : ( this . addDuration ( serviceDay , this . parseGTFSDuration ( staticData [ 'departure_time' ] ) ) . getTime ( ) / 1000 )
573+ }
574+ }
560575 }
561576
562577 // Check if arrival time is explicitly defined. In some cases only the delay is given
@@ -570,23 +585,10 @@ class Gtfsrt2LC {
570585 update [ 'arrival' ] [ 'time' ] = ( this . addDuration ( serviceDay , this . parseGTFSDuration ( staticData [ 'arrival_time' ] ) ) . getTime ( ) / 1000 ) + update [ 'arrival' ] [ 'delay' ] ;
571586 }
572587 }
573- }
574-
575- // If this stop is not the last of the trip and the stop update is missing departure info
576- // add it manually taking into account the arrival delay at this stop
577- if ( staticIndex != staticLength - 1 ) {
578- if ( ! update [ 'departure' ] ) {
579- update [ 'departure' ] = {
580- 'delay' : update [ 'arrival' ] [ 'delay' ] ,
581- 'time' : ( this . addDuration ( serviceDay , this . parseGTFSDuration ( staticData [ 'departure_time' ] ) ) . getTime ( ) / 1000 ) + update [ 'arrival' ] [ 'delay' ]
582- }
583- }
584- }
585-
586- // If the stop update is missing arrival info and is not the first stop of the trip
587- // add it manually taking into account the departure delay of the previous stop (if any)
588- if ( staticIndex != 0 ) {
589- if ( ! update [ 'arrival' ] && prevUpdate ) {
588+ } else {
589+ // If the stop update is missing arrival info and is not the first stop of the trip
590+ // add it manually taking into account the departure delay of the previous stop (if any)
591+ if ( staticIndex > 0 && prevUpdate ) {
590592 // We need to make sure that adding the departure delay of the previous stop
591593 // to the arrival time of this stop won't cause inconsistent times,
592594 // i.e. arrival > departure.
@@ -625,10 +627,10 @@ class Gtfsrt2LC {
625627
626628 }
627629 }
628- }
630+ } else { /* This should never happen */ }
629631 }
630632
631- // Check for consistencies between this update and the previous
633+ // Check for inconsistencies between this update and the previous
632634 if ( prevUpdate && update [ 'departure' ] && prevUpdate [ 'departure' ] [ 'time' ] > update [ 'arrival' ] [ 'time' ] ) {
633635 // Enforce previous delay on this update to keep consistency
634636 let prevDepDelay = prevUpdate ? prevUpdate [ 'departure' ] [ 'delay' ] : 0 ;
@@ -650,6 +652,8 @@ class Gtfsrt2LC {
650652 }
651653 } catch ( err ) {
652654 console . error ( err ) ;
655+ console . error ( 'Issue encountered while processing this update: ' , JSON . stringify ( update , null , 3 ) ) ;
656+ console . error ( 'From this trip: ' , JSON . stringify ( staticData , null , 3 ) ) ;
653657 }
654658
655659 return update ;
0 commit comments