mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
parseDateTime: return ISO str/timestamp 💥
This commit is contained in:
parent
b99ceb21fb
commit
a9fd9ff814
5 changed files with 32 additions and 39 deletions
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue