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.
This commit is contained in:
Dan Stillman 2020-09-09 23:33:10 -04:00
parent 4ac35ecda3
commit 0e74a91f6b
2 changed files with 26 additions and 10 deletions

View file

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

View file

@ -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"));
})