fix: issue with date overrides in some timezones (#10254)

* fix date overrides

* add test

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
pull/10260/head^2
Carina Wollendorfer 2023-07-19 14:44:41 -04:00 committed by GitHub
parent 92ad652666
commit 8a432b45a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View File

@ -112,6 +112,35 @@ describe("buildDateRanges", () => {
end: dayjs("2023-06-14T16:00:00Z").tz(timeZone),
});
});
it("should return correct date ranges if date override would already already be the next day in utc timezone", () => {
const items = [
{
date: new Date(Date.UTC(2023, 5, 13)),
startTime: new Date(Date.UTC(0, 0, 0, 22, 0)),
endTime: new Date(Date.UTC(0, 0, 0, 23, 0)),
},
{
days: [1, 2, 3, 4, 5],
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)),
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)),
},
];
const timeZone = "America/New_York";
const dateFrom = dayjs("2023-06-13T00:00:00Z");
const dateTo = dayjs("2023-06-15T00:00:00Z");
const results = buildDateRanges({ availability: items, timeZone, dateFrom, dateTo });
expect(results[0]).toEqual({
start: dayjs("2023-06-14T02:00:00Z").tz(timeZone),
end: dayjs("2023-06-14T03:00:00Z").tz(timeZone),
});
expect(results[1]).toEqual({
start: dayjs("2023-06-14T12:00:00Z").tz(timeZone),
end: dayjs("2023-06-14T21:00:00Z").tz(timeZone),
});
});
});
describe("subtract", () => {

View File

@ -52,9 +52,11 @@ export function processDateOverride({ item, timeZone }: { item: DateOverride; ti
const startTime = dayjs(item.startTime).utc().subtract(dayjs().tz(timeZone).utcOffset(), "minute");
const endTime = dayjs(item.endTime).utc().subtract(dayjs().tz(timeZone).utcOffset(), "minute");
const diffDays = startTime.startOf("day").diff(dayjs.utc(item.startTime).startOf("day"), "day");
return {
start: date.hour(startTime.hour()).minute(startTime.minute()).second(0).tz(timeZone),
end: date.hour(endTime.hour()).minute(endTime.minute()).second(0).tz(timeZone),
start: date.add(diffDays, "day").hour(startTime.hour()).minute(startTime.minute()).second(0).tz(timeZone),
end: date.add(diffDays, "day").hour(endTime.hour()).minute(endTime.minute()).second(0).tz(timeZone),
};
}
@ -105,7 +107,7 @@ export function groupByDate(ranges: DateRange[]): { [x: string]: DateRange[] } {
},
currentValue
) => {
const dateString = dayjs.utc(currentValue.start).format("YYYY-MM-DD");
const dateString = dayjs(currentValue.start).format("YYYY-MM-DD");
previousValue[dateString] =
typeof previousValue[dateString] === "undefined"