changeset 28:7cfcd7d5796c

Replace Paul Sowden’s ISO8601 functions by my own, since their licence is possibly incompatible with AGPL.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 03:01:40 +0100
parents a36a514e8be8
children e007a6364bf0
files backends/directory.js backends/file.js iso8601.js
diffstat 3 files changed, 43 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/backends/directory.js
+++ b/backends/directory.js
@@ -36,7 +36,7 @@ backend.save = function(dir) {
 			function sanitize(o) {
 				var n = {};
 				for (var i in o) {
-					if (i == 'content' || o[i].setISO8601)
+					if (i == 'content' || o[i].setFromISO8601)
 						n[i] = o[i].toString();
 					else if (o[i] instanceof Array)
 						n[i] = o[i];
@@ -109,7 +109,7 @@ backend.load = function(dir) {
 						parseStanza(o, i);
 					else if (typeof o[i] == 'string' && regexp(o[i])) {
 						var today = new Date();
-						today.setISO8601(o[i]);
+						today.setFromISO8601(o[i]);
 						o[i] = today;
 					} else if (typeof o[i] == 'object')
 						endParsing(o[i]);
--- a/backends/file.js
+++ b/backends/file.js
@@ -24,7 +24,7 @@ backend.save = function(file) {
 	function sanitize(o) {
 		var n = {};
 		for (var i in o) {
-			if (i == 'content' || o[i].setISO8601)
+			if (i == 'content' || o[i].setFromISO8601)
 				n[i] = o[i].toString();
 			else if (o[i] instanceof Array)
 				n[i] = o[i];
@@ -66,7 +66,7 @@ backend.load = function(file) {
 				parseStanza(o, i);
 			else if (typeof o[i] == 'string' && regexp(o[i])) {
 				var today = new Date();
-				today.setISO8601(o[i]);
+				today.setFromISO8601(o[i]);
 				o[i] = today;
 			} else if (typeof o[i] == 'object')
 				endParsing(o[i]);
--- a/iso8601.js
+++ b/iso8601.js
@@ -1,82 +1,47 @@
 /*
- * Thanks to Paul Sowden for this script.
- * http://delete.me.uk/2005/03/iso8601.html
+ *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ *
+ *  This file is part of PSĜS, a PubSub server written in JavaScript.
+ *
+ *  PSĜS is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License.
+ *
+ *  PSĜS is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-Date.prototype.setISO8601 = function(dString){
-	var d;
-	var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
-	if ((d = dString.match(regexp))) {
-		var offset = 0;
-		this.setUTCDate(1);
-		this.setUTCFullYear(parseInt(d[1],10));
-		this.setUTCMonth(parseInt(d[3],10) - 1);
-		this.setUTCDate(parseInt(d[5],10));
-		this.setUTCHours(parseInt(d[7],10));
-		this.setUTCMinutes(parseInt(d[9],10));
-		this.setUTCSeconds(parseInt(d[11],10));
-		if (d[12])
-			this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
-		else
-			this.setUTCMilliseconds(0);
-		if (d[13] != 'Z') {
-			offset = (d[15] * 60) + parseInt(d[17],10);
-			offset *= ((d[14] == '-') ? -1 : 1);
-			this.setTime(this.getTime() - offset * 60 * 1000);
-		}
-	} else
-		this.setTime(Date.parse(dString));
+Date.prototype.setFromISO8601 = function(iso){
+	var format = /(\d\{4\})(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)Z/;
+	var m = iso.match(format);
+
+	this.setUTCDate(1);
+	this.setUTCFullYear(parseInt(m[1],10));
+	this.setUTCMonth(parseInt(m[3],10) - 1);
+	this.setUTCDate(parseInt(m[5],10));
+	this.setUTCHours(parseInt(m[7],10));
+	this.setUTCMinutes(parseInt(m[9],10));
+	this.setUTCSeconds(parseInt(m[11],10));
+	this.setUTCMilliseconds(0);
+
 	return this;
 };
 
-Date.prototype.toString = function (format, offset) {
-	/* accepted values for the format [1-6]:
-	 1 Year:
-	   YYYY (eg 1997)
-	 2 Year and month:
-	   YYYY-MM (eg 1997-07)
-	 3 Complete date:
-	   YYYY-MM-DD (eg 1997-07-16)
-	 4 Complete date plus hours and minutes:
-	   YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
-	 5 Complete date plus hours, minutes and seconds:
-	   YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
-	 6 Complete date plus hours, minutes, seconds and a decimal
-	   fraction of a second
-	   YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
-	 */
-	if (!format)
-		var format = 5;
-	if (!offset) {
-		var offset = 'Z';
-		var date = this;
-	} else {
-		var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
-		var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
-		offsetnum *= ((d[1] == '-') ? -1 : 1);
-		var date = new Date(Number(Number(this) + (offsetnum * 60000)));
-	}
+Date.prototype.toString = function () {
+	var pad = function (n) {
+		return ((n < 10)? '0': '') + n;
+	};
 
-	var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; };
-
-	var str = "";
-	str += date.getUTCFullYear();
-	if (format > 1)
-		str += "-" + zeropad(date.getUTCMonth() + 1);
-	if (format > 2)
-		str += "-" + zeropad(date.getUTCDate());
-	if (format > 3)
-		str += "T" + zeropad(date.getUTCHours()) +
-			":" + zeropad(date.getUTCMinutes());
-	if (format > 5) {
-		var secs = Number(date.getUTCSeconds() + "." +
-				((date.getUTCMilliseconds() < 100) ? '0' : '') +
-				zeropad(date.getUTCMilliseconds()));
-		str += ":" + zeropad(secs);
-	} else if (format > 4)
-		str += ":" + zeropad(date.getUTCSeconds());
-
-	if (format > 3)
-		str += offset;
-	return str;
+	return this.getUTCFullYear() +
+		"-" + pad(this.getUTCMonth() + 1) +
+		"-" + pad(this.getUTCDate()) +
+		"T" + pad(this.getUTCHours()) +
+		":" + pad(this.getUTCMinutes()) +
+		":" + pad(this.getUTCSeconds()) + 'Z';
 };