diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index c7ec064f..34a91ecb 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -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) { diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 0e303a21..5c957fcc 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -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) {