comparison 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
comparison
equal deleted inserted replaced
27:a36a514e8be8 28:7cfcd7d5796c
1 /* 1 /*
2 * Thanks to Paul Sowden for this script. 2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
3 * http://delete.me.uk/2005/03/iso8601.html 3 *
4 * This file is part of PSĜS, a PubSub server written in JavaScript.
5 *
6 * PSĜS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License.
10 *
11 * PSĜS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>.
4 */ 18 */
5 19
6 Date.prototype.setISO8601 = function(dString){ 20 Date.prototype.setFromISO8601 = function(iso){
7 var d; 21 var format = /(\d\{4\})(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)Z/;
8 var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/; 22 var m = iso.match(format);
9 if ((d = dString.match(regexp))) { 23
10 var offset = 0; 24 this.setUTCDate(1);
11 this.setUTCDate(1); 25 this.setUTCFullYear(parseInt(m[1],10));
12 this.setUTCFullYear(parseInt(d[1],10)); 26 this.setUTCMonth(parseInt(m[3],10) - 1);
13 this.setUTCMonth(parseInt(d[3],10) - 1); 27 this.setUTCDate(parseInt(m[5],10));
14 this.setUTCDate(parseInt(d[5],10)); 28 this.setUTCHours(parseInt(m[7],10));
15 this.setUTCHours(parseInt(d[7],10)); 29 this.setUTCMinutes(parseInt(m[9],10));
16 this.setUTCMinutes(parseInt(d[9],10)); 30 this.setUTCSeconds(parseInt(m[11],10));
17 this.setUTCSeconds(parseInt(d[11],10)); 31 this.setUTCMilliseconds(0);
18 if (d[12]) 32
19 this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
20 else
21 this.setUTCMilliseconds(0);
22 if (d[13] != 'Z') {
23 offset = (d[15] * 60) + parseInt(d[17],10);
24 offset *= ((d[14] == '-') ? -1 : 1);
25 this.setTime(this.getTime() - offset * 60 * 1000);
26 }
27 } else
28 this.setTime(Date.parse(dString));
29 return this; 33 return this;
30 }; 34 };
31 35
32 Date.prototype.toString = function (format, offset) { 36 Date.prototype.toString = function () {
33 /* accepted values for the format [1-6]: 37 var pad = function (n) {
34 1 Year: 38 return ((n < 10)? '0': '') + n;
35 YYYY (eg 1997) 39 };
36 2 Year and month:
37 YYYY-MM (eg 1997-07)
38 3 Complete date:
39 YYYY-MM-DD (eg 1997-07-16)
40 4 Complete date plus hours and minutes:
41 YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
42 5 Complete date plus hours, minutes and seconds:
43 YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
44 6 Complete date plus hours, minutes, seconds and a decimal
45 fraction of a second
46 YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
47 */
48 if (!format)
49 var format = 5;
50 if (!offset) {
51 var offset = 'Z';
52 var date = this;
53 } else {
54 var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
55 var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
56 offsetnum *= ((d[1] == '-') ? -1 : 1);
57 var date = new Date(Number(Number(this) + (offsetnum * 60000)));
58 }
59 40
60 var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }; 41 return this.getUTCFullYear() +
61 42 "-" + pad(this.getUTCMonth() + 1) +
62 var str = ""; 43 "-" + pad(this.getUTCDate()) +
63 str += date.getUTCFullYear(); 44 "T" + pad(this.getUTCHours()) +
64 if (format > 1) 45 ":" + pad(this.getUTCMinutes()) +
65 str += "-" + zeropad(date.getUTCMonth() + 1); 46 ":" + pad(this.getUTCSeconds()) + 'Z';
66 if (format > 2)
67 str += "-" + zeropad(date.getUTCDate());
68 if (format > 3)
69 str += "T" + zeropad(date.getUTCHours()) +
70 ":" + zeropad(date.getUTCMinutes());
71 if (format > 5) {
72 var secs = Number(date.getUTCSeconds() + "." +
73 ((date.getUTCMilliseconds() < 100) ? '0' : '') +
74 zeropad(date.getUTCMilliseconds()));
75 str += ":" + zeropad(secs);
76 } else if (format > 4)
77 str += ":" + zeropad(date.getUTCSeconds());
78
79 if (format > 3)
80 str += offset;
81 return str;
82 }; 47 };