RRule datetime generation randomly wrong

I am making a project using google calendar. I am using the rrule library to generate dates for event times.
import { z } from "zod";
import { calendar_v3, google } from "googleapis";
import { DateTime } from "luxon";

//...

const rrule = RRule.fromString(event.recurrence[0].split(":")[1]!);
if (!event.start?.dateTime) return null;

const startDateTime = DateTime.fromISO(event.start.dateTime).setZone(
"UTC",
);

rrule.options.dtstart = startDateTime.toJSDate();
rrule.options.tzid = "UTC";
const utcStartTime = startDateTime.toJSDate();

rrule.options.bysecond = [utcStartTime.getUTCSeconds()]; // Never mentioned in the docs, works for some reason
rrule.options.byminute = [utcStartTime.getUTCMinutes()];
rrule.options.byhour = [utcStartTime.getUTCHours()];

const dates = rrule.all((d, i) => {
return i < 20;
});

console.log(dates.map((d) => d.toLocaleString("en-US"))); // log 1

const meetingModifications: calendar_v3.Schema$Event[] = [];
for (const date of dates) {
const eventModificationId = `${meetingLinkCheck.googleCalendarEventId}_${DateTime.fromJSDate(
date,
)
.setZone("UTC")
.toISO()
?.replaceAll("-", "")
.replaceAll(":", "")
.replaceAll(".", "")
.replace("000Z", "Z")}`;
try {
const eventModification = (
await calendar.events.get({
calendarId: "primary",
eventId: eventModificationId,
})
).data;
if (!eventModification) continue;
meetingModifications.push(eventModification);
} catch (error) {
console.log(eventModificationId, date.toLocaleString("en-US")); // log 2
continue;
}
}

meetingModifications.sort((a, b) => {
const dateTimeA = a.start?.dateTime ?? "";
const dateTimeB = b.start?.dateTime ?? "";
return dateTimeA.localeCompare(dateTimeB);
});
import { z } from "zod";
import { calendar_v3, google } from "googleapis";
import { DateTime } from "luxon";

//...

const rrule = RRule.fromString(event.recurrence[0].split(":")[1]!);
if (!event.start?.dateTime) return null;

const startDateTime = DateTime.fromISO(event.start.dateTime).setZone(
"UTC",
);

rrule.options.dtstart = startDateTime.toJSDate();
rrule.options.tzid = "UTC";
const utcStartTime = startDateTime.toJSDate();

rrule.options.bysecond = [utcStartTime.getUTCSeconds()]; // Never mentioned in the docs, works for some reason
rrule.options.byminute = [utcStartTime.getUTCMinutes()];
rrule.options.byhour = [utcStartTime.getUTCHours()];

const dates = rrule.all((d, i) => {
return i < 20;
});

console.log(dates.map((d) => d.toLocaleString("en-US"))); // log 1

const meetingModifications: calendar_v3.Schema$Event[] = [];
for (const date of dates) {
const eventModificationId = `${meetingLinkCheck.googleCalendarEventId}_${DateTime.fromJSDate(
date,
)
.setZone("UTC")
.toISO()
?.replaceAll("-", "")
.replaceAll(":", "")
.replaceAll(".", "")
.replace("000Z", "Z")}`;
try {
const eventModification = (
await calendar.events.get({
calendarId: "primary",
eventId: eventModificationId,
})
).data;
if (!eventModification) continue;
meetingModifications.push(eventModification);
} catch (error) {
console.log(eventModificationId, date.toLocaleString("en-US")); // log 2
continue;
}
}

meetingModifications.sort((a, b) => {
const dateTimeA = a.start?.dateTime ?? "";
const dateTimeB = b.start?.dateTime ?? "";
return dateTimeA.localeCompare(dateTimeB);
});
Solution:
its dts
Jump to solution
2 Replies
SharpieMaster
SharpieMasterOP5mo ago
// log 1
/*[
'7/4/2024, 4:30:00 AM', '8/1/2024, 4:30:00 AM',
'9/5/2024, 4:30:00 AM', '10/3/2024, 4:30:00 AM',
'11/7/2024, 3:30:00 AM', '12/5/2024, 3:30:00 AM',
'1/2/2025, 3:30:00 AM', '2/6/2025, 3:30:00 AM',
'3/6/2025, 3:30:00 AM', '4/3/2025, 4:30:00 AM',
'5/1/2025, 4:30:00 AM', '6/5/2025, 4:30:00 AM',
'7/3/2025, 4:30:00 AM', '8/7/2025, 4:30:00 AM',
'9/4/2025, 4:30:00 AM', '10/2/2025, 4:30:00 AM',
'11/6/2025, 3:30:00 AM', '12/4/2025, 3:30:00 AM',
'1/1/2026, 3:30:00 AM', '2/5/2026, 3:30:00 AM'
]*/
// log 1
/*[
'7/4/2024, 4:30:00 AM', '8/1/2024, 4:30:00 AM',
'9/5/2024, 4:30:00 AM', '10/3/2024, 4:30:00 AM',
'11/7/2024, 3:30:00 AM', '12/5/2024, 3:30:00 AM',
'1/2/2025, 3:30:00 AM', '2/6/2025, 3:30:00 AM',
'3/6/2025, 3:30:00 AM', '4/3/2025, 4:30:00 AM',
'5/1/2025, 4:30:00 AM', '6/5/2025, 4:30:00 AM',
'7/3/2025, 4:30:00 AM', '8/7/2025, 4:30:00 AM',
'9/4/2025, 4:30:00 AM', '10/2/2025, 4:30:00 AM',
'11/6/2025, 3:30:00 AM', '12/4/2025, 3:30:00 AM',
'1/1/2026, 3:30:00 AM', '2/5/2026, 3:30:00 AM'
]*/
Randomly 3:30 instead of 4:30 (correct time)
// log 2
/*
mccure6f57hh8aal0u57daof4k_20241107T113000Z 11/7/2024, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20241205T113000Z 12/5/2024, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20250102T113000Z 1/2/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20250206T113000Z 2/6/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20251106T113000Z 11/6/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20251204T113000Z 12/4/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20260101T113000Z 1/1/2026, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20260205T113000Z 2/5/2026, 3:30:00 AM
*/
// log 2
/*
mccure6f57hh8aal0u57daof4k_20241107T113000Z 11/7/2024, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20241205T113000Z 12/5/2024, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20250102T113000Z 1/2/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20250206T113000Z 2/6/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20251106T113000Z 11/6/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20251204T113000Z 12/4/2025, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20260101T113000Z 1/1/2026, 3:30:00 AM
mccure6f57hh8aal0u57daof4k_20260205T113000Z 2/5/2026, 3:30:00 AM
*/
Only the ones that are 3:30 are rejected
Solution
SharpieMaster
SharpieMaster5mo ago
its dts
Want results from more Discord servers?
Add your server