Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switching out RFC3339 parser for something better.

It is faster, however I'm having issues with it parsing sub-seconds, so our
current 5848 tests which just so happen to use them are broken.
  • Loading branch information...
commit 395acc31ce65110413730095abed0386d51006a8 1 parent f0e4099
@squeeks authored
Showing with 34 additions and 32 deletions.
  1. +34 −32 lib/glossy/parse.js
View
66 lib/glossy/parse.js
@@ -263,40 +263,42 @@ GlossyParser.prototype.parseTimeStamp = function(timeStamp) {
* Parse RFC3339 style timestamps
* @param {String} timeStamp
* @return {Date/false} Timestamp, if parsed correctly
+ * @see http://blog.toppingdesign.com/2009/08/13/fast-rfc-3339-date-processing-in-javascript/
*/
GlossyParser.prototype.parseRfc3339 = function(timeStamp){
- var parsedTime;
- //FIXME This regexp is *very* slow
- var d = timeStamp.match(/(\d{4})(-)?(\d{2})(-)?(\d{2})(T)?(\d{2})(:)?(\d{2})?(:)?(\d{2})?([\.,]\d+)?($|Z|([+-])(\d{2})(:)?(\d{2})?)/i);
- if (d) {
- var year = parseInt(d[1],10);
- var mon = parseInt(d[3],10) - 1;
- var day = parseInt(d[5],10);
- var hour = parseInt(d[7],10);
- var mins = ( d[9] ? parseInt(d[9],10) : 0 );
- var secs = ( d[11] ? parseInt(d[11],10) : 0 );
- var millis = ( d[12] ? parseFloat(String(1.5).charAt(1) + d[12].slice(1)) * 1000 : 0 );
- if (d[13]) {
- parsedTime = new Date();
- parsedTime.setUTCFullYear(year);
- parsedTime.setUTCMonth(mon);
- parsedTime.setUTCDate(day);
- parsedTime.setUTCHours(hour);
- parsedTime.setUTCMinutes(mins);
- parsedTime.setUTCSeconds(secs);
- parsedTime.setUTCMilliseconds(millis);
- if (d[13] && d[14]) {
- var offset = (d[15] * 60)
- if (d[17]) offset += parseInt(d[17],10);
- offset *= ((d[14] == '-') ? -1 : 1);
- parsedTime.setTime(parsedTime.getTime() - offset * 60 * 1000);
- }
-
- } else {
- parsedTime = new Date(year,mon,day,hour,mins,secs,millis);
- }
- }
- return parsedTime;
+   var utcOffset, offsetSplitChar, offsetString,
+    offsetMultiplier = 1,
+    dateTime = timeStamp.split("T");
+ if(dateTime.length < 2) return false;
+
+    var date = dateTime[0].split("-"),
+    time = dateTime[1].split(":"),
+    offsetField = time[time.length - 1];
+
+    offsetFieldIdentifier = offsetField.charAt(offsetField.length - 1);
+    if (offsetFieldIdentifier === "Z") {
+    utcOffset = 0;
+        time[time.length - 1] = offsetField.substr(0, offsetField.length - 2);
+    } else {
+    if (offsetField[offsetField.length - 1].indexOf("+") != -1) {
+            offsetSplitChar = "+";
+            offsetMultiplier = 1;
+        } else {
+            offsetSplitChar = "-";
+            offsetMultiplier = -1;
+        }
+
+        offsetString = offsetField.split(offsetSplitChar);
+ if(offsetString.length < 2) return false;
+ console.log(offsetString);
+        time[time.length - 1] == offsetString[0];
+        offsetString = offsetString[1].split(":");
+        utcOffset = (offsetString[0] * 60) + offsetString[1];
+        utcOffset = utcOffset * 60 * 1000;
+    }
+               
+    var parsedTime = new Date(Date.UTC(date[0], date[1] - 1, date[2], time[0], time[1], time[2]) + (utcOffset * offsetMultiplier ));
+    return this;
};
/*
Please sign in to comment.
Something went wrong with that request. Please try again.