From ab62fc5d47919ddd022f8a29ed066f7bbb75dc9a Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Fri, 7 Oct 2022 23:10:48 +0100 Subject: [PATCH] Bugfix/dst available hour mismatch (#4909) * Need to use local time, not inviteeDate * Use selected timeZone to perform localWorkingHours logic * Adding comment * More comments --- packages/lib/slots.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/lib/slots.ts b/packages/lib/slots.ts index 358bf0932a..6385ba0b93 100644 --- a/packages/lib/slots.ts +++ b/packages/lib/slots.ts @@ -42,7 +42,9 @@ const splitAvailableTime = ( const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours, eventLength }: GetSlots) => { // current date in invitee tz const startDate = dayjs().add(minimumBookingNotice, "minute"); - const startOfDayUTC = dayjs.utc().startOf("day"); + // This code is ran client side, startOf() does some conversions based on the + // local tz of the client. Sometimes this shifts the day incorrectly. + const startOfDayUTC = dayjs.utc().set("hour", 0).set("minute", 0).set("second", 0); const startOfInviteeDay = inviteeDate.startOf("day"); // checks if the start date is in the past @@ -61,9 +63,20 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours, startTime: /* Why? */ startOfDayUTC.add(schedule.startTime, "minute"), endTime: /* Why? */ startOfDayUTC.add(schedule.endTime, "minute"), })); - const localWorkingHours = getWorkingHours({ utcOffset: -inviteeDate.utcOffset() }, workingHoursUTC).filter( - (hours) => hours.days.includes(inviteeDate.day()) - ); + + // Dayjs does not expose the timeZone value publicly through .get("timeZone") + // instead, we as devs are required to somewhat hack our way to get the ... + // tz value as string + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const timeZone: string = (inviteeDate as any)["$x"]["$timezone"]; + + const localWorkingHours = getWorkingHours( + { + // initialize current day with timeZone without conversion, just parse. + utcOffset: -dayjs.tz(dayjs(), timeZone).utcOffset(), + }, + workingHoursUTC + ).filter((hours) => hours.days.includes(inviteeDate.day())); const slots: Dayjs[] = [];