public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.
ISC License
277 stars 53 forks source link

wrong date parsed for next-day DEVI legs on overnight journeys #301

Closed soerface closed 10 months ago

soerface commented 1 year ago

When a journey starts in the evening, and one leg of the journey is completely in the next day, this leg gets assigned the date of the departure day, not the arrival day.

This results in applications like bahn.guru reporting a negative travel time, since according to hafas-client you arrive on the same day early in the morning, instead of the next day early in the morning: https://github.com/juliuste/bahn.guru/issues/35

I'm not yet sure how to fix this, but I've successfully reproduced it with a test. I'll therefore open a PR which showcases the problem. Not sure if and when I'm able to fix this, so feel free to take over if you already know how to fix it.

derhuerst commented 11 months ago

Thanks for reporting this subtle behaviour! In #302, I have pushed another test case that I have obtained from the DB HAFAS API.

It seems to be a HAFAS gotcha or bug that doesn't occur with DEVI legs. Not sure about other leg types, though.

Compare the DEVI leg from your test case (now named db-journey-overnight-0.json):

{
    "type": "DEVI",
    "dep": {
        "locX": 3,
        "dTimeS": "012200",
        "type": "N",
        "location": {
            "type": "stop",
            "id": "8000147",
            "name": "Hamburg-Harburg",
            "location": {
                "type": "location",
                "id": "8000147",
                "latitude": 0,
                "longitude": 0
            },
            "products": {
                "nationalExpress": true,
                "national": true,
                "regionalExpress": true,
                "regional": true,
                "suburban": true,
                "bus": true,
                "ferry": false,
                "subway": false,
                "tram": false,
                "taxi": false,
            },
        },
    },
    "arr": {
        "locX": 2,
        "aTimeS": "014200",
        "type": "N",
        "location": {
            "type": "stop",
            "id": "8002549",
            "name": "Hamburg Hbf",
            "location": {
                "type": "location",
                "id": "8002549",
                "latitude": 0,
                "longitude": 0
            },
            "products": {
                "nationalExpress": true,
                "national": true,
                "regionalExpress": true,
                "regional": true,
                "suburban": true,
                "bus": true,
                "ferry": false,
                "subway": true,
                "tram": false,
                "taxi": false,
            },
        },
    },
    "gis": {
        "ctx": "H|1|D$A=1@O=Hamburg-Harburg@L=8000147@a=0@$A=1@O=Hamburg Hbf@L=8002549@a=0@$202311140122$202311140142$$$1$$$$$$|",
        "gisPrvr": "H",
        "getDescr": false,
        "getPoly": false,
        "sumLDrawStyleX": 0,
        "resLDrawStyleX": 1,
        "prodX": 0,
        "line": {
            "type": "line",
            "id": "deviation",
            "fahrtNr": null,
            "name": "Deviation",
            "public": true,
        },
    },
    "resState": "N",
    "resRecommendation": "N",
}

with a JNY (public transportation) leg from my test case (db-journey-overnight-1.json), where both dep.dTime*/arr.aTime* and jny.trainStartDate correctly indicate that the leg happens on the next day:

{
    "type": "JNY",
    "dep": {
        "locX": 2,
        "idx": 1,
        "dProdX": 2,
        "dPltfS": {
            "type": "PL",
            "txt": "1",
        },
        "dTimeS": "01043000",
        "dTZOffset": 60,
        "msgL": [
            // …
        ],
        "type": "N",
    },
    "arr": {
        "locX": 4,
        "idx": 9,
        "aProdX": 2,
        "aPltfS": {
            "type": "PL",
            "txt": "19",
        },
        "aTimeS": "01091700",
        "aProgType": "PROGNOSED",
        "aTZOffset": 60,
        "msgL": [
            // …
        ],
        "type": "N",
    },
    "jny": {
        "jid": "1|198958|0|80|25112023",
        "prodX": 2,
        "dirTxt": "München Hbf",
        "dirFlg": "1",
        "status": "P",
        "isRchbl": true,
        "ctxRecon": "T$A=1@O=Berlin Hbf (tief)@L=8098160@a=128@$A=1@O=München Hbf@L=8000261@a=128@$202311250430$202311250917$ICE  501$$1$$$$$$",
        "msgL": [
            // …
        ],
        "subscr": "F",
        "prodL": [
            {
                "prodX": 2,
                "fLocX": 2,
                "tLocX": 4,
                "fIdx": 1,
                "tIdx": 9,
            },
        ],
        "dTrnCmpSX": {
            "tcocX": [0, 1],
        },
        "sumLDrawStyleX": 2,
        "resLDrawStyleX": 1,
        "trainStartDate": "20231125",
        "durS": "044700",
    },
    "resState": "N",
    "resRecommendation": "N",
}
derhuerst commented 11 months ago

The lack of day offsets doesn't seem to occur with WALK legs either. I assume only DEVI is broken.

If we can find out what DEVI legs are, we can probably obtain more reproducing test cases. @marudor @schildbach @vkrause @alexander-albers @n0emis Do you know something about this?

marudor commented 11 months ago

DEVI Legs are Deviations. It happens if you ask for a route to a specific stopPlace but HAFAS returns one to another stopPlace. Happens mainly in the night when there is no route to you destination for the forseeable future. For Instance you want to go from Berlin Hbf to München Hbf. One of the only routes to München is to München Ost with a NJ at your time. HAFAS returns that NJ and the last leg is a DEVI that just says "you wanted München HBF, this route ends in München Ost"

Bahn.de for instance ignores this completly, the DEVI leg also has no real information.

derhuerst commented 11 months ago

Thanks for the explanation!

I have pushed a first attempt at a workaround in https://github.com/public-transport/hafas-client/commit/6e0e1f46d5ab74e13b0673bc0b77ba68ff2e3dc3 https://github.com/public-transport/hafas-client/pull/302/commits/0d2dc8f547cabc7fe47e274baa9a4def721738d1, but I haven't checked if it works for >1 real-world cases.