commit 28eaaaf2bf5e0e268efb32e16a38aeb42766ee1a
parent edcd2a16d29077b8578d16f616c1ae9a4a4620b0
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 11 Mar 2016 03:16:24 -0500
Don't try to parse non-SQL dates in Date.sqlToDate()
Diffstat:
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/chrome/content/zotero/xpcom/date.js b/chrome/content/zotero/xpcom/date.js
@@ -87,6 +87,10 @@ Zotero.Date = new function(){
**/
function sqlToDate(sqldate, isUTC){
try {
+ if (!this.isSQLDate(sqldate) && !this.isSQLDateTime(sqldate)) {
+ throw new Error("Invalid date");
+ }
+
var datetime = sqldate.split(' ');
var dateparts = datetime[0].split('-');
if (datetime[1]){
@@ -98,7 +102,7 @@ Zotero.Date = new function(){
// Invalid date part
if (dateparts.length==1){
- return false;
+ throw new Error("Invalid date part");
}
if (isUTC){
@@ -699,7 +703,7 @@ Zotero.Date = new function(){
function toUnixTimestamp(date) {
if (date === null || typeof date != 'object' ||
date.constructor.name != 'Date') {
- throw ('Not a valid date in Zotero.Date.toUnixTimestamp()');
+ throw new Error(`'${date}' is not a valid date`);
}
return Math.round(date.getTime() / 1000);
}
diff --git a/test/tests/dateTest.js b/test/tests/dateTest.js
@@ -1,4 +1,19 @@
describe("Zotero.Date", function() {
+ describe("#sqlToDate()", function () {
+ it("should convert an SQL local date into a JS Date object", function* () {
+ var date = "2016-02-27 22:00:00";
+ var offset = new Date().getTimezoneOffset() * 60 * 1000;
+ date = Zotero.Date.sqlToDate(date);
+ assert.equal(date.getTime(), 1456610400000 + offset);
+ })
+
+ it("should convert an SQL UTC date into a JS Date object", function* () {
+ var date = "2016-02-27 22:00:00";
+ date = Zotero.Date.sqlToDate(date, true);
+ assert.equal(date.getTime(), 1456610400000);
+ })
+ })
+
describe("#isISODate()", function () {
it("should determine whether a date is an ISO 8601 date", function () {
assert.ok(Zotero.Date.isISODate("2015"));