diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index a1c39493..1c3b043e 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -14,12 +14,11 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { const parseArrOrDep = (d) => { const t = d.stbStop[prefix + 'TimeR'] || d.stbStop[prefix + 'TimeS'] - const when = profile.parseDateTime(profile, d.date, t) const res = { tripId: d.jid, stop: locations[parseInt(d.stbStop.locX)] || null, - when: when.toISO(), + when: profile.parseDateTime(profile, d.date, t), // todo: for arrivals, this is the *origin*, not the *direction* direction: prefix === DEPARTURE && profile.parseStationName(d.dirTxt) || null, line: lines[parseInt(d.prodX)] || null, @@ -33,8 +32,8 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { const tR = d.stbStop[prefix + 'TimeR'] const tP = d.stbStop[prefix + 'TimeS'] if (tR && tP) { - const realtime = profile.parseDateTime(profile, d.date, tR) - const planned = profile.parseDateTime(profile, d.date, tP) + const realtime = profile.parseDateTime(profile, d.date, tR, true) + const planned = profile.parseDateTime(profile, d.date, tP, true) res.delay = Math.round((realtime - planned) / 1000) } else res.delay = null @@ -51,9 +50,7 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { res.cancelled = true Object.defineProperty(res, 'canceled', {value: true}) res.when = res.delay = null - - const when = profile.parseDateTime(profile, d.date, tP) - res.formerScheduledWhen = when.toISO() + res.formerScheduledWhen = profile.parseDateTime(profile, d.date, tP) } if (opt.remarks) { diff --git a/parse/date-time.js b/parse/date-time.js index 9c777ad4..e551ea94 100644 --- a/parse/date-time.js +++ b/parse/date-time.js @@ -5,7 +5,7 @@ const {DateTime, IANAZone} = require('luxon') const timezones = new WeakMap() // todo: change to `(profile) => (date, time) => {}` -const parseDateTime = (profile, date, time) => { +const parseDateTime = (profile, date, time, timestamp = false) => { 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) @@ -25,11 +25,12 @@ const parseDateTime = (profile, date, time) => { timezones.set(profile, timezone) } - const dt = DateTime.fromISO(pDate.join('-') + 'T' + pTime.join(':'), { + let dt = DateTime.fromISO(pDate.join('-') + 'T' + pTime.join(':'), { locale: profile.locale, zone: timezone }) - return offset > 0 ? dt.plus({days: offset}) : dt + if (offset > 0) dt = dt.plus({days: offset}) + return timestamp ? dt.toMillis() : dt.toISO() } module.exports = parseDateTime diff --git a/parse/journey-leg.js b/parse/journey-leg.js index 784fcbce..0150295f 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -54,20 +54,20 @@ const createParseJourneyLeg = (profile, opt, data) => { const res = { origin: clone(locations[parseInt(pt.dep.locX)]) || null, destination: clone(locations[parseInt(pt.arr.locX)]), - departure: dep.toISO(), - arrival: arr.toISO() + departure: dep, + arrival: arr } // todo: DRY with parseDeparture // todo: DRY with parseStopover if (pt.dep.dTimeR && pt.dep.dTimeS) { - const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR) - const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) + const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR, true) + const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS, true) res.departureDelay = Math.round((realtime - planned) / 1000) } if (pt.arr.aTimeR && pt.arr.aTimeS) { - const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR) - const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS) + const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR, true) + const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS, true) res.arrivalDelay = Math.round((realtime - planned) / 1000) } @@ -139,11 +139,10 @@ const createParseJourneyLeg = (profile, opt, data) => { const planned = st0.dTimeS && profile.parseDateTime(profile, j.date, st0.dTimeS) if (st0.dTimeR && planned) { const realtime = profile.parseDateTime(profile, j.date, st0.dTimeR) - when = realtime.toISO() - delay = Math.round((realtime - planned) / 1000) - } else if (planned) when = planned.toISO() + when = realtime + delay = Math.round((new Date(realtime) - new Date(planned)) / 1000) + } else if (planned) when = planned } - return { tripId: a.jid, line: lines[parseInt(a.prodX)] || null, @@ -162,13 +161,11 @@ const createParseJourneyLeg = (profile, opt, data) => { Object.defineProperty(res, 'canceled', {value: true}) if (pt.arr.aCncl) { res.arrival = res.arrivalPlatform = res.arrivalDelay = null - const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeS) - res.formerScheduledArrival = arr.toISO() + res.formerScheduledArrival = profile.parseDateTime(profile, j.date, pt.arr.aTimeS) } if (pt.dep.dCncl) { res.departure = res.departurePlatform = res.departureDelay = null - const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) - res.formerScheduledDeparture = dep.toISO() + res.formerScheduledDeparture = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) } } diff --git a/parse/stopover.js b/parse/stopover.js index 8c97acdd..1a3e7a7e 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -19,22 +19,20 @@ const createParseStopover = (profile, opt, data, date) => { // todo: DRY with parseDeparture // todo: DRY with parseJourneyLeg if (st.aTimeR || st.aTimeS) { - const arr = profile.parseDateTime(profile, date, st.aTimeR || st.aTimeS) - res.arrival = arr.toISO() + res.arrival = profile.parseDateTime(profile, date, st.aTimeR || st.aTimeS) } if (st.aTimeR && st.aTimeS) { - const realtime = profile.parseDateTime(profile, date, st.aTimeR) - const planned = profile.parseDateTime(profile, date, st.aTimeS) + const realtime = profile.parseDateTime(profile, date, st.aTimeR, true) + const planned = profile.parseDateTime(profile, date, st.aTimeS, true) res.arrivalDelay = Math.round((realtime - planned) / 1000) } if (st.dTimeR || st.dTimeS) { - const dep = profile.parseDateTime(profile, date, st.dTimeR || st.dTimeS) - res.departure = dep.toISO() + res.departure = profile.parseDateTime(profile, date, st.dTimeR || st.dTimeS) } if (st.dTimeR && st.dTimeS) { - const realtime = profile.parseDateTime(profile, date, st.dTimeR) - const planned = profile.parseDateTime(profile, date, st.dTimeS) + const realtime = profile.parseDateTime(profile, date, st.dTimeR, true) + const planned = profile.parseDateTime(profile, date, st.dTimeS, true) res.departureDelay = Math.round((realtime - planned) / 1000) } @@ -54,17 +52,17 @@ const createParseStopover = (profile, opt, data, date) => { res.cancelled = true Object.defineProperty(res, 'canceled', {value: true}) if (st.aCncl) { + res.formerArrivalDelay = res.arrivalDelay res.arrival = res.arrivalDelay = null if (st.aTimeS) { - const arr = profile.parseDateTime(profile, date, st.aTimeS) - res.formerScheduledArrival = arr.toISO() + res.formerScheduledArrival = profile.parseDateTime(profile, date, st.aTimeS) } } if (st.dCncl) { + res.formerDepartureDelay = res.departureDelay res.departure = res.departureDelay = null if (st.dTimeS) { - const arr = profile.parseDateTime(profile, date, st.dTimeS) - res.formerScheduledDeparture = arr.toISO() + res.formerScheduledDeparture = profile.parseDateTime(profile, date, st.dTimeS) } } } diff --git a/parse/warning.js b/parse/warning.js index 9d4eac05..234a71d2 100644 --- a/parse/warning.js +++ b/parse/warning.js @@ -35,9 +35,9 @@ const parseWarning = (profile, w, icons) => { category: w.cat || null // todo: parse to sth meaningful } - if (w.sDate && w.sTime) res.validFrom = parseDateTime(profile, w.sDate, w.sTime).toISO() - if (w.eDate && w.eTime) res.validUntil = parseDateTime(profile, w.eDate, w.eTime).toISO() - if (w.lModDate && w.lModTime) res.modified = parseDateTime(profile, w.lModDate, w.lModTime).toISO() + if (w.sDate && w.sTime) res.validFrom = parseDateTime(profile, w.sDate, w.sTime) + if (w.eDate && w.eTime) res.validUntil = parseDateTime(profile, w.eDate, w.eTime) + if (w.lModDate && w.lModTime) res.modified = parseDateTime(profile, w.lModDate, w.lModTime) return res }