Skip to content

Commit 074779b

Browse files
committed
initial work on dates
1 parent 0f926ca commit 074779b

File tree

3 files changed

+81
-48
lines changed

3 files changed

+81
-48
lines changed

lib/client.js

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -221,32 +221,51 @@ p.onDataRow = function(msg) {
221221
this.emit('row', msg);
222222
};
223223

224+
var dateParser = function(isoDate) {
225+
//TODO find some regexp help
226+
//this method works but it's ooglay
227+
var split = isoDate.split(' ');
228+
var date = split[0];
229+
var time = split[1];
230+
var splitDate = date.split('-');
231+
var year = (splitDate[0]);
232+
var month = parseInt(splitDate[1])-1;
233+
var day = (splitDate[2]);
234+
var splitTime = time.split(':');
235+
var hour = parseInt(splitTime[0]);
236+
var min = splitTime[1];
237+
var end = splitTime[2];
238+
var seconds = /(\d{2})/.exec(end);
239+
seconds = (seconds ? seconds[1] : 0);
240+
var mili = /\.(\d{1,})/.exec(end);
241+
mili = mili ? mili[1].slice(0,3) : 0;
242+
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(end);
243+
//minutes to adjust for timezone
244+
var tzAdjust = 0;
245+
if(tZone) {
246+
var type = tZone[1];
247+
switch(type) {
248+
case 'Z': break;
249+
case '-':
250+
tzAdjust = -(((parseInt(tZone[2])*60)+(parseInt(tZone[3]||0))));
251+
break;
252+
case '+':
253+
tzAdjust = (((parseInt(tZone[2])*60)+(parseInt(tZone[3]||0))));
254+
break;
255+
default:
256+
throw new Error("Unidentifed tZone part " + type);
257+
}
258+
}
259+
console.log("tzAdjust " + tzAdjust);
260+
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
261+
var date = new Date(utcOffset+(tzAdjust*1000*60));
224262

225263

226-
// var intParser = {
227-
// fromDbValue: parseInt
228-
// };
229-
230-
// var floatParser = {
231-
// fromDbValue: parseFloat
232-
// };
233-
234-
// var timeParser = {
235-
// fromDbValue: function(isoTime) {
236-
// var when = new Date();
237-
// var split = isoTime.split(':');
238-
// when.setHours(split[0]);
239-
// when.setMinutes(split[1]);
240-
// when.setSeconds(split[2].split('-') [0]);
241-
// return when;
242-
// }
243-
// };
244-
245-
// var dateParser = {
246-
// fromDbValue: function(isoDate) {
247-
// return Date.parse(isoDate);
248-
// }
249-
// };
264+
console.log(isoDate);
265+
console.log(date);
266+
console.log("");
267+
return isoDate;
268+
};
250269

251270
Client.dataTypeParser = {
252271
20: parseInt,
@@ -256,32 +275,14 @@ Client.dataTypeParser = {
256275
1700: parseFloat,
257276
700: parseFloat,
258277
701: parseFloat,
259-
16: function(dbVal) {
278+
16: function(dbVal) { //boolean
260279
return dbVal === 't';
261-
}
280+
},
262281
// 1083: timeParser,
263282
// 1266: timeParser,
264-
// 1114: dateParser,
265-
// 1184: dateParser
283+
1114: dateParser,
284+
1184: dateParser
266285
};
267286

268-
// p.processRowDescription = function(description) {
269-
// this.fields = description.fields;
270-
// };
271-
272-
// p.processDataRow = function(dataRow) {
273-
// var row = dataRow.fields;
274-
// var fields = this.fields || [];
275-
// var field, dataType;
276-
// for(var i = 0, len = row.length; i < len; i++) {
277-
// field = fields[i] || 0
278-
// var dataType = Client.dataTypes[field.dataTypeID];
279-
// if(dataType) {
280-
// row[i] = dataType.fromDbValue(row[i]);
281-
// }
282-
// }
283-
// this.emit('row',row);
284-
// };
285-
286287
//end parsing methods
287288
module.exports = Client;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var testForTypeCoercion = function(type){
3636
});
3737
};
3838

39-
//TODO test for nulls
4039
var types = [{
4140
name: 'integer',
4241
values: [1, -1, null]

test/unit/client/typed-query-results.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ test('typed results', function() {
6363
dataTypeID: 16,
6464
actual: null,
6565
expected: null
66+
},{
67+
name: 'timestamptz with minutes in timezone',
68+
dataTypeID: 1184,
69+
actual: '2010-10-31 14:54:13.74-0530',
70+
expected: function(val) {
71+
return false;
72+
}
73+
},{
74+
name: 'timestampz with huge miliseconds in UTC',
75+
dataTypeID: 1184,
76+
actual: '2010-10-30 14:11:12.730838Z',
77+
expected: function(val) {
78+
return false;
79+
}
80+
},{
81+
name: 'timestampz with no miliseconds',
82+
dataTypeID: 1184,
83+
actual: '2010-10-30 13:10:01+05',
84+
expected: function(val) {
85+
return false;
86+
}
87+
},{
88+
name: 'timestamp',
89+
dataTypeID: 1114,
90+
actual: '2010-10-31 00:00:00',
91+
expected: function(val) {
92+
return false;
93+
}
6694
}];
6795

6896

@@ -74,7 +102,12 @@ test('typed results', function() {
74102
assert.emits(query, 'row', function(row) {
75103
for(var i = 0; i < tests.length; i++) {
76104
test('parses ' + tests[i].name, function() {
77-
assert.strictEqual(row.fields[i], tests[i].expected);
105+
var expected = tests[i].expected;
106+
if(typeof expected === 'function') {
107+
return expected(row.fields[i]);
108+
}
109+
assert.strictEqual(row.fields[i], expected);
110+
78111
});
79112
}
80113
});

0 commit comments

Comments
 (0)