mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
edge case millisecond parsing fixed
This commit is contained in:
parent
2e3dee254f
commit
2029248dbe
34
lib/query.js
34
lib/query.js
@ -191,32 +191,28 @@ p.prepare = function(connection) {
|
||||
};
|
||||
|
||||
var dateParser = function(isoDate) {
|
||||
//TODO find some regexp help
|
||||
//this method works but it's ooglay
|
||||
//if you wanna contribute...... ;)
|
||||
var split = isoDate.split(' ');
|
||||
var dateMatcher = /(\d{4})-(\d{2})-(\d{2})/;
|
||||
//TODO this could do w/ a refactor
|
||||
|
||||
var date = split[0];
|
||||
var time = split[1];
|
||||
var match = dateMatcher.exec(date);
|
||||
var splitDate = date.split('-');
|
||||
var dateMatcher = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/;
|
||||
|
||||
var match = dateMatcher.exec(isoDate);
|
||||
var year = match[1];
|
||||
var month = parseInt(match[2],10)-1;
|
||||
var day = match[3];
|
||||
var hour = parseInt(match[4],10);
|
||||
var min = parseInt(match[5],10);
|
||||
var seconds = parseInt(match[6], 10);
|
||||
|
||||
var splitTime = time.split(':');
|
||||
var hour = parseInt(splitTime[0],10);
|
||||
var min = parseInt(splitTime[1],10);
|
||||
var end = splitTime[2];
|
||||
var seconds = /(\d{2})/.exec(end);
|
||||
seconds = (seconds ? seconds[1] : 0);
|
||||
seconds = parseInt(seconds,10);
|
||||
var mili = /\.(\d{1,})/.exec(end+"000");
|
||||
mili = mili ? mili[1].slice(0,3) : 0;
|
||||
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(end);
|
||||
var miliString = match[7];
|
||||
var mili = 0;
|
||||
if(miliString) {
|
||||
mili = 1000 * parseFloat(miliString);
|
||||
}
|
||||
|
||||
var tZone = /([Z|+\-])(\d{2})?(\d{2})?/.exec(isoDate.split(' ')[1]);
|
||||
//minutes to adjust for timezone
|
||||
var tzAdjust = 0;
|
||||
|
||||
if(tZone) {
|
||||
var type = tZone[1];
|
||||
switch(type) {
|
||||
|
||||
@ -107,7 +107,7 @@ test("timestampz round trip", function() {
|
||||
text: 'select * from date_tests where name = $1',
|
||||
values: ['now']
|
||||
});
|
||||
client.connection.on('dataRow', console.log);
|
||||
|
||||
assert.emits(result, 'row', function(row) {
|
||||
var date = row.tstz;
|
||||
assert.equal(date.getYear(),now.getYear());
|
||||
|
||||
@ -5,9 +5,21 @@ test("testing dateParser", function() {
|
||||
assert.equal(q.dateParser("2010-12-11 09:09:04").toUTCString(),new Date("2010-12-11 09:09:04 GMT").toUTCString());
|
||||
});
|
||||
|
||||
var testForMs = function(part, expected) {
|
||||
var dateString = "2010-01-01 01:01:01" + part;
|
||||
test('testing for correcting parsing of ' + dateString, function() {
|
||||
var ms = q.dateParser(dateString).getMilliseconds();
|
||||
assert.equal(ms, expected)
|
||||
})
|
||||
}
|
||||
|
||||
testForMs('.1', 100);
|
||||
testForMs('.01', 10);
|
||||
testForMs('.74', 740);
|
||||
|
||||
test("testing 2dateParser", function() {
|
||||
var actual = "2010-12-11 09:09:04.19";
|
||||
var expected = "\"2010-12-11T09:09:04.190Z\"";
|
||||
var actual = "2010-12-11 09:09:04.1";
|
||||
var expected = "\"2010-12-11T09:09:04.100Z\"";
|
||||
assert.equal(JSON.stringify(q.dateParser(actual)),expected);
|
||||
});
|
||||
|
||||
@ -17,3 +29,9 @@ test("testing 2dateParser", function() {
|
||||
assert.equal(JSON.stringify(q.dateParser(actual)),expected);
|
||||
});
|
||||
|
||||
test("testing 2dateParser", function() {
|
||||
var actual = "2011-01-23 22:15:51.280843-06";
|
||||
var expected = "\"2011-01-24T04:15:51.280Z\"";
|
||||
assert.equal(JSON.stringify(q.dateParser(actual)),expected);
|
||||
});
|
||||
|
||||
|
||||
@ -68,14 +68,14 @@ test('typed results', function() {
|
||||
dataTypeID: 1184,
|
||||
actual: '2010-10-31 14:54:13.74-0530',
|
||||
expected: function(val) {
|
||||
assert.UTCDate(val, 2010, 9, 31, 20, 24, 13, 74);
|
||||
assert.UTCDate(val, 2010, 9, 31, 20, 24, 13, 740);
|
||||
}
|
||||
},{
|
||||
name: 'timestamptz with other milisecond digits dropped',
|
||||
dataTypeID: 1184,
|
||||
actual: '2011-01-23 22:05:00.68-06',
|
||||
expected: function(val) {
|
||||
assert.UTCDate(val, 2011, 01, 24, 4, 5, 00, 68);
|
||||
assert.UTCDate(val, 2011, 01, 24, 4, 5, 00, 680);
|
||||
}
|
||||
}, {
|
||||
name: 'timestampz with huge miliseconds in UTC',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user