view iso8601.js @ 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 9ee956af41e3
children e007a6364bf0
line wrap: on
line source

/*
 *  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.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 () {
	var pad = function (n) {
		return ((n < 10)? '0': '') + n;
	};

	return this.getUTCFullYear() +
		"-" + pad(this.getUTCMonth() + 1) +
		"-" + pad(this.getUTCDate()) +
		"T" + pad(this.getUTCHours()) +
		":" + pad(this.getUTCMinutes()) +
		":" + pad(this.getUTCSeconds()) + 'Z';
};