From 0d2dc8f547cabc7fe47e274baa9a4def721738d1 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 5 Dec 2023 15:01:35 +0100 Subject: [PATCH] =?UTF-8?q?fix=20date=20of=20next-day=20DEVI=20leg=20in=20?= =?UTF-8?q?an=20overnight=20journey=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit follow-up of a2870f6a follow-up of 6e6285c7 fixes #301 --- parse/date-time.js | 10 ++++++++-- parse/journey.js | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/parse/date-time.js b/parse/date-time.js index 4110b212a..623bae1ee 100644 --- a/parse/date-time.js +++ b/parse/date-time.js @@ -1,7 +1,13 @@ import {DateTime, FixedOffsetZone, IANAZone} from 'luxon' import {luxonIANAZonesByProfile as timezones} from '../lib/luxon-timezones.js' -const parseDateTime = ({profile}, date, time, tzOffset = null, timestamp = false) => { +const parseDaysOffset = (_, time) => { + return time.length > 6 ? parseInt(time.slice(0, -6)) : 0 +} + +const parseDateTime = (ctx, date, time, tzOffset = null, timestamp = false) => { + const {profile} = ctx + const pDate = [date.substr(-8, 4), date.substr(-4, 2), date.substr(-2, 2)] if (!pDate[0] || !pDate[1] || !pDate[2]) { throw new Error('invalid date format: ' + date) @@ -12,7 +18,7 @@ const parseDateTime = ({profile}, date, time, tzOffset = null, timestamp = false throw new Error('invalid time format: ' + time) } - const daysOffset = time.length > 6 ? parseInt(time.slice(0, -6)) : 0 + const daysOffset = parseDaysOffset(ctx, time) let timezone if (tzOffset !== null) { diff --git a/parse/journey.js b/parse/journey.js index 40cba50e6..d672c903e 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -15,7 +15,28 @@ import {findRemarks} from './find-remarks.js' const parseJourney = (ctx, j) => { // j = raw jouney const {profile, opt} = ctx - const legs = j.secL.map(l => profile.parseJourneyLeg(ctx, l, j.date)) + const legs = [] + for (const l of j.secL) { + let date = j.date + // Next-day DEVI legs in an overnight journey lack both + // - the "01" prefix in {dep.d,arr.a}Time{S,R} and + // - the jny.trainStartDate field. + // However, we can use the previous leg's effective date. + const prevLeg = legs[legs.length - 1] || null + if (l.type === 'DEVI' && prevLeg?.arrival) { + // todo: parse effective date from jny.ctxRecon/gis.ctx instead? + // todo: prefer plannedArrival? + date = [ + prevLeg.arrival.slice(0, 4), // year + prevLeg.arrival.slice(5, 7), // month + prevLeg.arrival.slice(8, 10), // day + ].join('') + } + + const leg = profile.parseJourneyLeg(ctx, l, date) + legs.push(leg) + } + const res = { type: 'journey', legs,