diff --git a/lib/packets/packet.js b/lib/packets/packet.js index 221d45d867..8349ba1459 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -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(); @@ -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); }; diff --git a/test/integration/connection/test-invalid-date-result.js b/test/integration/connection/test-invalid-date-result.js new file mode 100644 index 0000000000..26d38a54f2 --- /dev/null +++ b/test/integration/connection/test-invalid-date-result.js @@ -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); +});