public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HAFAS bug results in wrong timestamps

yu-re-ka opened this issue · comments

const createClient = require('hafas-client');
const dbProfile = require('hafas-client/p/db');
const hafas = createClient(dbProfile, 'trainsearch');

const printTimestamps = journeys => {
    for (let journey of journeys)
        for (let leg of journey.legs)
            if (leg.walking) console.log(leg.departure, leg.arrival);
};

(async () => {
    const origData = await hafas.journeys("800001", "800002", {results: 10});
    console.log("original");
    printWalkingTimestamps(origData.journeys);

    const refreshed = await Promise.all(origData.journeys.map(x => hafas.refreshJourney(x.refreshToken)));
    console.log("\nafter refresh");
    printWalkingTimestamps(refreshed);
}) ()
$ node bug.js
original
2021-08-15T04:20:00+02:00 2021-08-15T04:29:00+02:00
2021-08-15T07:31:00+02:00 2021-08-15T07:34:00+02:00
2021-08-15T06:26:00+02:00 2021-08-15T06:35:00+02:00
2021-08-15T09:56:00+02:00 2021-08-15T09:59:00+02:00
2021-08-15T06:45:00+02:00 2021-08-15T06:55:00+02:00
2021-08-15T09:56:00+02:00 2021-08-15T09:59:00+02:00
2021-08-15T08:25:00+02:00 2021-08-15T08:34:00+02:00
2021-08-15T11:56:00+02:00 2021-08-15T11:59:00+02:00
2021-08-15T08:45:00+02:00 2021-08-15T08:55:00+02:00
2021-08-15T11:56:00+02:00 2021-08-15T11:59:00+02:00
2021-08-15T10:25:00+02:00 2021-08-15T10:34:00+02:00
2021-08-15T13:56:00+02:00 2021-08-15T13:59:00+02:00

after refresh
2021-08-15T04:19:00Z 2021-08-15T04:29:00+02:00
2021-08-15T07:31:00+02:00 2021-08-15T07:34:00+02:00
2021-08-15T06:25:00Z 2021-08-15T06:35:00+02:00
2021-08-15T09:56:00+02:00 2021-08-15T09:59:00+02:00
2021-08-15T06:45:00+02:00 2021-08-15T06:55:00+02:00
2021-08-15T09:56:00+02:00 2021-08-15T09:59:00+02:00
2021-08-15T08:24:00Z 2021-08-15T08:34:00+02:00
2021-08-15T11:56:00+02:00 2021-08-15T11:59:00+02:00
2021-08-15T08:45:00+02:00 2021-08-15T08:55:00+02:00
2021-08-15T11:56:00+02:00 2021-08-15T11:59:00+02:00
2021-08-15T10:24:00Z 2021-08-15T10:34:00+02:00
2021-08-15T13:56:00+02:00 2021-08-15T13:59:00+02:00

Note that some of the departure times are now in UTC timezone, and if they are parsed accordingly the arrival is before the departure.

screenshot2

I investigated it a bit more and for these legs dep.dTZOffset is actually set to 0, so it is yet another bug in HAFAS 😅

My really really ugly quick-fix looks like this:

diff --git a/parse/journey-leg.js b/parse/journey-leg.js
index 7035686..95ddbc7 100644
--- a/parse/journey-leg.js
+++ b/parse/journey-leg.js
@@ -59,6 +59,11 @@ const parseJourneyLeg = (ctx, pt, date) => { // pt = raw leg
                destination: clone(pt.arr.location)
        }

+       if (pt.type === 'WALK' && pt.dep.dTZOffset != pt.arr.aTZOffset) {
+               if (pt.dep.dTZOffset == 0) pt.dep.dTZOffset = pt.arr.aTZOffset;
+               if (pt.arr.aTZOffset == 0) pt.arr.aTZOffset = pt.dep.dTZOffset;
+       }
+
        const dep = profile.parseWhen(ctx, date, pt.dep.dTimeS, pt.dep.dTimeR, pt.dep.dTZOffset, pt.dep.dCncl)
        res.departure = dep.when
        res.plannedDeparture = dep.plannedWhen

Now it looks like it should:

screenshot1

Thanks for this excellent bug report!

My really really ugly quick-fix looks like this:

if (pt.type === 'WALK' && pt.dep.dTZOffset != pt.arr.aTZOffset) {
	if (pt.dep.dTZOffset == 0) pt.dep.dTZOffset = pt.arr.aTZOffset;
	if (pt.arr.aTZOffset == 0) pt.arr.aTZOffset = pt.dep.dTZOffset;
}

Do you know if the bug only appears with walking legs that are the first leg of their journey? If that's the case, we could make the workaround more specific to reduce the chance of breaking valid cases.

I think it appears on walking legs, which are the first or last leg of a journey

Do you have an example where the last leg has it? I can only find examples for the first leg.

It would be great to have the raw response, in order to use it as a test. You can get the raw response using the DEBUG=hafas-client environment variable.

Do you want to be mentioned as a contributor? If yes, how? Yuka <github@yuka.dev>?

Do you want to be mentioned as a contributor? If yes, how?

Sure: Yureka <github@yuka.dev>

TZ-bug-last-leg.ndjson.txt
TZ-bug-first-leg.ndjson.txt