Skip to content

Commit 5bf7bda

Browse files
fix: zero dates come back as "undefined 00:00:00" with dateStrings (#4491)
1 parent 928c3c5 commit 5bf7bda

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

lib/packets/packet.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Long = require('long');
1111
const StringParser = require('../parsers/string.js');
1212
const Types = require('../constants/types.js');
1313
const INVALID_DATE = new Date(NaN);
14+
const ZERO_DATE = '0000-00-00';
1415

1516
// this is nearly duplicate of previous function so generated code is not slower
1617
// due to "if (dateStrings)" branching
@@ -317,7 +318,7 @@ class Packet {
317318
return new Date(y, m - 1, d, H, M, S, ms);
318319
}
319320
let str = this.readDateTimeString(6, 'T', null);
320-
if (!str) {
321+
if (str.startsWith(ZERO_DATE)) {
321322
return INVALID_DATE;
322323
}
323324
if (str.length === 10) {
@@ -335,7 +336,7 @@ class Packet {
335336
let M = 0;
336337
let S = 0;
337338
let ms = 0;
338-
let str;
339+
let str = ZERO_DATE;
339340
if (length > 3) {
340341
y = this.readInt16();
341342
m = this.readInt8();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { RowDataPacket } from '../../../index.js';
2+
import { describe, it, strict } from 'poku';
3+
import { createConnection } from '../../common.test.mjs';
4+
5+
await describe('dateStrings: binary protocol keeps the zero date of DATE/DATETIME/TIMESTAMP', async () => {
6+
const connection = createConnection({ dateStrings: true }).promise();
7+
8+
const [modes] = await connection.query<RowDataPacket[]>(
9+
'SELECT @@sql_mode AS value'
10+
);
11+
const relaxedMode = String(modes[0].value)
12+
.split(',')
13+
.filter((mode) => mode !== 'NO_ZERO_DATE' && mode !== 'NO_ZERO_IN_DATE')
14+
.join(',');
15+
16+
await connection.query('SET sql_mode=?', [relaxedMode]);
17+
await connection.query(
18+
'CREATE TEMPORARY TABLE zero_dates (d DATE, dt DATETIME, ts TIMESTAMP NULL)'
19+
);
20+
await connection.query(
21+
"INSERT INTO zero_dates VALUES ('0000-00-00', '0000-00-00 00:00:00', '0000-00-00 00:00:00')"
22+
);
23+
24+
const [rows] = await connection.execute<RowDataPacket[]>(
25+
'SELECT * FROM zero_dates'
26+
);
27+
28+
await it('returns the zero DATETIME and TIMESTAMP as strings', () => {
29+
strict.equal(rows[0].dt, '0000-00-00 00:00:00');
30+
strict.equal(rows[0].ts, '0000-00-00 00:00:00');
31+
});
32+
33+
await it('returns the zero DATE as a string', () => {
34+
strict.equal(rows[0].d, '0000-00-00');
35+
});
36+
37+
await connection.end();
38+
});

0 commit comments

Comments
 (0)