Skip to content

Fixed duration issue when having multiple time units #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,27 +458,37 @@ module.exports = {
curr.end = (curr.datetype === 'date-time') ? new Date(curr.start.getTime()) : moment.utc(curr.start).add(1, 'days').toDate();

// If there was a duration specified
// see RFC5545, 3.3.6 (no year and month)
if (curr.duration !== undefined) {
const durationUnits =
{
// Y: 'years',
// M: 'months',
W: 'weeks',
D: 'days',
H: 'hours',
M: 'minutes',
S: 'seconds'
};
// Get the list of duration elements
const r = curr.duration.match(/-?\d{1,10}[YMWDHS]/g);
const duration = curr.duration.match(/-?\d{1,10}[WDHMS]/g);

// Use the duration to create the end value, from the start
let newend = moment.utc(curr.start);
const startMoment = moment.utc(curr.start);
let newEnd = startMoment;

// Is the 1st character a negative sign?
const indicator = curr.duration.startsWith('-') ? -1 : 1;
newend = newend.add(Number.parseInt(r, 10) * indicator, durationUnits[r.toString().slice(-1)]);

for (const r of duration) {
const unit = r.slice(-1);
if (!durationUnits[unit]) {
throw new Error(`Invalid duration unit: ${unit}`);
}

newEnd = newEnd.add(Number.parseInt(r, 10) * indicator, durationUnits[r.toString().slice(-1)]);
}

// End is a Date type, not moment
curr.end = newend.toDate();
curr.end = newEnd.toDate();
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ vows
},
'it uses the start/end of the event'(event) {
assert.equal(event.start.toJSON(), '2024-02-15T09:00:00.000Z');
assert.equal(event.end.toJSON(), '2024-02-15T09:15:00.000Z');
assert.equal(event.end.toJSON(), '2024-02-15T10:15:00.000Z');
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ vows
},
'it uses the start/end of the event'(event) {
assert.equal(event.start.toJSON(), '2024-02-15T09:00:00.000Z');
assert.equal(event.end.toJSON(), '2024-02-15T09:15:00.000Z');
assert.equal(event.end.toJSON(), '2024-02-15T10:15:00.000Z');
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion test/test_date_time_duration.ics
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ DESCRIPTION:
LOCATION:Kriftel
X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-TITLE=Kriftel: geo:50.083558\,8.4693855
DTSTART:20240215T090000Z
DURATION:PT15M
DURATION:PT1H15M
END:VEVENT
END:VCALENDAR
Loading