2023-02-16 22:39:57 +00:00
|
|
|
import type { I18n } from "next-i18next";
|
2022-05-05 21:16:25 +00:00
|
|
|
import { RRule } from "rrule";
|
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-03-22 10:15:16 +00:00
|
|
|
import type { TimeFormat } from "@calcom/lib/timeFormat";
|
|
|
|
import { detectBrowserTimeFormat } from "@calcom/lib/timeFormat";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { RecurringEvent } from "@calcom/types/Calendar";
|
2022-04-14 21:25:24 +00:00
|
|
|
|
|
|
|
import { parseZone } from "./parseZone";
|
|
|
|
|
2023-03-22 10:15:16 +00:00
|
|
|
const processDate = (date: string | null | Dayjs, i18n: I18n, selectedTimeFormat?: TimeFormat) => {
|
2022-04-14 21:25:24 +00:00
|
|
|
const parsedZone = parseZone(date);
|
|
|
|
if (!parsedZone?.isValid()) return "Invalid date";
|
2023-03-22 10:15:16 +00:00
|
|
|
const formattedTime = parsedZone?.format(selectedTimeFormat || detectBrowserTimeFormat);
|
2022-04-14 21:25:24 +00:00
|
|
|
return formattedTime + ", " + dayjs(date).toDate().toLocaleString(i18n.language, { dateStyle: "full" });
|
|
|
|
};
|
2022-05-05 21:16:25 +00:00
|
|
|
|
2023-03-22 10:15:16 +00:00
|
|
|
export const parseDate = (date: string | null | Dayjs, i18n: I18n, selectedTimeFormat?: TimeFormat) => {
|
2022-05-05 21:16:25 +00:00
|
|
|
if (!date) return ["No date"];
|
2023-03-22 10:15:16 +00:00
|
|
|
return processDate(date, i18n, selectedTimeFormat);
|
2022-05-05 21:16:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const parseRecurringDates = (
|
|
|
|
{
|
|
|
|
startDate,
|
2022-07-07 01:08:38 +00:00
|
|
|
timeZone,
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringEvent,
|
|
|
|
recurringCount,
|
2023-03-22 10:15:16 +00:00
|
|
|
selectedTimeFormat,
|
2022-07-07 01:08:38 +00:00
|
|
|
}: {
|
|
|
|
startDate: string | null | Dayjs;
|
|
|
|
timeZone?: string;
|
|
|
|
recurringEvent: RecurringEvent | null;
|
|
|
|
recurringCount: number;
|
2023-03-22 10:15:16 +00:00
|
|
|
selectedTimeFormat?: TimeFormat;
|
2022-07-07 01:08:38 +00:00
|
|
|
},
|
2022-05-05 21:16:25 +00:00
|
|
|
i18n: I18n
|
|
|
|
): [string[], Date[]] => {
|
2022-05-17 20:43:27 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-06-10 00:32:34 +00:00
|
|
|
const { count, ...restRecurringEvent } = recurringEvent || {};
|
2022-05-05 21:16:25 +00:00
|
|
|
const rule = new RRule({
|
|
|
|
...restRecurringEvent,
|
|
|
|
count: recurringCount,
|
2022-11-04 14:59:38 +00:00
|
|
|
dtstart: new Date(dayjs(startDate).valueOf()),
|
|
|
|
});
|
|
|
|
|
|
|
|
const startUtcOffset = dayjs(startDate).utcOffset();
|
|
|
|
// UTC still need to have DST applied, rrule does not do this.
|
|
|
|
const times = rule.all().map((t) => {
|
|
|
|
// applying the DST offset.
|
|
|
|
return dayjs.utc(t).add(startUtcOffset - dayjs(t).utcOffset(), "minute");
|
2022-05-05 21:16:25 +00:00
|
|
|
});
|
2022-10-24 22:37:55 +00:00
|
|
|
const dateStrings = times.map((t) => {
|
2022-11-04 14:59:38 +00:00
|
|
|
// finally; show in local timeZone again
|
2023-03-22 10:15:16 +00:00
|
|
|
return processDate(t.tz(timeZone), i18n, selectedTimeFormat);
|
2022-05-05 21:16:25 +00:00
|
|
|
});
|
2022-10-24 22:37:55 +00:00
|
|
|
|
2022-11-04 14:59:38 +00:00
|
|
|
return [dateStrings, times.map((t) => t.toDate())];
|
2022-05-05 21:16:25 +00:00
|
|
|
};
|