Skip to content

Commit

Permalink
treat 0/0/0/0/0 datetime as JS invalid time (NaN) sidorares#16
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Jul 13, 2013
1 parent 832b905 commit 9049f9c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Packet.prototype.readBuffer = function(len) {
return this.buffer.slice(this.offset - len, this.offset);
};

var INVALID_DATE = new Date(NaN);
// DATE, DATETIME and TIMESTAMP
Packet.prototype.readDateTime = function(convertTtoMs) {
var length = this.readInt8();
Expand All @@ -153,6 +154,9 @@ Packet.prototype.readDateTime = function(convertTtoMs) {
}
if (length > 11)
ms = this.readInt32();
if ((y + m + d + H + M + S + ms) === 0) {
return INVALID_DATE;
}
return new Date(y, m, d, H, M, S, ms);
};

Expand Down
21 changes: 21 additions & 0 deletions test/integration/connection/test-invalid-date-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');

var rows = undefined;

connection.execute('SELECT TIMESTAMP(0000-00-00) t', [], function(err, _rows, _fields) {
if (err) throw err;
rows = _rows;
});

connection.end();

function isInvalidTime(t) {
return isNaN(t.getTime());
}

process.on('exit', function() {
assert.deepEqual(Object.prototype.toString.call(rows[0].t), "[object Date]");
assert.deepEqual(isInvalidTime(rows[0].t), true);
});

0 comments on commit 9049f9c

Please sign in to comment.