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 (#3204)
  • Loading branch information
w666 authored Nov 14, 2024
1 parent d5a76e6 commit bded498
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 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, columnType) {
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 (columnType === 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
4 changes: 2 additions & 2 deletions lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function readCodeFor(field, config, options, fieldNum) {
case Types.TIMESTAMP:
case Types.NEWDATE:
if (helpers.typeMatch(field.columnType, dateStrings, Types)) {
return `packet.readDateTimeString(${parseInt(field.decimals, 10)});`;
return `packet.readDateTimeString(${parseInt(field.decimals, 10)}, ${null}, ${field.columnType});`;
}
return `packet.readDateTime(${helpers.srcEscape(timezone)});`;
case Types.TIME:
Expand All @@ -56,7 +56,7 @@ function readCodeFor(field, config, options, fieldNum) {
case Types.GEOMETRY:
return 'packet.parseGeometryValue();';
case Types.VECTOR:
return 'packet.parseVector()';
return 'packet.parseVector()';
case Types.JSON:
// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
Expand Down
30 changes: 12 additions & 18 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,28 +73,20 @@ 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=?, d7=?',
[date, date1, date2, date3, date4, date5, date6],
);
connection1.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date,
date1,
date2,
date3,
date4,
date5,
]);

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=?, d7=?',
[date, date1, date2, date3, date4, date5, date6],
);
connection2.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date,
date1,
date2,
date3,
date4,
date5,
]);

connectionZ.query(
'CREATE TEMPORARY TABLE t (d1 DATE, d2 DATETIME(3), d3 DATETIME(6))',
Expand Down Expand Up @@ -123,6 +116,7 @@ const dateAsStringExpected = [
d4: date3,
d5: date4,
d6: date5,
d7: date6,
},
];

Expand Down

0 comments on commit bded498

Please sign in to comment.