From 0e74a91f6ba7785bf2d63b42efe29d9374dffadc Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 9 Sep 2020 23:33:10 -0400 Subject: [PATCH] Fix parsing of SQL dates without seconds Previously, "2020-09-09 23:33" would be treated as a multipart date, with "23:33" left in the visible field and "d" showing in the indicator. --- chrome/content/zotero/xpcom/date.js | 29 +++++++++++++++++++---------- test/tests/dateTest.js | 7 +++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/chrome/content/zotero/xpcom/date.js b/chrome/content/zotero/xpcom/date.js index 9cec2bd04d..8caed47bc4 100644 --- a/chrome/content/zotero/xpcom/date.js +++ b/chrome/content/zotero/xpcom/date.js @@ -24,9 +24,6 @@ */ Zotero.Date = new function(){ - this.isMultipart = isMultipart; - this.multipartToSQL = multipartToSQL; - this.multipartToStr = multipartToStr; this.isSQLDate = isSQLDate; this.isSQLDateTime = isSQLDateTime; this.sqlHasYear = sqlHasYear; @@ -115,7 +112,9 @@ Zotero.Date = new function(){ **/ this.sqlToDate = function (sqldate, isUTC) { try { - if (!this.isSQLDate(sqldate) && !this.isSQLDateTime(sqldate)) { + if (!this.isSQLDate(sqldate) + && !this.isSQLDateTime(sqldate) + && !this.isSQLDateTimeWithoutSeconds(sqldate)) { throw new Error("Invalid date"); } @@ -132,6 +131,10 @@ Zotero.Date = new function(){ if (dateparts.length==1){ throw new Error("Invalid date part"); } + // Allow missing seconds + if (timeparts.length == 2) { + timeparts[2] = '00'; + } if (isUTC){ return new Date(Date.UTC(dateparts[0], dateparts[1]-1, dateparts[2], @@ -663,13 +666,14 @@ Zotero.Date = new function(){ var _sqldateRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31)$/; var _sqldateWithZeroesRE = /^\-?[0-9]{4}\-(0[0-9]|10|11|12)\-(0[0-9]|[1-2][0-9]|30|31)$/; var _sqldatetimeRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31) ([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/; + var _sqlDateTimeWithoutSecondsRE = /^\-?[0-9]{4}\-(0[1-9]|10|11|12)\-(0[1-9]|[1-2][0-9]|30|31) ([0-1][0-9]|[2][0-3]):([0-5][0-9])$/; /** * Tests if a string is a multipart date string * e.g. '2006-11-03 November 3rd, 2006' */ - function isMultipart(str){ - if (isSQLDateTime(str)) { + this.isMultipart = function (str) { + if (this.isSQLDateTime(str) || this.isSQLDateTimeWithoutSeconds(str)) { return false; } return _multipartRE.test(str); @@ -680,12 +684,12 @@ Zotero.Date = new function(){ * Returns the SQL part of a multipart date string * (e.g. '2006-11-03 November 3rd, 2006' returns '2006-11-03') */ - function multipartToSQL(multi){ + this.multipartToSQL = function (multi) { if (!multi){ return ''; } - if (!isMultipart(multi)){ + if (!this.isMultipart(multi)) { return '0000-00-00'; } @@ -697,12 +701,12 @@ Zotero.Date = new function(){ * Returns the user part of a multipart date string * (e.g. '2006-11-03 November 3rd, 2006' returns 'November 3rd, 2006') */ - function multipartToStr(multi){ + this.multipartToStr = function (multi) { if (!multi){ return ''; } - if (!isMultipart(multi)){ + if (!this.isMultipart(multi)) { return multi; } @@ -746,6 +750,11 @@ Zotero.Date = new function(){ } + this.isSQLDateTimeWithoutSeconds = function (str) { + return _sqlDateTimeWithoutSecondsRE.test(str); + } + + function sqlHasYear(sqldate){ return isSQLDate(sqldate, true) && sqldate.substr(0,4)!='0000'; } diff --git a/test/tests/dateTest.js b/test/tests/dateTest.js index cbe969db65..5d6ba7520c 100644 --- a/test/tests/dateTest.js +++ b/test/tests/dateTest.js @@ -143,6 +143,12 @@ describe("Zotero.Date", function() { var date = "2016-02-27 22:00:00"; date = Zotero.Date.sqlToDate(date, true); assert.equal(date.getTime(), 1456610400000); + }); + + it("should convert an SQL UTC date without seconds into a JS Date object", function () { + var date = "2016-02-27 22:00"; + date = Zotero.Date.sqlToDate(date, true); + assert.equal(date.getTime(), 1456610400000); }) }) @@ -151,6 +157,7 @@ describe("Zotero.Date", function() { assert.ok(Zotero.Date.isISODate("2015")); assert.ok(Zotero.Date.isISODate("2015-04")); assert.ok(Zotero.Date.isISODate("2015-04-29")); + assert.ok(Zotero.Date.isISODate("2015-04-29T17:28")); assert.ok(Zotero.Date.isISODate("2015-04-29T17:28Z")); assert.isFalse(Zotero.Date.isISODate("2015-04-29 17:28")); })