diff --git a/p/dbnav/index.js b/p/dbnav/index.js index 2e3331bf..130291fc 100644 --- a/p/dbnav/index.js +++ b/p/dbnav/index.js @@ -8,6 +8,7 @@ import {formatStopReq} from './stop-req.js'; import {formatNearbyReq} from './nearby-req.js'; import {formatStationBoardReq} from './station-board-req.js'; import {parseStop} from './parse-stop.js'; +import {parseJourney} from './parse-journey.js'; const profile = { ...baseProfile, @@ -24,6 +25,7 @@ const profile = { formatStationBoardReq, formatLocationFilter, + parseJourney, parseStop, }; diff --git a/p/dbnav/parse-journey.js b/p/dbnav/parse-journey.js new file mode 100644 index 00000000..ddc46d29 --- /dev/null +++ b/p/dbnav/parse-journey.js @@ -0,0 +1,34 @@ +import {parseJourney as parseJourneyDefault} from '../../parse/journey.js'; + +const parseJourney = (ctx, jj) => { + const legs = (jj.verbindung || jj).verbindungsAbschnitte; + if (legs.length > 0) { + legs[0] = preprocessJourneyLeg(legs[0]); + } + if (legs.length > 1) { + legs[legs.length - 1] = preprocessJourneyLeg(legs.at(-1)); + } + + return parseJourneyDefault(ctx, jj); +}; + +const preprocessJourneyLeg = (pt) => { // fixes https://github.com/public-transport/db-vendo-client/issues/24 + if (pt.typ === 'FUSSWEG' || pt.typ === 'TRANSFER') { + pt.ezAbgangsDatum = correctRealtimeTimeZone(pt.abgangsDatum, pt.ezAbgangsDatum); + pt.ezAnkunftsDatum = correctRealtimeTimeZone(pt.ankunftsDatum, pt.ezAnkunftsDatum); + } + + return pt; +}; + +const correctRealtimeTimeZone = (planned, realtime) => { + if (planned && realtime) { + const timeZoneOffsetRegex = /([+-]\d\d:\d\d|Z)$/; + const timeZoneOffsetPlanned = timeZoneOffsetRegex.exec(planned)[0]; + return realtime.replace(timeZoneOffsetRegex, timeZoneOffsetPlanned); + } + + return realtime; +}; + +export {parseJourney};