Skip to content

Commit 381598d

Browse files
committed
date type coercion works in both directions
1 parent 2c36225 commit 381598d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/client.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ p.hasBeenParsed = function(connection) {
144144

145145
p.prepare = function(connection) {
146146
var self = this;
147-
148147
var onParseComplete = function() {
148+
if(self.values) {
149+
self.values = self.values.map(function(val) {
150+
return (val instanceof Date) ? JSON.stringify(val) : val;
151+
});
152+
}
149153
connection.bind({
150154
portal: self.name,
151155
statement: self.name,
@@ -260,9 +264,9 @@ var dateParser = function(isoDate) {
260264
throw new Error("Unidentifed tZone part " + type);
261265
}
262266
}
263-
267+
264268
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
265-
269+
266270
var date = new Date(utcOffset - (tzAdjust * 60* 1000));
267271
return date;
268272
};

test/integration/client/type-coercion-tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,34 @@ var types = [{
7777

7878
types.forEach(testForTypeCoercion);
7979

80+
test("timestampz round trip", function() {
81+
var now = new Date();
82+
var client = helper.client();
83+
client.on('error', function(err) {
84+
console.log(err);
85+
client.end();
86+
});
87+
client.query("create temp table date_tests(name varchar(10), tstz timestamptz(3))");
88+
client.query({
89+
text: "insert into date_tests(name, tstz)VALUES($1, $2)",
90+
name: 'add date',
91+
values: ['now', now]
92+
});
93+
var result = client.query({
94+
name: 'get date',
95+
text: 'select * from date_tests where name = $1',
96+
values: ['now']
97+
});
98+
assert.emits(result, 'row', function(row) {
99+
var date = row.fields[1];
100+
assert.equal(date.getYear(),now.getYear());
101+
assert.equal(date.getMonth(), now.getMonth());
102+
assert.equal(date.getDate(), now.getDate());
103+
assert.equal(date.getHours(), now.getHours());
104+
assert.equal(date.getMinutes(), now.getMinutes());
105+
assert.equal(date.getSeconds(), now.getSeconds());
106+
assert.equal(date.getMilliseconds(), now.getMilliseconds());
107+
});
108+
client.on('drain', client.end.bind(client));
109+
});
80110

0 commit comments

Comments
 (0)