Permalink
Browse files

Splitting time parse methods into own methods.

  • Loading branch information...
1 parent 19b24eb commit f0e409994c9fb838a38af36bb05414d613ceb382 @squeeks committed Mar 18, 2013
Showing with 33 additions and 17 deletions.
  1. +33 −17 lib/glossy/parse.js
View
@@ -241,12 +241,31 @@ GlossyParser.prototype.decodePri = function(message) {
GlossyParser.prototype.parseTimeStamp = function(timeStamp) {
if(typeof timeStamp != 'string') return;
-
var parsedTime;
- // Try RFC3339 Style (specified in RFC5424)
- // https://github.com/tardate/rfc3339date.js
- // This portion is Copyright (c) 2010 Paul GALLAGHER http://tardate.com
+ parsedTime = this.parseRfc3339(timeStamp);
+ if(parsedTime) return parsedTime;
+
+ parsedTime = this.parseBsdTime(timeStamp);
+ if(parsedTime) return parsedTime;
+
+ // Try ISO 8601 (suggested in RFC 3164)
+ d = timeStamp.match(/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/); //TODO: Less strict match? TZ designations?
+ if(d) {
+ parsedTime = new Date(d[1], d[2], d[3], d[4], d[5], d[6]);
+ }
+
+ return parsedTime;
+
+};
+
+/*
+ * Parse RFC3339 style timestamps
+ * @param {String} timeStamp
+ * @return {Date/false} Timestamp, if parsed correctly
+ */
+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) {
@@ -277,11 +296,17 @@ GlossyParser.prototype.parseTimeStamp = function(timeStamp) {
parsedTime = new Date(year,mon,day,hour,mins,secs,millis);
}
}
+ return parsedTime;
+};
- if(parsedTime) return parsedTime;
-
- // Try BSD Style formatting (specified in RFC 3164)
- d = timeStamp.match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{2}):(\d{2}):(\d{2})/);
+/*
+ * Parse "BSD style" timestamps, as defined in RFC3164
+ * @param {String} timeStamp
+ * @return {Date/false} Timestamp, if parsed correctly
+ */
+GlossyParser.prototype.parseBsdTime = function(timeStamp) {
+ var parsedTime;
+ var d = timeStamp.match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{2}):(\d{2}):(\d{2})/);
if(d) {
// Years are absent from the specification, use this year
currDate = new Date();
@@ -294,16 +319,7 @@ GlossyParser.prototype.parseTimeStamp = function(timeStamp) {
d[5]);
}
- if(parsedTime) return parsedTime;
-
- // Try ISO 8601 (suggested in RFC 3164)
- d = timeStamp.match(/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/); //TODO: Less strict match? TZ designations?
- if(d) {
- parsedTime = new Date(d[1], d[2], d[3], d[4], d[5], d[6]);
- }
-
return parsedTime;
-
};

0 comments on commit f0e4099

Please sign in to comment.