parseDateTime: return ISO str/timestamp 💥

This commit is contained in:
Jannis R 2018-10-15 15:50:43 +02:00
parent b99ceb21fb
commit a9fd9ff814
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 32 additions and 39 deletions

View file

@ -14,12 +14,11 @@ const createParseArrOrDep = (profile, opt, data, prefix) => {
const parseArrOrDep = (d) => { const parseArrOrDep = (d) => {
const t = d.stbStop[prefix + 'TimeR'] || d.stbStop[prefix + 'TimeS'] const t = d.stbStop[prefix + 'TimeR'] || d.stbStop[prefix + 'TimeS']
const when = profile.parseDateTime(profile, d.date, t)
const res = { const res = {
tripId: d.jid, tripId: d.jid,
stop: locations[parseInt(d.stbStop.locX)] || null, 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* // todo: for arrivals, this is the *origin*, not the *direction*
direction: prefix === DEPARTURE && profile.parseStationName(d.dirTxt) || null, direction: prefix === DEPARTURE && profile.parseStationName(d.dirTxt) || null,
line: lines[parseInt(d.prodX)] || null, line: lines[parseInt(d.prodX)] || null,
@ -33,8 +32,8 @@ const createParseArrOrDep = (profile, opt, data, prefix) => {
const tR = d.stbStop[prefix + 'TimeR'] const tR = d.stbStop[prefix + 'TimeR']
const tP = d.stbStop[prefix + 'TimeS'] const tP = d.stbStop[prefix + 'TimeS']
if (tR && tP) { if (tR && tP) {
const realtime = profile.parseDateTime(profile, d.date, tR) const realtime = profile.parseDateTime(profile, d.date, tR, true)
const planned = profile.parseDateTime(profile, d.date, tP) const planned = profile.parseDateTime(profile, d.date, tP, true)
res.delay = Math.round((realtime - planned) / 1000) res.delay = Math.round((realtime - planned) / 1000)
} else res.delay = null } else res.delay = null
@ -51,9 +50,7 @@ const createParseArrOrDep = (profile, opt, data, prefix) => {
res.cancelled = true res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true}) Object.defineProperty(res, 'canceled', {value: true})
res.when = res.delay = null res.when = res.delay = null
res.formerScheduledWhen = profile.parseDateTime(profile, d.date, tP)
const when = profile.parseDateTime(profile, d.date, tP)
res.formerScheduledWhen = when.toISO()
} }
if (opt.remarks) { if (opt.remarks) {

View file

@ -5,7 +5,7 @@ const {DateTime, IANAZone} = require('luxon')
const timezones = new WeakMap() const timezones = new WeakMap()
// todo: change to `(profile) => (date, time) => {}` // 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)] const pDate = [date.substr(-8, 4), date.substr(-4, 2), date.substr(-2, 2)]
if (!pDate[0] || !pDate[1] || !pDate[2]) { if (!pDate[0] || !pDate[1] || !pDate[2]) {
throw new Error('invalid date format: ' + date) throw new Error('invalid date format: ' + date)
@ -25,11 +25,12 @@ const parseDateTime = (profile, date, time) => {
timezones.set(profile, timezone) 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, locale: profile.locale,
zone: timezone 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 module.exports = parseDateTime

View file

@ -54,20 +54,20 @@ const createParseJourneyLeg = (profile, opt, data) => {
const res = { const res = {
origin: clone(locations[parseInt(pt.dep.locX)]) || null, origin: clone(locations[parseInt(pt.dep.locX)]) || null,
destination: clone(locations[parseInt(pt.arr.locX)]), destination: clone(locations[parseInt(pt.arr.locX)]),
departure: dep.toISO(), departure: dep,
arrival: arr.toISO() arrival: arr
} }
// todo: DRY with parseDeparture // todo: DRY with parseDeparture
// todo: DRY with parseStopover // todo: DRY with parseStopover
if (pt.dep.dTimeR && pt.dep.dTimeS) { if (pt.dep.dTimeR && pt.dep.dTimeS) {
const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR) const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR, true)
const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS, true)
res.departureDelay = Math.round((realtime - planned) / 1000) res.departureDelay = Math.round((realtime - planned) / 1000)
} }
if (pt.arr.aTimeR && pt.arr.aTimeS) { if (pt.arr.aTimeR && pt.arr.aTimeS) {
const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR) const realtime = profile.parseDateTime(profile, j.date, pt.arr.aTimeR, true)
const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS) const planned = profile.parseDateTime(profile, j.date, pt.arr.aTimeS, true)
res.arrivalDelay = Math.round((realtime - planned) / 1000) 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) const planned = st0.dTimeS && profile.parseDateTime(profile, j.date, st0.dTimeS)
if (st0.dTimeR && planned) { if (st0.dTimeR && planned) {
const realtime = profile.parseDateTime(profile, j.date, st0.dTimeR) const realtime = profile.parseDateTime(profile, j.date, st0.dTimeR)
when = realtime.toISO() when = realtime
delay = Math.round((realtime - planned) / 1000) delay = Math.round((new Date(realtime) - new Date(planned)) / 1000)
} else if (planned) when = planned.toISO() } else if (planned) when = planned
} }
return { return {
tripId: a.jid, tripId: a.jid,
line: lines[parseInt(a.prodX)] || null, line: lines[parseInt(a.prodX)] || null,
@ -162,13 +161,11 @@ const createParseJourneyLeg = (profile, opt, data) => {
Object.defineProperty(res, 'canceled', {value: true}) Object.defineProperty(res, 'canceled', {value: true})
if (pt.arr.aCncl) { if (pt.arr.aCncl) {
res.arrival = res.arrivalPlatform = res.arrivalDelay = null res.arrival = res.arrivalPlatform = res.arrivalDelay = null
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeS) res.formerScheduledArrival = profile.parseDateTime(profile, j.date, pt.arr.aTimeS)
res.formerScheduledArrival = arr.toISO()
} }
if (pt.dep.dCncl) { if (pt.dep.dCncl) {
res.departure = res.departurePlatform = res.departureDelay = null res.departure = res.departurePlatform = res.departureDelay = null
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) res.formerScheduledDeparture = profile.parseDateTime(profile, j.date, pt.dep.dTimeS)
res.formerScheduledDeparture = dep.toISO()
} }
} }

View file

@ -19,22 +19,20 @@ const createParseStopover = (profile, opt, data, date) => {
// todo: DRY with parseDeparture // todo: DRY with parseDeparture
// todo: DRY with parseJourneyLeg // todo: DRY with parseJourneyLeg
if (st.aTimeR || st.aTimeS) { if (st.aTimeR || st.aTimeS) {
const arr = profile.parseDateTime(profile, date, st.aTimeR || st.aTimeS) res.arrival = profile.parseDateTime(profile, date, st.aTimeR || st.aTimeS)
res.arrival = arr.toISO()
} }
if (st.aTimeR && st.aTimeS) { if (st.aTimeR && st.aTimeS) {
const realtime = profile.parseDateTime(profile, date, st.aTimeR) const realtime = profile.parseDateTime(profile, date, st.aTimeR, true)
const planned = profile.parseDateTime(profile, date, st.aTimeS) const planned = profile.parseDateTime(profile, date, st.aTimeS, true)
res.arrivalDelay = Math.round((realtime - planned) / 1000) res.arrivalDelay = Math.round((realtime - planned) / 1000)
} }
if (st.dTimeR || st.dTimeS) { if (st.dTimeR || st.dTimeS) {
const dep = profile.parseDateTime(profile, date, st.dTimeR || st.dTimeS) res.departure = profile.parseDateTime(profile, date, st.dTimeR || st.dTimeS)
res.departure = dep.toISO()
} }
if (st.dTimeR && st.dTimeS) { if (st.dTimeR && st.dTimeS) {
const realtime = profile.parseDateTime(profile, date, st.dTimeR) const realtime = profile.parseDateTime(profile, date, st.dTimeR, true)
const planned = profile.parseDateTime(profile, date, st.dTimeS) const planned = profile.parseDateTime(profile, date, st.dTimeS, true)
res.departureDelay = Math.round((realtime - planned) / 1000) res.departureDelay = Math.round((realtime - planned) / 1000)
} }
@ -54,17 +52,17 @@ const createParseStopover = (profile, opt, data, date) => {
res.cancelled = true res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true}) Object.defineProperty(res, 'canceled', {value: true})
if (st.aCncl) { if (st.aCncl) {
res.formerArrivalDelay = res.arrivalDelay
res.arrival = res.arrivalDelay = null res.arrival = res.arrivalDelay = null
if (st.aTimeS) { if (st.aTimeS) {
const arr = profile.parseDateTime(profile, date, st.aTimeS) res.formerScheduledArrival = profile.parseDateTime(profile, date, st.aTimeS)
res.formerScheduledArrival = arr.toISO()
} }
} }
if (st.dCncl) { if (st.dCncl) {
res.formerDepartureDelay = res.departureDelay
res.departure = res.departureDelay = null res.departure = res.departureDelay = null
if (st.dTimeS) { if (st.dTimeS) {
const arr = profile.parseDateTime(profile, date, st.dTimeS) res.formerScheduledDeparture = profile.parseDateTime(profile, date, st.dTimeS)
res.formerScheduledDeparture = arr.toISO()
} }
} }
} }

View file

@ -35,9 +35,9 @@ const parseWarning = (profile, w, icons) => {
category: w.cat || null // todo: parse to sth meaningful 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.sDate && w.sTime) res.validFrom = parseDateTime(profile, w.sDate, w.sTime)
if (w.eDate && w.eTime) res.validUntil = parseDateTime(profile, w.eDate, w.eTime).toISO() 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).toISO() if (w.lModDate && w.lModTime) res.modified = parseDateTime(profile, w.lModDate, w.lModTime)
return res return res
} }