Skip to content

Commit

Permalink
Fixed duration issue when having multiple time units
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterlemcke committed Oct 28, 2024
1 parent 7dd8e11 commit b66b57b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,19 @@ module.exports = {
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}[YMWDHS]/g);

// Use the duration to create the end value, from the start
let newend = moment.utc(curr.start);
let newEnd = moment.utc(curr.start);
// 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) {
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

0 comments on commit b66b57b

Please sign in to comment.