date type coercion works in both directions

This commit is contained in:
brianc 2010-11-02 00:50:42 -05:00
parent 2c362250b3
commit 381598d3c0
2 changed files with 37 additions and 3 deletions

View File

@ -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;
};

View File

@ -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));
});