From 8963df8bbb62ed37e091daf51c9b31dbb64c753c Mon Sep 17 00:00:00 2001 From: Adrien Dorsaz Date: Fri, 18 Aug 2017 23:22:39 +1200 Subject: [PATCH] mivExchangeCalendar.js: getItemsFromMemoryCache: fix looking for items in cache The code before were using simple day index to go through start and end of the range. Furthermore, it didn't managed that range were given in local timezone and memory cache is in UTC. This fix use Lightning tools to go through range and it converts local timezone to UTC, so the cache is correctly read. Fixes #67 --- .../exchangeCalendar/mivExchangeCalendar.js | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/interfaces/exchangeCalendar/mivExchangeCalendar.js b/interfaces/exchangeCalendar/mivExchangeCalendar.js index f2de5d32..7cc64e8f 100644 --- a/interfaces/exchangeCalendar/mivExchangeCalendar.js +++ b/interfaces/exchangeCalendar/mivExchangeCalendar.js @@ -2842,30 +2842,31 @@ calExchangeCalendar.prototype = { // This is by using the this.itemCacheByStartDate and this.itemCacheByEndDate index. if (wantEvents) { - - var startYear = aRangeStart.year; - var startYearday = aRangeStart.yearday; - - var endYear = aRangeEnd.year; - var endYearday = aRangeEnd.yearday; - var doStop = false; - var ids = {}; - while (!doStop) { - if ((startYear == endYear) && (startYearday == endYearday)) { - doStop = true; - } - - if ((this.itemCacheByStartDate) && (this.itemCacheByStartDate[startYear]) && (this.itemCacheByStartDate[startYear][startYearday])) { - for (var itemid in this.itemCacheByStartDate[startYear][startYearday]) { + let ids = {}; + + let dayOffset = cal.createDuration(); + dayOffset.days = 1; + + let dayPos = cal.createDateTime(); + dayPos = aRangeStart.clone(); + + // Convert date from local timezone to UTC + // Given range is in local timezone and cache save in UTC + dayPos.isDate = false; + dayPos = dayPos.getInTimezone(cal.UTC()); + dayPos.isDate = true; + + // Go through all days bewteen the range start day and the range end day + while (dayPos.compare(aRangeEnd) < 0 ) { + if (this.itemCacheByStartDate + && this.itemCacheByStartDate[dayPos.year] + && this.itemCacheByStartDate[dayPos.year][dayPos.yearday]) { + for (let itemid in this.itemCacheByStartDate[dayPos.year][dayPos.yearday]) { ids[itemid] = true; } } - startYearday++; - if (startYearday > 366) { - startYear++; - startYearday = 1; - } + dayPos.addDuration(dayOffset); } // For all found ids, if item cache has it, push it to events answer