From a8d4b84c6cf3ec498d861c05048b1853511fb0ec Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 6 Jun 2019 14:09:23 +0200 Subject: [PATCH] ICAL.Event duration should take timezones into account Signed-off-by: Georg Ehrke --- lib/ical/event.js | 2 +- test/event_test.js | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/ical/event.js b/lib/ical/event.js index 009f3b2d..107a16fc 100644 --- a/lib/ical/event.js +++ b/lib/ical/event.js @@ -391,7 +391,7 @@ ICAL.Event = (function() { get duration() { var duration = this._firstProp('duration'); if (!duration) { - return this.endDate.subtractDate(this.startDate); + return this.endDate.subtractDateTz(this.startDate); } return duration; }, diff --git a/test/event_test.js b/test/event_test.js index 965ff228..0a5254fd 100644 --- a/test/event_test.js +++ b/test/event_test.js @@ -2,7 +2,7 @@ suite('ICAL.Event', function() { var testTzid = 'America/New_York'; - testSupport.useTimezones(testTzid, 'America/Denver'); + testSupport.useTimezones(testTzid, 'America/Denver', 'America/Los_Angeles'); var icsData; @@ -867,7 +867,7 @@ suite('ICAL.Event', function() { day: 2, hour: 6, isDate: false, - timezone: testTzid + timezone: subject.startDate.zone }); assert.equal(subject.duration.toString(), 'P2D'); @@ -956,6 +956,49 @@ suite('ICAL.Event', function() { icsData = data; }); + test('result with different timezones', function() { + var subject = (new ICAL.Component(ICAL.parse(icsData))) + .getFirstSubcomponent('vevent'); + // 3 hours ahead of L.A. + subject.updatePropertyWithValue('dtstart', ICAL.Time.fromData({ + year: 2012, + month: 1, + day: 1, + hour: 10, + minute: 20, + timezone: 'America/New_York' + })); + subject.updatePropertyWithValue('dtend', ICAL.Time.fromData({ + year: 2012, + month: 1, + day: 1, + hour: 12, + minute: 50, + timezone: 'America/Los_Angeles' + })); + + subject = new ICAL.Event(subject); + assert.equal(subject.startDate.toString(), ICAL.Time.fromData({ + year: 2012, + month: 1, + day: 1, + hour: 10, + minute: 20, + timezone: 'America/New_York', + }).toString()); + + assert.equal(subject.endDate.toString(), ICAL.Time.fromData({ + year: 2012, + month: 1, + day: 1, + hour: 12, + minute: 50, + timezone: 'America/Los_Angeles' + }).toString()); + + assert.equal(subject.duration.toString(), 'PT5H30M'); + }) + test('set', function() { var subject = new ICAL.Component(ICAL.parse(icsData)); subject = new ICAL.Event(subject.getFirstSubcomponent('vevent'));