From 5c233896f1263efc8e586bc2eace785b990708ab Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sun, 1 Sep 2013 21:59:01 -0500 Subject: [PATCH 1/5] Add failing early date tests --- .../integration/client/type-coercion-tests.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 0e303a2..d590c35 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -150,6 +150,25 @@ test("timestampz round trip", function() { client.on('drain', client.end.bind(client)); }); +test('early AD 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) { + console.log(res.rows[0].when) + 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) { From b7f8429ff7fcde5185a9d026ec5f3bd85c4ddf46 Mon Sep 17 00:00:00 2001 From: William Becker Date: Mon, 26 Aug 2013 17:56:50 +0100 Subject: [PATCH 2/5] handle early dates (< 100AD) --- lib/types/textParsers.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index c7ec064..a1c0bd8 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) { From 3c6b106b868d7d93cda3b7d3b16a289f323b197f Mon Sep 17 00:00:00 2001 From: William Becker Date: Mon, 26 Aug 2013 18:10:32 +0100 Subject: [PATCH 3/5] fix trailing whitespace breaking jshint --- lib/types/textParsers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index a1c0bd8..34a91ec 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -17,7 +17,7 @@ var parseDate = function(isoDate) { return new Date(isoDate); } } - var isBC = /BC$/.test(isoDate); + var isBC = /BC$/.test(isoDate); var _year = parseInt(match[1], 10); var isFirstCentury = (_year > 0) && (_year < 100); var year = (isBC ? "-" : "") + match[1]; @@ -65,7 +65,7 @@ var parseDate = function(isoDate) { else { date = new Date(year, month, day, hour, min, seconds, mili); } - + if (isFirstCentury) { date.setUTCFullYear(year); } From fb499e4b3d863daaa8058482fea96580f401e9b0 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sun, 1 Sep 2013 21:59:33 -0500 Subject: [PATCH 4/5] Fix incorrect failing date test --- test/integration/client/type-coercion-tests.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index d590c35..6e39228 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -162,8 +162,7 @@ test('early AD date', function() { })) client.query('SELECT $1::TIMESTAMPTZ as when', ["0062-03-08 14:32:00 BC"], assert.success(function(res) { - console.log(res.rows[0].when) - assert.equal(res.rows[0].when.getFullYear(), 62); + assert.equal(res.rows[0].when.getFullYear(), -62); })) client.on('drain', client.end.bind(client)); From a2d0ab286a7a2fb0a2cb4e3de01c4b3fe56924d7 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sun, 1 Sep 2013 22:03:49 -0500 Subject: [PATCH 5/5] Do not run tests for binary mode --- .../integration/client/type-coercion-tests.js | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 6e39228..5c957fc 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -150,23 +150,25 @@ test("timestampz round trip", function() { client.on('drain', client.end.bind(client)); }); -test('early AD date', function() { - var client = helper.client(); - client.on('error', function(err) { - console.log(err); - client.end(); - }); +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"], 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.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)); -}) + client.on('drain', client.end.bind(client)); + }) +} helper.pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err);