Skip to content

Commit

Permalink
Merge branch 'jens-maus:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnytest1 authored Feb 28, 2024
2 parents 0b8bf09 + 6a5175e commit 87e3b6f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 19 deletions.
21 changes: 9 additions & 12 deletions ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,11 @@ 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',
Expand All @@ -469,14 +467,13 @@ module.exports = {
};
// 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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"url": "git://github.com/jens-maus/node-ical.git"
},
"dependencies": {
"axios": "1.6.3",
"axios": "1.6.7",
"moment-timezone": "^0.5.44",
"rrule": "2.8.1",
"uuid": "^9.0.0"
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 @@ -990,6 +990,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 87e3b6f

Please sign in to comment.