From 8a432b45a48d1112b2bd8a05c9776cca51db542b Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Wed, 19 Jul 2023 14:44:41 -0400 Subject: [PATCH] fix: issue with date overrides in some timezones (#10254) * fix date overrides * add test --------- Co-authored-by: CarinaWolli --- packages/lib/date-ranges.test.ts | 29 +++++++++++++++++++++++++++++ packages/lib/date-ranges.ts | 8 +++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/lib/date-ranges.test.ts b/packages/lib/date-ranges.test.ts index 1973f8e883..237de2dfde 100644 --- a/packages/lib/date-ranges.test.ts +++ b/packages/lib/date-ranges.test.ts @@ -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", () => { diff --git a/packages/lib/date-ranges.ts b/packages/lib/date-ranges.ts index 5580a6e2fb..878ba8963e 100644 --- a/packages/lib/date-ranges.ts +++ b/packages/lib/date-ranges.ts @@ -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"