Skip to content

Commit

Permalink
fix date of next-day DEVI leg in an overnight journey 🐛
Browse files Browse the repository at this point in the history
follow-up of a2870f6
follow-up of 6e6285c
fixes #301
  • Loading branch information
derhuerst committed Dec 7, 2023
1 parent 6d94286 commit 0d2dc8f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions parse/date-time.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion parse/journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0d2dc8f

Please sign in to comment.