Skip to content

Commit

Permalink
⚗️ expressively submit the client timezone because the server might r…
Browse files Browse the repository at this point in the history
…un on UTC
  • Loading branch information
volkland committed Oct 23, 2021
1 parent 31eae72 commit afb254d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
9 changes: 6 additions & 3 deletions client/src/App.tsx
Expand Up @@ -64,12 +64,15 @@ function App() {

setPending(true);
fetch(BACKEND_URL + "journeys?" + new URLSearchParams({
from, to, departure: departure.toISOString()
from,
to,
departure: departure.toISOString(),
departureTZOffset: departure.getTimezoneOffset().toString()
}))
.then(res => res.json())
.then(({ journeys, error }: { journeys?: JourneyResponse[], error?: string }) => {
setError(error)

if (journeys) {
setJourneys(journeys)
}
Expand Down Expand Up @@ -141,7 +144,7 @@ function App() {
</Form>
</Col>
<Col md={6}>
<Table striped bordered hover style={{ marginBottom: "2rem" }}>
<Table striped bordered hover style={{ marginBottom: "2rem" }}>
<thead>
<tr>
<th>Reise</th>
Expand Down
7 changes: 4 additions & 3 deletions src/app.ts
Expand Up @@ -28,7 +28,7 @@ const lineDesc = (l: Leg) => {

return "other"
}
const journeyText = (j: Journey) => `${toShortDate(j.legs[0].departure)} => ${toShortDate(j.legs[j.legs.length - 1].arrival)}: ${j.legs.map(lineDesc).join(", ")}`
const journeyText = (j: Journey, departureTZOffset: number) => `${toShortDate(j.legs[0].departure, departureTZOffset)} => ${toShortDate(j.legs[j.legs.length - 1].arrival, departureTZOffset)}: ${j.legs.map(lineDesc).join(", ")}`

/**
* @route GET /journeys
Expand All @@ -40,17 +40,18 @@ app.get('/journeys', async (req, res) => {
const from = await client.locations(req.query.from as string, { results: 1 })
const to = await client.locations(req.query.to as string, { results: 1 })
const departure = new Date(req.query.departure as string);
const departureTZOffset = parseInt(req.query.departureTZOffset as string);

if (Object.is(departure.getTime(), NaN)) {
return res.status(400).send({ error: "Bad Request: Bad departure time" })
}

const journeys = (await client.journeys(from[0], to[0], {
results: 10,
departure: req.query.departure as string
departure: departure
}))
.journeys.map(j => ({
journeyText: journeyText(j),
journeyText: journeyText(j, departureTZOffset),
refreshToken: encode(j.refreshToken)
}))

Expand Down
16 changes: 10 additions & 6 deletions src/date-utils.ts
Expand Up @@ -14,23 +14,27 @@ export type ArrivingDepartingWithPossibleDelay = ({
plannedDeparture?: IsoDate,
}

export const toShortDate = (d: IsoDate): string =>
new Intl.DateTimeFormat('de-DE', {
export const toShortDate = (d: IsoDate, departureTZOffset: number): string => {
const date = new Date(d)
date.setUTCMinutes(date.getMinutes() - departureTZOffset);

return new Intl.DateTimeFormat('de-DE', {
hour: '2-digit',
minute: '2-digit'
}).format(new Date(d));
}).format(date);
}

export const toDateObj = (d: IsoDate): Date =>
export const toDateObj = (d: IsoDate): Date =>
new Date(d)

export const dateWithDelay = (d: IsoDate, delay?: number): Date => {
const scheduled = new Date(d);

if ( typeof delay !=="number" || !delay) {
if (typeof delay !== "number" || !delay) {
return scheduled;
}

const date= new Date(scheduled.getTime() + (delay ?? 0) * 60 * 1000);
const date = new Date(scheduled.getTime() + (delay ?? 0) * 60 * 1000);

return date;
}
2 changes: 1 addition & 1 deletion src/ical.ts
Expand Up @@ -51,7 +51,7 @@ const getEmoji = (leg: Leg): string => {

const getStopovers = (leg: Leg): string =>
leg.stopovers.map((s) =>
`${s.stop.name} (an: ${toShortDate(s.arrival)}${s.arrivalDelay ? ` + ${s.arrivalDelay}min` : ""}, ab: ${toShortDate(s.departure)}${s.departureDelay ? ` + ${s.departureDelay}min` : ""})`
`${s.stop.name} (an: ${toShortDate(s.arrival, 0)}${s.arrivalDelay ? ` + ${s.arrivalDelay}min` : ""}, ab: ${toShortDate(s.departure, 0)}${s.departureDelay ? ` + ${s.departureDelay}min` : ""})`
).join(", ");

export const legToEvent = (leg: Leg): Event | null => {
Expand Down

0 comments on commit afb254d

Please sign in to comment.