Skip to content

Commit dd51527

Browse files
authored
fix: give each recurring instance the nominal length of its DURATION (#606)
* fix: give each recurring instance the nominal length of its DURATION expandRecurringEvent() gave every instance of a timed event the length of its first instance in milliseconds. With DURATION:P1D and a TZID, an instance that crosses a DST change, or any instance after a first one that crossed it, ended an hour off its start's wall-clock time. RFC 5545 section 3.8.5.3 applies the same nominal duration to every instance when the length comes from DURATION, so the end of each instance is now its start plus the DURATION, the same way the event's own end is computed. Events with DTEND keep their exact duration. The expandOngoing search window gets a day more for these events, since a later instance can be longer than the first. * fix: only widen the expandOngoing search for nominal durations * docs: say when an event with both DTEND and DURATION keeps its exact length
1 parent 0834bb8 commit dd51527

3 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎lib/expand-recurring-event.js‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import {getDateKey, localDate, utcDateMs} from './date-utils.js';
2+
import {applyDurationToDate, cloneDateWithMeta, getDurationString} from './ical-parser-utils.js';
3+
import tzUtil from './tz-utils.js';
4+
5+
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
26

37
// Shared recurring expansion implementation used by both CJS and ESM entrypoints.
48

@@ -111,6 +115,26 @@ function getEventDurationMs(eventData, isFullDay) {
111115
return eventData?.start && eventData.end ? new Date(eventData.end).getTime() - new Date(eventData.start).getTime() : 0;
112116
}
113117

118+
/**
119+
* Get the end of a timed instance from the event's DURATION. RFC 5545 §3.8.5.3 gives every
120+
* instance the same nominal duration, so days and weeks keep the wall-clock time of the
121+
* instance's own start across a DST change, as they do for the event's end.
122+
* @param {Date} start - The start time of this specific instance
123+
* @param {object} eventData - The event data (original or override)
124+
* @returns {Date|undefined} End time, or undefined when the event's end does not come from a DURATION
125+
*/
126+
function getDurationEndTime(start, eventData) {
127+
if (eventData?.duration === undefined || !(eventData.start instanceof Date) || !(eventData.end instanceof Date)) {
128+
return undefined;
129+
}
130+
131+
const durationString = getDurationString(eventData.duration);
132+
// An event with both DTEND and DURATION (not allowed by RFC 5545) keeps its exact length
133+
// when the two disagree; when they agree, the DURATION is used like for any other event
134+
const isEndFromDuration = applyDurationToDate(eventData.start, durationString, tzUtil.utcAdd)?.getTime() === eventData.end.getTime();
135+
return isEndFromDuration ? applyDurationToDate(cloneDateWithMeta(eventData.start, start), durationString, tzUtil.utcAdd) : undefined;
136+
}
137+
114138
/**
115139
* Calculate end time for an event instance.
116140
* @param {Date} start - The start time of this specific instance
@@ -128,6 +152,11 @@ function calculateEndTime(start, eventData, isFullDay, baseDurationMs) {
128152
return localDate(start.getFullYear(), start.getMonth(), start.getDate() + daySpan, 0, 0, 0, 0);
129153
}
130154

155+
const durationEnd = getDurationEndTime(start, eventData);
156+
if (durationEnd) {
157+
return durationEnd;
158+
}
159+
131160
const durationMs = (eventData?.start && eventData.end)
132161
? getEventDurationMs(eventData, isFullDay)
133162
: (baseDurationMs ?? 0);
@@ -467,7 +496,13 @@ export default function expandRecurringEvent(event, options) {
467496

468497
const isFullDay = event.datetype === 'date' || Boolean(event.start?.dateOnly);
469498
const baseDurationMs = getEventDurationMs(event, isFullDay);
470-
const {searchFrom, searchTo} = adjustSearchRange(from, to, isFullDay, expandOngoing, baseDurationMs);
499+
// Days and weeks in a DURATION are nominal, so a later instance can be longer than the first
500+
// one across a DST change. Exact units (hours, minutes, seconds) never change length.
501+
const hasNominalDuration = !isFullDay
502+
&& /\d[dw]/iv.test(getDurationString(event.duration))
503+
&& getDurationEndTime(event.start, event) !== undefined;
504+
const searchDurationMs = hasNominalDuration ? baseDurationMs + ONE_DAY_MS : baseDurationMs;
505+
const {searchFrom, searchTo} = adjustSearchRange(from, to, isFullDay, expandOngoing, searchDurationMs);
471506
const dates = withMaxIterations(event.rrule, maxIterations).between(searchFrom, searchTo, true);
472507
const instances = [];
473508
const seenRecurrenceKeys = new Set();

‎lib/ical-parser-utils.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ function createExdateParameterFactory({dateParameter, getDateKey}) {
10371037

10381038
export {
10391039
text,
1040+
applyDurationToDate,
10401041
normalizeRruleValue,
10411042
PARAMETER_VALUE,
10421043
parseValue,

‎test/expand-recurring-event.test.js‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,129 @@ describe('expandRecurringEvent', () => {
847847
assert.strictEqual(duration, 4_500_000, 'Duration should be 1h 15min (4_500_000ms)');
848848
});
849849

850+
it('should apply a DURATION in days to each instance as a nominal duration across DST', () => {
851+
// RFC 5545 §3.8.5.3: with DURATION, every instance gets the same nominal duration,
852+
// so P1D ends at the wall-clock time of its start, however long that day is.
853+
// With DTEND, every instance gets the same exact duration instead.
854+
const parse = (dtstart, length) => Object.values(ical.parseICS([
855+
'BEGIN:VCALENDAR',
856+
'VERSION:2.0',
857+
'PRODID:-//Test//DURATION across DST//EN',
858+
'BEGIN:VEVENT',
859+
'UID:duration-nominal-dst@node-ical-test',
860+
'DTSTAMP:20250101T000000Z',
861+
dtstart,
862+
length,
863+
'RRULE:FREQ=DAILY;COUNT=4',
864+
'END:VEVENT',
865+
'END:VCALENDAR',
866+
].join('\r\n'))).find(event => event.type === 'VEVENT');
867+
const ends = event => ical.expandRecurringEvent(event, {
868+
from: new Date('2025-03-01T00:00:00Z'),
869+
to: new Date('2025-11-30T00:00:00Z'),
870+
}).map(instance => instance.end.toISOString());
871+
872+
// Europe/Berlin: DST starts 2025-03-30, ends 2025-10-26. Every instance ends at 10:00 local.
873+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20250328T100000', 'DURATION:P1D')), [
874+
'2025-03-29T09:00:00.000Z',
875+
'2025-03-30T08:00:00.000Z',
876+
'2025-03-31T08:00:00.000Z',
877+
'2025-04-01T08:00:00.000Z',
878+
]);
879+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20251024T100000', 'DURATION:P1DT2H')), [
880+
'2025-10-25T10:00:00.000Z',
881+
'2025-10-26T11:00:00.000Z',
882+
'2025-10-27T11:00:00.000Z',
883+
'2025-10-28T11:00:00.000Z',
884+
]);
885+
// The first instance crosses the change, the later ones must not inherit its 23 hours
886+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20250329T100000', 'DURATION:P1W')), [
887+
'2025-04-05T08:00:00.000Z',
888+
'2025-04-06T08:00:00.000Z',
889+
'2025-04-07T08:00:00.000Z',
890+
'2025-04-08T08:00:00.000Z',
891+
]);
892+
893+
// Exact durations keep their length
894+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20250328T100000', 'DURATION:PT24H')), [
895+
'2025-03-29T09:00:00.000Z',
896+
'2025-03-30T09:00:00.000Z',
897+
'2025-03-31T08:00:00.000Z',
898+
'2025-04-01T08:00:00.000Z',
899+
]);
900+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20250328T100000', 'DTEND;TZID=Europe/Berlin:20250329T100000')), [
901+
'2025-03-29T09:00:00.000Z',
902+
'2025-03-30T09:00:00.000Z',
903+
'2025-03-31T08:00:00.000Z',
904+
'2025-04-01T08:00:00.000Z',
905+
]);
906+
// DTEND wins over a DURATION next to it
907+
assert.deepStrictEqual(ends(parse('DTSTART;TZID=Europe/Berlin:20250328T100000', 'DTEND;TZID=Europe/Berlin:20250329T100000\r\nDURATION:P2D')), [
908+
'2025-03-29T09:00:00.000Z',
909+
'2025-03-30T09:00:00.000Z',
910+
'2025-03-31T08:00:00.000Z',
911+
'2025-04-01T08:00:00.000Z',
912+
]);
913+
});
914+
915+
it('should include an ongoing instance whose nominal DURATION is longer than the first one', () => {
916+
const event = Object.values(ical.parseICS([
917+
'BEGIN:VCALENDAR',
918+
'VERSION:2.0',
919+
'PRODID:-//Test//DURATION ongoing across DST//EN',
920+
'BEGIN:VEVENT',
921+
'UID:duration-ongoing-dst@node-ical-test',
922+
'DTSTAMP:20250101T000000Z',
923+
'DTSTART;TZID=Europe/Berlin:20251018T100000',
924+
'DURATION:P1D',
925+
'RRULE:FREQ=WEEKLY;COUNT=2',
926+
'END:VEVENT',
927+
'END:VCALENDAR',
928+
].join('\r\n'))).find(event => event.type === 'VEVENT');
929+
930+
// The 2025-10-25 instance runs 25 hours, until 2025-10-26 10:00 CET (09:00Z)
931+
const instances = ical.expandRecurringEvent(event, {
932+
from: new Date('2025-10-26T08:30:00Z'),
933+
to: new Date('2025-10-26T08:45:00Z'),
934+
expandOngoing: true,
935+
});
936+
937+
assert.deepStrictEqual(instances.map(instance => [instance.start.toISOString(), instance.end.toISOString()]), [
938+
['2025-10-25T08:00:00.000Z', '2025-10-26T09:00:00.000Z'],
939+
]);
940+
});
941+
942+
it('should not widen the search for an exact DURATION', () => {
943+
const event = Object.values(ical.parseICS([
944+
'BEGIN:VCALENDAR',
945+
'VERSION:2.0',
946+
'PRODID:-//Test//Exact DURATION search//EN',
947+
'BEGIN:VEVENT',
948+
'UID:duration-exact-search@node-ical-test',
949+
'DTSTAMP:20250101T000000Z',
950+
'DTSTART:20250101T000000Z',
951+
'DURATION:PT1S',
952+
'RRULE:FREQ=SECONDLY',
953+
'END:VEVENT',
954+
'END:VCALENDAR',
955+
].join('\r\n'))).find(event => event.type === 'VEVENT');
956+
957+
// Seconds never change length, so only the instances around the window are generated,
958+
// not a whole extra day of them.
959+
const instances = ical.expandRecurringEvent(event, {
960+
from: new Date('2025-01-01T12:00:00Z'),
961+
to: new Date('2025-01-01T12:00:01Z'),
962+
expandOngoing: true,
963+
maxIterations: 1000,
964+
});
965+
966+
assert.deepStrictEqual(instances.map(instance => instance.start.toISOString()), [
967+
'2025-01-01T11:59:59.000Z',
968+
'2025-01-01T12:00:00.000Z',
969+
'2025-01-01T12:00:01.000Z',
970+
]);
971+
});
972+
850973
it('should use event.end when available', () => {
851974
const events = ical.sync.parseFile(path.join(__dirname, 'fixtures', 'multi-event-basic.ics'));
852975
const event = Object.values(events).find(event => event.type === 'VEVENT' && event.end);

0 commit comments

Comments
 (0)