From 381598d3c073d234d23099a57573b5847f8d233d Mon Sep 17 00:00:00 2001 From: brianc Date: Tue, 2 Nov 2010 00:50:42 -0500 Subject: [PATCH] date type coercion works in both directions --- lib/client.js | 10 +++++-- .../integration/client/type-coercion-tests.js | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 5f8dbea9..ac57e49b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -144,8 +144,12 @@ p.hasBeenParsed = function(connection) { p.prepare = function(connection) { var self = this; - var onParseComplete = function() { + if(self.values) { + self.values = self.values.map(function(val) { + return (val instanceof Date) ? JSON.stringify(val) : val; + }); + } connection.bind({ portal: self.name, statement: self.name, @@ -260,9 +264,9 @@ var dateParser = function(isoDate) { throw new Error("Unidentifed tZone part " + type); } } - + var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili); - + var date = new Date(utcOffset - (tzAdjust * 60* 1000)); return date; }; diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 88700381..8654d287 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -77,4 +77,34 @@ var types = [{ types.forEach(testForTypeCoercion); +test("timestampz round trip", function() { + var now = new Date(); + var client = helper.client(); + client.on('error', function(err) { + console.log(err); + client.end(); + }); + client.query("create temp table date_tests(name varchar(10), tstz timestamptz(3))"); + client.query({ + text: "insert into date_tests(name, tstz)VALUES($1, $2)", + name: 'add date', + values: ['now', now] + }); + var result = client.query({ + name: 'get date', + text: 'select * from date_tests where name = $1', + values: ['now'] + }); + assert.emits(result, 'row', function(row) { + var date = row.fields[1]; + assert.equal(date.getYear(),now.getYear()); + assert.equal(date.getMonth(), now.getMonth()); + assert.equal(date.getDate(), now.getDate()); + assert.equal(date.getHours(), now.getHours()); + assert.equal(date.getMinutes(), now.getMinutes()); + assert.equal(date.getSeconds(), now.getSeconds()); + assert.equal(date.getMilliseconds(), now.getMilliseconds()); + }); + client.on('drain', client.end.bind(client)); +});