diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 0ba43b1d3f..f1353e5408 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -1675,6 +1675,16 @@ Zotero.Utilities = { } if(date) { + // Convert UTC timestamp to local timestamp for access date + if (CSL_DATE_MAPPINGS[variable] == 'accessDate') { + // Accept ISO date + if (Zotero.Date.isISODate(date) && !Zotero.Date.isSQLDate(date)) { + let d = Zotero.Date.isoToDate(date); + date = Zotero.Date.dateToSQL(d, true); + } + let localDate = Zotero.Date.sqlToDate(date, true); + date = Zotero.Date.dateToSQL(localDate); + } var dateObj = Zotero.Date.strToDate(date); // otherwise, use date-parts var dateParts = []; diff --git a/test/tests/utilitiesTest.js b/test/tests/utilitiesTest.js index a12f665b47..0e0e3d1312 100644 --- a/test/tests/utilitiesTest.js +++ b/test/tests/utilitiesTest.js @@ -366,6 +366,31 @@ describe("Zotero.Utilities", function() { assert.deepEqual(cslCreators[4], creators[4].expect, 'institutional author is not parsed'); assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing'); }); + + it("should convert UTC access date to local time", async function () { + var offset = new Date().getTimezoneOffset(); + var item = new Zotero.Item('webpage'); + var localDate; + if (offset < 0) { + localDate = '2019-01-09 00:00:00'; + } + else if (offset > 0) { + localDate = '2019-01-09 23:59:59'; + } + // Can't test timezone offset if in UTC + else { + this.skip(); + return; + } + var utcDate = Zotero.Date.sqlToDate(localDate); + item.setField('accessDate', Zotero.Date.dateToSQL(utcDate, true)); + await item.saveTx(); + let accessed = Zotero.Utilities.itemToCSLJSON(item).accessed; + + assert.equal(accessed['date-parts'][0][0], 2019); + assert.equal(accessed['date-parts'][0][1], 1); + assert.equal(accessed['date-parts'][0][2], 9); + }); }); describe("itemFromCSLJSON", function () { it("should stably perform itemToCSLJSON -> itemFromCSLJSON -> itemToCSLJSON", function* () {