Skip to content

Commit

Permalink
fix mssing end with duration,add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
sdetweil committed Feb 22, 2024
1 parent b4008a7 commit 7c21f75
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 22 deletions.
39 changes: 18 additions & 21 deletions ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,33 +450,30 @@ module.exports = {
const par = stack.pop();

if (!curr.end) { // RFC5545, 3.6.1
if (curr.datetype === 'date-time') {
curr.end = new Date(curr.start.getTime());
// If the duration is not set
} else if (curr.duration === undefined) {
// Set the end to the start plus one day RFC5545, 3.6.1
curr.end = moment.utc(curr.start).add(1, 'days').toDate(); // New Date(moment(curr.start).add(1, 'days'));
} else {
// Set the end according to the datetype of event
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
if (curr.duration !== undefined) {
const durationUnits =
{
// Y: 'years',
// M: 'months',
W: 'weeks',
D: 'days',
H: 'hours',
M: 'minutes',
S: 'seconds'
};
{
// 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+[YMWDHS]/g);

// Use the duration to create the end value, from the start
let newend = moment.utc(curr.start);
// Is the 1st character a negative sign?
const indicator = curr.duration.startsWith('-') ? -1 : 1;
// Process each element
for (const d of r) {
newend = newend.add(Number.parseInt(d, 10) * indicator, durationUnits[d.slice(-1)]);
}

newend = newend.add(Number.parseInt(r, 10) * indicator, durationUnits[r.toString().slice(-1)]);
// End is a Date type, not moment
curr.end = newend.toDate();
}
}
Expand Down
33 changes: 32 additions & 1 deletion test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,38 @@ vows
}
}
}

},
'with test_date_time_duration.ics': {
topic() {
return ical.parseFile('./test/test_date_time_duration.ics');
},
'using an event containing a start date but no end, only duration': {
'topic'(events) {
return _.select(_.values(events), x => {
return x.summary === 'period test2';
})[0];
},
'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');
}
}
},
'with test_date_duration.ics': {
topic() {
return ical.parseFile('./test/test_date_duration.ics');
},
'using an event containing a start date but no end, only duration': {
'topic'(events) {
return _.select(_.values(events), x => {
return x.summary === 'period test2';
})[0];
},
'ends one week later'(event) {
assert.equal(event.start.toDateString(), new Date(2024, 1, 15).toDateString());
assert.equal(event.end.toDateString(), new Date(2024, 1, 22).toDateString());
}
}
}
})
.export(module);
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,38 @@ vows
assert.equal(topic.start.tz, 'Europe/Berlin');
}
}
},
'with test_date_time_duration.ics': {
topic() {
return ical.parseFile('./test/test_date_time_duration.ics');
},
'using an event containing a start date but no end, only duration': {
'topic'(events) {
return _.select(_.values(events), x => {
return x.summary === 'period test2';
})[0];
},
'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');
}
}
},
'with test_date_duration.ics': {
topic() {
return ical.parseFile('./test/test_date_duration.ics');
},
'using an event containing a start date but no end, only duration': {
'topic'(events) {
return _.select(_.values(events), x => {
return x.summary === 'period test2';
})[0];
},
'ends one week later'(event) {
assert.equal(event.start.toDateString(), new Date(2024, 1, 15).toDateString());
assert.equal(event.end.toDateString(), new Date(2024, 1, 22).toDateString());
}
}
}
})
.export(module);
18 changes: 18 additions & 0 deletions test/test_date_duration.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BEGIN:VCALENDAR
PRODID:-//Synology//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20240215T101655
LAST-MODIFIED:20240215T101655
DTSTAMP:20240215T101655
UID:20240215T101655[email protected]
SEQUENCE:2
SUMMARY:period test2
TRANSP:OPAQUE
DESCRIPTION:
LOCATION:Kriftel
X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-TITLE=Kriftel: geo:50.083558\,8.4693855
DTSTART:20240215
DURATION:PT1W
END:VEVENT
END:VCALENDAR
18 changes: 18 additions & 0 deletions test/test_date_time_duration.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BEGIN:VCALENDAR
PRODID:-//Synology//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20240215T101655
LAST-MODIFIED:20240215T101655
DTSTAMP:20240215T101655
UID:20240215T101655[email protected]
SEQUENCE:2
SUMMARY:period test2
TRANSP:OPAQUE
DESCRIPTION:
LOCATION:Kriftel
X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-TITLE=Kriftel: geo:50.083558\,8.4693855
DTSTART:20240215T090000Z
DURATION:PT15M
END:VEVENT
END:VCALENDAR

0 comments on commit 7c21f75

Please sign in to comment.