fix: Set the dateFrom to startOf day otherwise bad fromOffset is used (#12210)

This commit is contained in:
Alex van Andel 2023-11-03 03:13:42 +00:00 committed by GitHub
parent f044c2d7c7
commit dd0c2dbff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import dayjs from "@calcom/dayjs";
@ -48,6 +48,106 @@ describe("processWorkingHours", () => {
expect(lastAvailableSlot.start.date()).toBe(31);
});
it("It has the correct working hours on date of DST change (- tz)", () => {
vi.useFakeTimers().setSystemTime(new Date("2023-11-05T13:26:14.000Z"));
const item = {
days: [1, 2, 3, 4, 5],
startTime: new Date(Date.UTC(2023, 5, 12, 9, 0)), // 9 AM
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)), // 5 PM
};
const timeZone = "America/New_York";
const dateFrom = dayjs();
const dateTo = dayjs().endOf("month");
const results = processWorkingHours({ item, timeZone, dateFrom, dateTo });
expect(results).toStrictEqual([
{
start: dayjs("2023-11-06T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-06T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-07T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-07T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-08T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-08T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-09T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-09T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-10T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-10T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-13T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-13T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-14T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-14T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-15T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-15T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-16T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-16T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-17T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-17T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-20T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-20T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-21T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-21T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-22T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-22T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-23T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-23T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-24T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-24T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-27T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-27T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-28T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-28T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-29T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-29T22:00:00.000Z").tz(timeZone),
},
{
start: dayjs("2023-11-30T14:00:00.000Z").tz(timeZone),
end: dayjs("2023-11-30T22:00:00.000Z").tz(timeZone),
},
]);
vi.setSystemTime(vi.getRealSystemTime());
vi.useRealTimers();
});
// TEMPORAIRLY SKIPPING THIS TEST - Started failing after 29th Oct
it.skip("should return the correct working hours in the month were DST ends", () => {
const item = {

View File

@ -23,7 +23,7 @@ export function processWorkingHours({
}) {
const results = [];
for (let date = dateFrom.tz(timeZone).startOf("day"); dateTo.isAfter(date); date = date.add(1, "day")) {
const fromOffset = dateFrom.tz(timeZone).utcOffset();
const fromOffset = dateFrom.tz(timeZone).startOf("day").utcOffset();
const offset = date.tz(timeZone).utcOffset();
// it always has to be start of the day (midnight) even when DST changes