Fix parsing of pre-1000 years

If a leading zero (e.g., '068'), parse as actual year instead of
19xx/20xx.
This commit is contained in:
Dan Stillman 2019-12-19 02:13:57 -05:00
parent 360f034b51
commit b7dc0d8b0a
2 changed files with 37 additions and 2 deletions

View file

@ -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;

View file

@ -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;