diff --git a/format-rest/date.js b/format-rest/date.js new file mode 100644 index 000000000..a2c5ea3bb --- /dev/null +++ b/format-rest/date.js @@ -0,0 +1,21 @@ +'use strict' + +const {DateTime, IANAZone} = require('luxon') + +const timezones = new WeakMap() + +const formatDate = (profile, when) => { + let timezone + if (timezones.has(profile)) timezone = timezones.get(profile) + else { + timezone = new IANAZone(profile.timezone) + timezones.set(profile, timezone) + } + + return DateTime.fromMillis(+when, { + locale: profile.locale, + zone: timezone + }).toFormat('yyyy-MM-dd') +} + +module.exports = formatDate diff --git a/rest-exe.js b/rest-exe.js index f4bab3f9e..e878be55d 100644 --- a/rest-exe.js +++ b/rest-exe.js @@ -17,6 +17,7 @@ const parseStopover = require('./parse-rest/stopover') const parseLocation = require('./parse-rest/location') const parseArrivalOrDeparture = require('./parse-rest/arrival-or-departure') const parseTrip = require('./parse-rest/trip') +const formatDate = require('./format-rest/date') const defaultProfile = require('./lib/default-profile') const _profile = require('./p/db') @@ -97,7 +98,8 @@ const profile = { parseJourney, parseJourneyLeg, parseStopover, - parseLocation + parseLocation, + formatDate } const opt = { @@ -222,15 +224,63 @@ const trip = async (id) => { return parseTrip(profile, opt, {})(res) } +// todo: fails with 404 +// const tripHistory = async (tripId) => { +// const res = await request('rtarchive', { +// id: tripId, +// date: profile.formatDate(profile, opt.when || Date.now()) +// }) +// return res +// } + +// todo: fails with 404 +// const radar = async (bbox) => { +// const res = await request('journeyPos', { +// llLat: bbox.south, +// llLon: bbox.west, +// urLat: bbox.north, +// urLon: bbox.east, +// // todo: operators, products, attributes, lines, jid, infotexts +// // todo: maxJny, time +// date: profile.formatDate(profile, opt.when || Date.now()) +// }) +// return res +// } + +// todo: fails with 404 +// const remarks = async () => { +// const res = await request('himSearch', { +// // todo: dateB, dateE, timeB, timeE, himIds, operators, categories +// // todo: channels, companies, metas, himcategory, poly, searchmode +// // todo: minprio, maxprio +// }) +// return res +// } + +// todo: fails with 404 +// const dataInfo = async () => { +// const res = await request('datainfo') +// return res +// } + // journeys() // .then(([journey]) => journey.legs.map(l => l.tripId).find(tripId => !!tripId)) // .then(trip) +// .then(tripHistory) // locations('leopoldplatz berlin') // nearby({type: 'location', latitude: 52.4751309, longitude: 13.3656537}) // tripAlternatives() // departures() // arrivals() +// radar({ +// north: 52.52411, +// west: 13.41002, +// south: 52.51942, +// east: 13.41709 +// }) +// remarks() +// dataInfo() .then(data => console.error(require('util').inspect(data, {depth: null, colors: true}))) .catch((err) => {