Merge pull request #432 from brianc/pull/425

Fix for early dates
This commit is contained in:
Brian C 2013-09-01 20:08:30 -07:00
commit c13f34aad9
2 changed files with 33 additions and 3 deletions

View File

@ -17,7 +17,11 @@ var parseDate = function(isoDate) {
return new Date(isoDate);
}
}
var year = match[1];
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);
@ -37,6 +41,7 @@ var parseDate = function(isoDate) {
var tZone = /([Z|+\-])(\d{2})?:?(\d{2})?/.exec(isoDate.split(' ')[1]);
//minutes to adjust for timezone
var tzAdjust = 0;
var date;
if(tZone) {
var type = tZone[1];
switch(type) {
@ -53,13 +58,18 @@ var parseDate = function(isoDate) {
}
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
return new Date(utcOffset - (tzAdjust * 60* 1000));
date = new Date(utcOffset - (tzAdjust * 60* 1000));
}
//no timezone information
else {
return new Date(year, month, day, hour, min, seconds, mili);
date = new Date(year, month, day, hour, min, seconds, mili);
}
if (isFirstCentury) {
date.setUTCFullYear(year);
}
return date;
};
var parseBool = function(val) {

View File

@ -150,6 +150,26 @@ test("timestampz round trip", function() {
client.on('drain', client.end.bind(client));
});
if(!helper.config.binary) {
test('early AD & BC date', function() {
var client = helper.client();
client.on('error', function(err) {
console.log(err);
client.end();
});
client.query('SELECT $1::TIMESTAMPTZ as when', ["0062-03-08 14:32:00"], assert.success(function(res) {
assert.equal(res.rows[0].when.getFullYear(), 62);
}))
client.query('SELECT $1::TIMESTAMPTZ as when', ["0062-03-08 14:32:00 BC"], assert.success(function(res) {
assert.equal(res.rows[0].when.getFullYear(), -62);
}))
client.on('drain', client.end.bind(client));
})
}
helper.pg.connect(helper.config, assert.calls(function(err, client, done) {
assert.isNull(err);
client.query('select null as res;', assert.calls(function(err, res) {