diff --git a/chrome/content/zotero/xpcom/date.js b/chrome/content/zotero/xpcom/date.js index 189ad0b790..91fb8018ba 100644 --- a/chrome/content/zotero/xpcom/date.js +++ b/chrome/content/zotero/xpcom/date.js @@ -315,6 +315,7 @@ Zotero.Date = new function(){ date.order += 'y'; } + var zeroYear = date.year && date.year.toString().startsWith('0'); if(date.year) date.year = parseInt(date.year, 10); if(date.day) date.day = parseInt(date.day, 10); if(date.month) { @@ -333,8 +334,8 @@ Zotero.Date = new function(){ } if((!date.month || date.month <= 12) && (!date.day || date.day <= 31)) { - if(date.year && date.year < 100) { // for two digit years, determine proper - // four digit year + // For two digit years, determine proper four-digit year + if (date.year && date.year < 100 && !zeroYear) { var today = new Date(); var year = today.getFullYear(); var twoDigitYear = year % 100; diff --git a/test/tests/dateTest.js b/test/tests/dateTest.js index eb7eec8036..c756782fdf 100644 --- a/test/tests/dateTest.js +++ b/test/tests/dateTest.js @@ -182,6 +182,40 @@ describe("Zotero.Date", function() { assert.notProperty(o, 'year'); }); + it("should parse two- and three-digit dates with leading zeros", function () { + var o = Zotero.Date.strToDate('0068'); + assert.equal(o.year, 68); + + o = Zotero.Date.strToDate('068'); + assert.equal(o.year, 68); + + o = Zotero.Date.strToDate('0168'); + assert.equal(o.year, 168); + }); + + it("should parse two-digit year greater than current year as previous century", function () { + var o = Zotero.Date.strToDate('1/1/68'); + assert.equal(o.year, 1968); + }); + + it("should parse two-digit year less than or equal to current year as current century", function () { + var o = Zotero.Date.strToDate('1/1/19'); + assert.equal(o.year, 2019); + }); + + it("should parse string with just month number", function () { + var o = Zotero.Date.strToDate('1'); + assert.equal(o.month, 0); + assert.isUndefined(o.year); + }); + + it("should parse string with just day number", function () { + var o = Zotero.Date.strToDate('25'); + assert.equal(o.day, 25); + assert.isUndefined(o.month); + assert.isUndefined(o.year); + }); + it("should work in translator sandbox", function* () { var item = createUnsavedDataObject('item'); item.libraryID = Zotero.Libraries.userLibraryID;