Skip to content

Commit

Permalink
Move date parser to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker committed Jun 13, 2015
1 parent 7d0e439 commit 468dfda
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 157 deletions.
79 changes: 1 addition & 78 deletions lib/textParsers.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
var array = require('postgres-array')
var ap = require('ap')
var arrayParser = require(__dirname + "/arrayParser.js");

//parses PostgreSQL server formatted date strings into javascript date objects
var parseDate = function(isoDate) {
//TODO this could do w/ a refactor
var dateMatcher = /(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/;

var match = dateMatcher.exec(isoDate);
//could not parse date
if(!match) {
dateMatcher = /^(\d{1,})-(\d{2})-(\d{2})$/;
match = dateMatcher.test(isoDate);
if(!match) {
return null;
} else {
//it is a date in YYYY-MM-DD format
//add time portion to force js to parse as local time
return new Date(isoDate + ' 00:00:00');
}
}
var isBC = /BC$/.test(isoDate);
var _year = parseInt(match[1], 10);
var isFirstCentury = (_year > 0) && (_year < 100);
var year = (isBC ? "-" : "") + match[1];

var month = parseInt(match[2],10)-1;
var day = match[3];
var hour = parseInt(match[4],10);
var min = parseInt(match[5],10);
var seconds = parseInt(match[6], 10);

var miliString = match[7];
var mili = 0;
if(miliString) {
mili = 1000 * parseFloat(miliString);
}

//match timezones like the following:
//Z (UTC)
//-05
//+06:30
var tZone = /([Z|+\-])(\d{2})?:?(\d{2})?:?(\d{2})?/.exec(isoDate.split(' ')[1]);
//minutes to adjust for timezone
var tzAdjust = 0;
var tzSign = 1;
var date;
if(tZone) {
var type = tZone[1];
switch(type) {
case 'Z':
break;
case '-':
tzSign = -1;
case '+':
tzAdjust = tzSign * (
(parseInt(tZone[2], 10) * 3600) +
(parseInt(tZone[3] || 0, 10) * 60) +
(parseInt(tZone[4] || 0, 10))
);
break;
default:
throw new Error("Unidentifed tZone part " + type);
}

var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);

date = new Date(utcOffset - (tzAdjust * 1000));
}
//no timezone information
else {
date = new Date(year, month, day, hour, min, seconds, mili);
}

if (isFirstCentury) {
date.setUTCFullYear(year);
}

return date;
};
var parseDate = require('postgres-date');

function allowNull (fn) {
return function nullAllowed (value) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"ap": "~0.2.0",
"postgres-array": "~1.0.0"
"postgres-array": "~1.0.0",
"postgres-date": "^1.0.0"
}
}
78 changes: 0 additions & 78 deletions test/date.js

This file was deleted.

0 comments on commit 468dfda

Please sign in to comment.