commit 322339876e751abc6f5cb32aeea94d6afd628085
parent e8d4b3e840627303a2a03a5801a4bf2bd14de319
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 29 Apr 2015 17:26:05 -0400
Add Zotero.Date.isISODate() and Zotero.Date.isoToSQL()
Diffstat:
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/chrome/content/zotero/xpcom/date.js b/chrome/content/zotero/xpcom/date.js
@@ -180,6 +180,15 @@ Zotero.Date = new function(){
}
+ var _re8601 = /^([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?$/;
+
+ /**
+ * @return {Boolean} - True if string is an ISO 8601 date, false if not
+ */
+ this.isISODate = function (str) {
+ return _re8601.test(str);
+ }
+
/**
* Convert an ISO 8601–formatted UTC date/time to a JS Date
*
@@ -189,8 +198,7 @@ Zotero.Date = new function(){
* @return {Date} JS Date
*/
this.isoToDate = function (isoDate) {
- var re8601 = /([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?/;
- var d = isoDate.match(re8601);
+ var d = isoDate.match(_re8601);
var offset = 0;
var date = new Date(d[1], 0, 1);
@@ -212,6 +220,11 @@ Zotero.Date = new function(){
}
+ this.isoToSQL = function (isoDate) {
+ return this.dateToSQL(this.isoToDate(isoDate), true);
+ }
+
+
/*
* converts a string to an object containing:
* day: integer form of the day
diff --git a/test/tests/dateTest.js b/test/tests/dateTest.js
@@ -0,0 +1,11 @@
+describe("Zotero.Date", function() {
+ describe("#isISODate()", function () {
+ it("should determine whether a date is an ISO 8601 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:28Z"));
+ assert.isFalse(Zotero.Date.isISODate("2015-04-29 17:28"));
+ })
+ })
+})