Skip to content

Commit

Permalink
fixed convention dates display to UTC, in convention form, summary an…
Browse files Browse the repository at this point in the history
…d document
  • Loading branch information
enguerranws committed Dec 12, 2024
1 parent b043a2f commit 1251d9b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ export const ScheduleSection = () => {
const inputName = event.target.name;
if (inputValue !== "" && isStringDate(inputValue)) {
const newDates: DateIntervalDto = {
start: new Date(values.dateStart),
end: new Date(values.dateEnd),
start: convertLocaleDateToUtcTimezoneDate(new Date(values.dateStart)),
end: convertLocaleDateToUtcTimezoneDate(new Date(values.dateEnd)),
};

const inputValueAsDate = new Date(inputValue);
const inputValueAsDate = convertLocaleDateToUtcTimezoneDate(
new Date(inputValue),
);

if (inputName === "dateStart") {
newDates.start = inputValueAsDate;
Expand Down
21 changes: 16 additions & 5 deletions front/src/app/contents/convention/conventionSummary.helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
ConventionReadDto,
addressDtoToString,
convertLocaleDateToUtcTimezoneDate,
makeSiretDescriptionLink,
makeWeeklySchedule,
removeEmptyValue,
Expand Down Expand Up @@ -385,7 +386,9 @@ const makeBeneficiarySubSections = (
key: "beneficiaryBirthdate",
label: "Date de naissance",
value: toDisplayedDate({
date: new Date(convention.signatories.beneficiary.birthdate),
date: convertLocaleDateToUtcTimezoneDate(
new Date(convention.signatories.beneficiary.birthdate),
),
}),
},
{
Expand Down Expand Up @@ -588,12 +591,20 @@ const makeImmersionSubSections = (
{
key: "dateStart",
label: "Date de début",
value: toDisplayedDate({ date: new Date(convention.dateStart) }),
value: toDisplayedDate({
date: convertLocaleDateToUtcTimezoneDate(
new Date(convention.dateStart),
),
}),
},
{
key: "dateEnd",
label: "Date de fin",
value: toDisplayedDate({ date: new Date(convention.dateEnd) }),
value: toDisplayedDate({
date: convertLocaleDateToUtcTimezoneDate(
new Date(convention.dateEnd),
),
}),
},
],
},
Expand Down Expand Up @@ -677,8 +688,8 @@ export const makeConventionSections = (

const printWeekSchedule = (convention: ConventionReadDto, cx: Cx) => {
const weeklySchedule = makeWeeklySchedule(convention.schedule, {
start: new Date(convention.dateStart),
end: new Date(convention.dateEnd),
start: convertLocaleDateToUtcTimezoneDate(new Date(convention.dateStart)),
end: convertLocaleDateToUtcTimezoneDate(new Date(convention.dateEnd)),
});
return (
<div className={fr.cx("fr-grid-row")}>
Expand Down
21 changes: 18 additions & 3 deletions front/src/app/pages/convention/ConventionDocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ConventionId,
ConventionReadDto,
addressDtoToString,
convertLocaleDateToUtcTimezoneDate,
domElementIds,
isConventionRenewed,
isStringDate,
Expand Down Expand Up @@ -221,7 +222,11 @@ export const ConventionDocumentPage = ({
né(e) le{" "}
<strong>
{isStringDate(beneficiary.birthdate)
? toDisplayedDate({ date: new Date(beneficiary.birthdate) })
? toDisplayedDate({
date: convertLocaleDateToUtcTimezoneDate(
new Date(beneficiary.birthdate),
),
})
: "Date invalide"}
</strong>{" "}
en qualité de <strong>bénéficiaire</strong> {""}
Expand Down Expand Up @@ -434,8 +439,18 @@ export const ConventionDocumentPage = ({
<p className={fr.cx("fr-text--bold")}>
{internshipKind === "immersion" ? "L'immersion" : "Le mini-stage"}{" "}
se déroulera du{" "}
{toDisplayedDate({ date: new Date(convention.dateStart) })} au{" "}
{toDisplayedDate({ date: new Date(convention.dateEnd) })}.
{toDisplayedDate({
date: convertLocaleDateToUtcTimezoneDate(
new Date(convention.dateStart),
),
})}{" "}
au{" "}
{toDisplayedDate({
date: convertLocaleDateToUtcTimezoneDate(
new Date(convention.dateEnd),
),
})}
.
</p>
<div>
<strong>
Expand Down
19 changes: 11 additions & 8 deletions shared/src/schedule/ScheduleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "../convention/convention.dto";
import { isConventionOld } from "../convention/convention.schema";
import { arrayFromNumber } from "../utils";
import { DateString } from "../utils/date";
import { DateString, convertLocaleDateToUtcTimezoneDate } from "../utils/date";
import {
DailyScheduleDto,
DateIntervalDto,
Expand Down Expand Up @@ -361,13 +361,16 @@ export const makeImmersionTimetable = (
mondayOfFirstWeek,
(dayIndex + weekIndex * weekdays.length) * 24,
);
const dailySchedule = complexSchedule.find(
(dailySchedule) =>
parseISO(dailySchedule.date).getDate() === date.getDate() &&
parseISO(dailySchedule.date).getMonth() === date.getMonth() &&
parseISO(dailySchedule.date).getFullYear() ===
date.getFullYear(),
);
const dailySchedule = complexSchedule.find((dailyScheduleItem) => {
const schedule = convertLocaleDateToUtcTimezoneDate(
parseISO(dailyScheduleItem.date),
);
return (
schedule.getDate() === date.getDate() &&
schedule.getMonth() === date.getMonth() &&
schedule.getFullYear() === date.getFullYear()
);
});
return {
timePeriods: dailySchedule?.timePeriods ?? null,
date: date.toISOString(),
Expand Down
17 changes: 14 additions & 3 deletions shared/src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Matches valid dates of the format 'yyyy-mm-dd'.
import { addHours, format, isValid } from "date-fns";
import { z } from "zod";
import { Flavor } from "../typeFlavors";
Expand Down Expand Up @@ -31,5 +30,17 @@ export const toDisplayedDate = ({

export const isStringDate = (string: string) => isValid(new Date(string));

export const convertLocaleDateToUtcTimezoneDate = (date: Date): Date =>
addHours(date, date.getTimezoneOffset() / 60);
export const convertLocaleDateToUtcTimezoneDate = (date: Date): Date => {
const datePlusTwoHours = addHours(date, 2); // get rid of daylight saving time
return new Date(
Date.UTC(
datePlusTwoHours.getFullYear(),
datePlusTwoHours.getMonth(),
datePlusTwoHours.getDate(),
0,
0,
0,
0,
),
);
};
33 changes: 32 additions & 1 deletion shared/src/utils/date.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { parseISO } from "date-fns";
import { toDateString, toDisplayedDate } from "./date";
import {
convertLocaleDateToUtcTimezoneDate,
toDateString,
toDisplayedDate,
} from "./date";

describe("Date utils tests - toDateString", () => {
it("should format a valid date", () => {
Expand Down Expand Up @@ -38,3 +42,30 @@ describe("Date utils tests - toDisplayedDate", () => {
expect(() => toDisplayedDate({ date })).toThrow("Invalid time value");
});
});

describe("Date utils tests - convertLocaleDateToUtcTimezoneDate", () => {
it("should convert a date with timezone + to an absolute date (UTC)", () => {
const date = parseISO("2024-10-19T05:00:00.000+02:00");
expect(convertLocaleDateToUtcTimezoneDate(date)).toEqual(
parseISO("2024-10-19T00:00:00.000Z"),
);
});
it("should convert a date with timezone - to an absolute date (UTC)", () => {
const date = parseISO("2024-01-01T00:00:00.000-05:00");
expect(convertLocaleDateToUtcTimezoneDate(date)).toEqual(
parseISO("2024-01-01T00:00:00.000Z"),
);
});
it("should do nothing to an absolute date (UTC)", () => {
const date = parseISO("2024-01-01T00:00:00.000Z");
expect(convertLocaleDateToUtcTimezoneDate(date)).toEqual(
parseISO("2024-01-01T00:00:00.000Z"),
);
});
it("should handle hour switch", () => {
const date = parseISO("2025-03-30T00:00:00.000+02:00");
expect(convertLocaleDateToUtcTimezoneDate(date).toISOString()).toBe(
"2025-03-30T00:00:00.000Z",
);
});
});

0 comments on commit 1251d9b

Please sign in to comment.