make when timezone- and local-independent

This commit is contained in:
Julius Tens 2017-12-28 22:57:22 +01:00
parent 0de6776815
commit 66d869a3eb
3 changed files with 36 additions and 30 deletions

View file

@ -16,9 +16,11 @@ const {
assertValidLocation,
assertValidLine,
assertValidStopover,
when, isValidWhen
createWhen, assertValidWhen
} = require('./util.js')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStationProducts = (t, p) => {
t.ok(p)
t.equal(typeof p.nationalExp, 'boolean')
@ -106,7 +108,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
if (journey.origin.products) {
assertValidProducts(t, journey.origin.products)
}
t.ok(isValidWhen(journey.departure))
assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.origin.products)
@ -116,7 +118,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
if (journey.destination.products) {
assertValidProducts(t, journey.destination.products)
}
t.ok(isValidWhen(journey.arrival))
assertValidWhen(t, journey.arrival, when)
t.ok(Array.isArray(journey.legs))
t.ok(journey.legs.length > 0, 'no legs')
@ -127,7 +129,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
t.ok(isValidWhen(leg.departure))
assertValidWhen(t, leg.departure, when)
t.equal(typeof leg.departurePlatform, 'string')
assertValidStation(t, leg.destination)
@ -135,7 +137,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
if (!(yield findStation(leg.destination.id))) {
console.error('unknown station', leg.destination.id, leg.destination.name)
}
t.ok(isValidWhen(leg.arrival))
assertValidWhen(t, leg.arrival, when)
t.equal(typeof leg.arrivalPlatform, 'string')
assertValidLine(t, leg.line)
@ -166,8 +168,8 @@ test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(leg.departure))
t.ok(isValidWhen(leg.arrival))
assertValidWhen(t, leg.departure, when)
assertValidWhen(t, leg.arrival, when)
const d = leg.destination
assertValidAddress(t, d)
@ -195,8 +197,8 @@ test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(leg.departure))
t.ok(isValidWhen(leg.arrival))
assertValidWhen(t, leg.departure, when)
assertValidWhen(t, leg.arrival, when)
const d = leg.destination
assertValidPoi(t, d)
@ -220,7 +222,7 @@ test('departures at Berlin Jungfernheide', co.wrap(function* (t) {
console.error('unknown station', dep.station.id, dep.station.name)
}
if (dep.station.products) assertValidProducts(t, dep.station.products)
t.ok(isValidWhen(dep.when))
assertValidWhen(t, dep.when, when)
}
t.end()

View file

@ -98,18 +98,20 @@ const hour = 60 * 60 * 1000
const week = 7 * 24 * hour
// next Monday 10 am
const when = DateTime.fromMillis(Date.now(), {
zone: 'Europe/Berlin',
locale: 'de-DE'
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
const isValidWhen = (w) => {
const ts = +new Date(w)
const createWhen = (timezone, locale) => {
return DateTime.fromMillis(Date.now(), {
zone: timezone,
locale,
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
}
const isValidWhen = (actual, expected) => {
const ts = +new Date(actual)
if (Number.isNaN(ts)) return false
return isRoughlyEqual(12 * hour, +when, ts)
return isRoughlyEqual(12 * hour, +expected, ts)
}
const assertValidWhen = (t, w) => {
t.ok(isValidWhen(w), 'invalid when')
const assertValidWhen = (t, actual, expected) => {
t.ok(isValidWhen(actual, expected), 'invalid when')
}
const assertValidTicket = (t, ti) => {
@ -151,6 +153,6 @@ module.exports = {
assertValidLine,
isValidDateTime,
assertValidStopover,
hour, when, isValidWhen, assertValidWhen,
hour, createWhen, isValidWhen, assertValidWhen,
assertValidTicket
}

View file

@ -17,11 +17,13 @@ const {
assertValidLocation,
assertValidLine: _assertValidLine,
assertValidStopover,
hour, when,
hour, createWhen,
assertValidWhen,
assertValidTicket
} = require('./util')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStation = (t, s, coordsOptional = false) => {
_assertValidStation(t, s, coordsOptional)
t.equal(s.name, shorten(s.name))
@ -70,12 +72,12 @@ test('journeys  station to station', co.wrap(function* (t) {
assertValidStationProducts(t, journey.origin.products)
t.ok(journey.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(journey.origin.id, spichernstr)
assertValidWhen(t, journey.departure)
assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.destination.products)
t.strictEqual(journey.destination.id, amrumerStr)
assertValidWhen(t, journey.arrival)
assertValidWhen(t, journey.arrival, when)
t.ok(Array.isArray(journey.legs))
t.strictEqual(journey.legs.length, 1)
@ -87,12 +89,12 @@ test('journeys  station to station', co.wrap(function* (t) {
assertValidStationProducts(t, leg.origin.products)
t.ok(leg.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(leg.origin.id, spichernstr)
assertValidWhen(t, leg.departure)
assertValidWhen(t, leg.departure, when)
assertValidStation(t, leg.destination)
assertValidStationProducts(t, leg.destination.products)
t.strictEqual(leg.destination.id, amrumerStr)
assertValidWhen(t, leg.arrival)
assertValidWhen(t, leg.arrival, when)
assertValidLine(t, leg.line)
t.ok(findStation(leg.direction))
@ -198,14 +200,14 @@ test('journeys  station to address', co.wrap(function* (t) {
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure)
assertValidWhen(t, leg.departure, when)
const dest = leg.destination
assertValidAddress(t, dest)
t.strictEqual(dest.address, 'Torfstraße 17')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223))
assertValidWhen(t, leg.arrival)
assertValidWhen(t, leg.arrival, when)
t.end()
}))
@ -225,14 +227,14 @@ test('journeys  station to POI', co.wrap(function* (t) {
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure)
assertValidWhen(t, leg.departure, when)
const dest = leg.destination
assertValidPoi(t, dest)
t.strictEqual(dest.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686))
assertValidWhen(t, leg.arrival)
assertValidWhen(t, leg.arrival, when)
t.end()
}))
@ -253,7 +255,7 @@ test('departures', co.wrap(function* (t) {
assertValidStationProducts(t, dep.station.products)
t.strictEqual(dep.station.id, spichernstr)
assertValidWhen(t, dep.when)
assertValidWhen(t, dep.when, when)
t.ok(findStation(dep.direction))
assertValidLine(t, dep.line)
}