Skip to content

Commit

Permalink
fix #1040 datetime fields returned without time part when time is 00:…
Browse files Browse the repository at this point in the history
…00:00
  • Loading branch information
w666 committed Nov 12, 2024
1 parent 30a278b commit 772c48c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ErrorCodeToName = require('../constants/errors.js');
const NativeBuffer = require('buffer').Buffer;
const Long = require('long');
const StringParser = require('../parsers/string.js');

const Types = require('../constants/types.js');
const INVALID_DATE = new Date(NaN);

// this is nearly duplicate of previous function so generated code is not slower
Expand Down Expand Up @@ -292,14 +292,14 @@ class Packet {
}
return new Date(y, m - 1, d, H, M, S, ms);
}
let str = this.readDateTimeString(6, 'T');
let str = this.readDateTimeString(6, 'T', null);
if (str.length === 10) {
str += 'T00:00:00';
}
return new Date(str + timezone);
}

readDateTimeString(decimals, timeSep) {
readDateTimeString(decimals, timeSep, type) {
const length = this.readInt8();
let y = 0;
let m = 0;
Expand All @@ -324,6 +324,8 @@ class Packet {
leftPad(2, M),
leftPad(2, S)
].join(':')}`;
} else if (type === Types.DATETIME) {
str += ' 00:00:00';
}
if (length > 10) {
ms = this.readInt32();
Expand Down Expand Up @@ -425,7 +427,7 @@ class Packet {
return StringParser.decode(
this.buffer,
encoding,
this.offset - len,
this.offset - len,
this.offset
);
}
Expand Down Expand Up @@ -915,7 +917,7 @@ class Packet {
}

static MockBuffer() {
const noop = function () {};
const noop = function () { };
const res = Buffer.alloc(0);
for (const op in NativeBuffer.prototype) {
if (typeof res[op] === 'function') {
Expand Down
8 changes: 6 additions & 2 deletions test/integration/connection/test-datetime.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const date2 = '2010-12-10 14:12:09.019473';
const date3 = null;
const date4 = '2010-12-10 14:12:09.123456';
const date5 = '2010-12-10 14:12:09.019';
const date6 = '2024-11-10 00:00:00';

function adjustTZ(d, offset) {
if (offset === undefined) {
Expand Down Expand Up @@ -72,7 +73,7 @@ connection.query('INSERT INTO t set d1=?, d2=?, d3=?', [
]);

connection1.query(
'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3))',
'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3), d7 DATETIME)',
);
connection1.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date,
Expand All @@ -81,10 +82,11 @@ connection1.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date3,
date4,
date5,
date6,
]);

connection2.query(
'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3))',
'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3), d7 DATETIME)',
);
connection2.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date,
Expand All @@ -93,6 +95,7 @@ connection2.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date3,
date4,
date5,
date6
]);

connectionZ.query(
Expand Down Expand Up @@ -123,6 +126,7 @@ const dateAsStringExpected = [
d4: date3,
d5: date4,
d6: date5,
d7: date6
},
];

Expand Down

0 comments on commit 772c48c

Please sign in to comment.