Skip to content

Commit

Permalink
Fix rrule bug regarding DST
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Nov 10, 2024
1 parent fa99147 commit 802c795
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/events.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RRule } from "rrule";
import { ProcessedEvent } from "./lib/types";
import { convertDateToRRuleDate } from "./lib/helpers/generals";

export const EVENTS: ProcessedEvent[] = [
{
Expand Down Expand Up @@ -116,8 +117,12 @@ export const EVENTS: ProcessedEvent[] = [
),
recurring: new RRule({
freq: RRule.WEEKLY,
dtstart: new Date(
new Date(new Date(new Date().setHours(10)).setMinutes(0)).setDate(new Date().getDate() + 1)
dtstart: convertDateToRRuleDate(
new Date(
new Date(new Date(new Date().setHours(10)).setMinutes(0)).setDate(
new Date().getDate() - 20
)
)
),
until: new Date(
new Date().setMonth(
Expand All @@ -139,7 +144,7 @@ export const EVENTS: ProcessedEvent[] = [
recurring: new RRule({
freq: RRule.HOURLY,
count: 3,
dtstart: new Date(new Date(new Date().setHours(14)).setMinutes(15)),
dtstart: convertDateToRRuleDate(new Date(new Date(new Date().setHours(14)).setMinutes(15))),
}),
color: "#dc4552",
},
Expand Down
45 changes: 34 additions & 11 deletions src/lib/helpers/generals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
SchedulerProps,
} from "../types";
import { StateEvent } from "../views/Editor";
import { datetime } from "rrule";

export const getOneView = (state: Partial<SchedulerProps>): View => {
if (state.month) {
Expand Down Expand Up @@ -125,19 +126,43 @@ export const differenceInDaysOmitTime = (start: Date, end: Date) => {
return differenceInDays(endOfDay(addSeconds(end, -1)), startOfDay(start));
};

export const getRecurrencesForDate = (event: ProcessedEvent, today: Date) => {
export const convertDateToRRuleDate = (date: Date) => {
return datetime(
date.getFullYear(),
date.getMonth() + 1,
date.getDate(),
date.getHours(),
date.getMinutes()
);
};

export const convertRRuleDateToDate = (rruleDate: Date) => {
return new Date(
rruleDate.getUTCFullYear(),
rruleDate.getUTCMonth(),
rruleDate.getUTCDate(),
rruleDate.getUTCHours(),
rruleDate.getUTCMinutes()
);
};

export const getRecurrencesForDate = (event: ProcessedEvent, today: Date, timeZone?: string) => {
const duration = differenceInMilliseconds(event.end, event.start);
if (event.recurring) {
return event.recurring
?.between(today, addDays(today, 1), true)
.map((d: Date, index: number) => ({
...event,
recurrenceId: index,
start: d,
end: addMilliseconds(d, duration),
}));
.map((d: Date, index: number) => {
const start = convertRRuleDateToDate(d);
return {
...event,
recurrenceId: index,
start: start,
end: addMilliseconds(start, duration),
};
})
.map((event) => convertEventTimeZone(event, timeZone));
}
return [event];
return [convertEventTimeZone(event, timeZone)];
};

export const filterTodayEvents = (
Expand All @@ -148,9 +173,7 @@ export const filterTodayEvents = (
const list: ProcessedEvent[] = [];

for (let i = 0; i < events.length; i++) {
const event = convertEventTimeZone(events[i], timeZone);

for (const rec of getRecurrencesForDate(event, today)) {
for (const rec of getRecurrencesForDate(events[i], today, timeZone)) {
const isToday =
!rec.allDay && isSameDay(today, rec.start) && !differenceInDaysOmitTime(rec.start, rec.end);
if (isToday) {
Expand Down

0 comments on commit 802c795

Please sign in to comment.