Skip to content

Commit

Permalink
front: drop formatDurationAsISO8601()
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Jan 3, 2025
1 parent 5024f0b commit 34d98e9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils/timeManipulation';
import { Duration } from 'utils/duration';

import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';
Expand Down Expand Up @@ -157,7 +157,7 @@ const getTimeLockDate = (
};

const formatDateDifference = (start: Date, stop: Date) =>
formatDurationAsISO8601(calculateTimeDifferenceInSeconds(start, stop));
Duration.subtractDate(stop, start).toString();

const createTrainSchedulePayload = async ({
trainrunSections,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nextId from 'react-id-generator';
import type { ImportedTrainSchedule } from 'applications/operationalStudies/types';
import type { TrainScheduleBase } from 'common/api/osrdEditoastApi';
import { formatToIsoDate } from 'utils/date';
import { calculateTimeDifferenceInSeconds, formatDurationAsISO8601 } from 'utils/timeManipulation';
import { Duration } from 'utils/duration';

export function generateTrainSchedulesPayloads(
trains: ImportedTrainSchedule[],
Expand All @@ -19,6 +19,8 @@ export function generateTrainSchedulesPayloads(
return payloads; // Skip this train
}

const departureTime = new Date(train.departureTime);

const { path, schedule } = train.steps.reduce(
(acc, step, index) => {
const stepId = nextId();
Expand All @@ -37,13 +39,10 @@ export function generateTrainSchedulesPayloads(

// Skip first step, handle time differences
if (index !== 0) {
const timeDifferenceInSeconds = calculateTimeDifferenceInSeconds(
train.departureTime,
step.arrivalTime
);
const arrivalTime = new Date(step.arrivalTime);
const schedulePoint: NonNullable<TrainScheduleBase['schedule']>[number] = {
at: stepId,
arrival: formatDurationAsISO8601(timeDifferenceInSeconds),
arrival: Duration.subtractDate(arrivalTime, departureTime).toString(),
stop_for: step.duration ? `PT${step.duration}S` : undefined,
};
acc.schedule.push(schedulePoint);
Expand Down
4 changes: 0 additions & 4 deletions front/src/utils/timeManipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ export function calculateTimeDifferenceInDays(datetime1?: Date, datetime2?: Date
return dayjs.duration(date2.getTime() - date1.getTime()).asDays();
}

export function formatDurationAsISO8601(seconds: number) {
return `PT${Math.abs(seconds)}S`;
}

/**
* Parse ISO8601 duration, for instance "PT11H9M8S" (11h, 9min and 8s) to seconds
*/
Expand Down

0 comments on commit 34d98e9

Please sign in to comment.