Permalink
Browse files

outsource iso8601 support to an own file as proposded by @flosse

  • Loading branch information...
1 parent a2d58d5 commit 13952f06eddfa9a1e8fe9d449ce1e687755b21bd @mweibel mweibel committed Mar 20, 2012
Showing with 36 additions and 38 deletions.
  1. +3 −0 archive/README.markdown
  2. +32 −0 archive/iso8601_support.js
  3. +1 −38 archive/strophe.archive.js
View
@@ -3,6 +3,9 @@
strophe.archive.js is a plugin to provide Message Archiving
( [XEP-0136](http://xmpp.org/extensions/xep-0136.html) ).
+## Notes on Browser support
+If you want to support Browsers which do not support setISO8601 on a Date-Object (like e.g. FF 11), include iso8601_support.js, too.
+
## Usage
## ToDo
View
@@ -0,0 +1,32 @@
+if (!Date.prototype.setISO6801) {
+ // from http://stackoverflow.com/questions/5249216/javascript-timestamp-from-iso8061
+ Date.prototype.setISO8601 = function(dString){
+ var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
+ if (dString.toString().match(new RegExp(regexp))) {
+ var d = dString.match(new RegExp(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));
+ }
+ return this;
+ }
+}
View
@@ -31,7 +31,7 @@ Strophe.ArchivedCollection = function(connection, jid, start) {
this.connection = connection;
this.jid = jid;
this.start = start;
- this.startDate = iso8601toDate(start);
+ this.startDate = (new Date()).setISO8601(start);
};
Strophe.ArchivedCollection.prototype = {
@@ -86,40 +86,3 @@ Strophe.ArchivedMessage = function(timestamp, from, to, body) {
Strophe.ArchivedMessage.prototype = {
};
-/** Function: iso8610toDate
- * Parses a ISO-8610 Date to a Date-Object.
- *
- * Uses a fallback if the client's browser doesn't support it.
- *
- * Quote:
- * ECMAScript revision 5 adds native support for ISO-8601 dates in the Date.parse method,
- * but many browsers currently on the market (Safari 4, Chrome 4, IE 6-8) do not support it.
- *
- * Credits:
- * <Colin Snover at http://zetafleet.com/blog/javascript-dateparse-for-iso-8601>
- *
- * Parameters:
- * (String) date - ISO-8610 Date
- *
- * Returns:
- * Date-Object
- */
-function iso8601toDate(date) {
- var timestamp = Date.parse(date), minutesOffset = 0;
- if(isNaN(timestamp)) {
- var struct = /^(\d{4}|[+\-]\d{6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/.exec(date);
- if(struct) {
- if(struct[8] !== 'Z') {
- minutesOffset = +struct[10] * 60 + (+struct[11]);
- if(struct[9] === '+') {
- minutesOffset = -minutesOffset;
- }
- }
- return new Date(+struct[1], +struct[2] - 1, +struct[3], +struct[4], +struct[5] + minutesOffset, +struct[6], struct[7] ? +struct[7].substr(0, 3) : 0);
- } else {
- // XEP-0091 date
- timestamp = Date.parse(date.replace(/^(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') + 'Z');
- }
- }
- return new Date(timestamp);
-};

0 comments on commit 13952f0

Please sign in to comment.