Skip to content

Commit

Permalink
SebastianHanfland#159: round the times differently
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianHanfland committed Apr 19, 2024
1 parent 706b6cf commit 266da48
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
6 changes: 3 additions & 3 deletions website/src/planner/trackoverview/TrackOverviewDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const getBody = (trackStreetInfos: TrackStreetInfo[]): string => {
.map(
(info) =>
`${info.name};${formatNumber(info.distanceInKm)};${info.peopleCount ?? ''};
${formatTimeOnly(roundStartTimes(info.startFront))};${formatTimeOnly(info.startFront)};${formatTimeOnly(
info.arrivalFront
)};${formatTimeOnly(info.arrivalBack)}`
${formatTimeOnly(roundStartTimes(info.startFront, info.name))};${formatTimeOnly(
info.startFront
)};${formatTimeOnly(info.arrivalFront)};${formatTimeOnly(info.arrivalBack)}`
)
.join('\n');
};
Expand Down
2 changes: 1 addition & 1 deletion website/src/planner/trackoverview/TracksOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const TracksOverview = () => {
<td>{info.name}</td>
<td>{formatNumber(info.distanceInKm)}</td>
<td>{info.peopleCount ?? ''}</td>
<td>{formatTimeOnly(roundStartTimes(info.startFront))}</td>
<td>{formatTimeOnly(roundStartTimes(info.startFront, info.name))}</td>
<td>{formatTimeOnly(info.startFront)}</td>
<td>{formatTimeOnly(info.arrivalFront)}</td>
<td>{formatTimeOnly(info.arrivalBack)}</td>
Expand Down
24 changes: 20 additions & 4 deletions website/src/utils/__tests__/dateUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,34 @@ describe('dateUtil', () => {
describe('roundStartTimes', () => {
const testCases = [
{
description: 'Should subtract 15 minutes and round down to full 15 minutes for M routes',
input: '2007-10-14T10:09:57.000Z',
output: '2007-10-14T09:55:00.000Z',
trackName: 'M1',
output: '2007-10-14T09:45:00.000Z',
},
{
description: 'Should subtract 15 minutes and round down to full 15 minutes for M routes',
input: '2007-10-14T10:15:00.000Z',
trackName: 'M1',
output: '2007-10-14T10:00:00.000Z',
},
{
description: 'Should subtract 10 minutes and round down to full 5 minutes for A routes',
input: '2007-10-14T10:10:00.000Z',
trackName: 'A9',
output: '2007-10-14T10:00:00.000Z',
},
{
description: 'Should subtract 10 minutes and round down to full 5 minutes for A routes',
input: '2007-10-14T10:09:57.000Z',
trackName: 'A9',
output: '2007-10-14T09:55:00.000Z',
},
];

testCases.forEach(({ output, input }) =>
it(`${input} -> ${output}`, () => {
expect(roundStartTimes(input)).toEqual(output);
testCases.forEach(({ output, input, trackName, description }) =>
it(`${description}: ${input} -> ${output}`, () => {
expect(roundStartTimes(input, trackName)).toEqual(output);
})
);
});
Expand Down
17 changes: 12 additions & 5 deletions website/src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ export const DateTimeFormat: FormatDateOptions = {
day: 'numeric',
};

const MINUTES_TO_ROUND_TO = 5;
const MINUTES_TO_SUBTRACT = 10;
const A_ROUTE_MINUTES_TO_ROUND_TO = 5;
const A_ROUTE_MINUTES_TO_SUBTRACT = 10;

export function roundStartTimes(startFront: string) {
const timeMinus5Min = date.addSeconds(new Date(startFront), -MINUTES_TO_SUBTRACT * 60).toISOString();
const M_ROUTE_MINUTES_TO_ROUND_TO = 15;
const M_ROUTE_MINUTES_TO_SUBTRACT = 15;

const coeff = 1000 * 60 * MINUTES_TO_ROUND_TO;
export function roundStartTimes(startFront: string, trackName: string) {
const isMRoute = trackName.startsWith('M');
const minutesToSubtract = isMRoute ? M_ROUTE_MINUTES_TO_SUBTRACT : A_ROUTE_MINUTES_TO_SUBTRACT;
const minutesToRoundTo = isMRoute ? M_ROUTE_MINUTES_TO_ROUND_TO : A_ROUTE_MINUTES_TO_ROUND_TO;

const timeMinus5Min = date.addSeconds(new Date(startFront), -minutesToSubtract * 60).toISOString();

const coeff = 1000 * 60 * minutesToRoundTo;
return new Date(Math.floor(new Date(timeMinus5Min).getTime() / coeff) * coeff).toISOString();
}
4 changes: 3 additions & 1 deletion website/src/versions/trackInfo/TrackInformationModalBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function TrackInfo({ track }: { track: ZipTrack }) {
<>
<h6>{track.filename}</h6>
{showTimes && (
<p className={'p-0 m-0'}>{`Start: ${formatTimeOnly(roundStartTimes(foundInfo.startFront))}`}</p>
<p className={'p-0 m-0'}>{`Start: ${formatTimeOnly(
roundStartTimes(foundInfo.startFront, track.filename)
)}`}</p>
)}
{showTimes && <p className={'p-0 m-0'}>{`Ziel: ${formatTimeOnly(foundInfo.arrivalFront)}`}</p>}
<p className={'p-0 m-0'}>{`Länge: ${formatNumber(foundInfo.distanceInKm)} km`}</p>
Expand Down

0 comments on commit 266da48

Please sign in to comment.