Skip to content

Commit 3e5d32f

Browse files
authored
Merge pull request #1366 from martenjacobs/master
Improved formatting for TIME-type columns
2 parents 21ad545 + 80e932d commit 3e5d32f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lib/packets/packet.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ class Packet {
368368
ms *= sign;
369369
return ms;
370370
}
371+
// Format follows mySQL TIME format ([-][h]hh:mm:ss[.u[u[u[u[u[u]]]]]])
372+
// For positive times below 24 hours, this makes it equal to ISO 8601 times
371373
return (
372374
(sign === -1 ? '-' : '') +
373-
[d ? d * 24 + H : H, leftPad(2, M), leftPad(2, S)].join(':') +
374-
(ms ? `.${ms}` : '')
375+
[leftPad(2, d * 24 + H), leftPad(2, M), leftPad(2, S)].join(':') +
376+
(ms ? `.${ms}`.replace(/0+$/, '') : '')
375377
);
376378
}
377379

@@ -410,9 +412,9 @@ class Packet {
410412

411413
// TODO reuse?
412414
readString(len, encoding) {
413-
if ((typeof len === 'string') && (typeof encoding === 'undefined')) {
414-
encoding = len
415-
len = undefined
415+
if (typeof len === 'string' && typeof encoding === 'undefined') {
416+
encoding = len;
417+
len = undefined;
416418
}
417419
if (typeof len === 'undefined') {
418420
len = this.end - this.offset;
@@ -899,7 +901,7 @@ class Packet {
899901
}
900902

901903
static MockBuffer() {
902-
const noop = function() {};
904+
const noop = function () {};
903905
const res = Buffer.alloc(0);
904906
for (const op in NativeBuffer.prototype) {
905907
if (typeof res[op] === 'function') {

test/unit/packets/test-time.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const packets = require('../../../lib/packets/index.js');
5+
6+
[
7+
['01:23:45', '0b000004000008000000000001172d'], // CONVERT('01:23:45', TIME)
8+
['01:23:45.123456', '0f00000400000c000000000001172d40e20100'], // DATE_ADD(CONVERT('01:23:45', TIME), INTERVAL 0.123456 SECOND)
9+
['-01:23:44.876544', '0f00000400000c010000000001172c00600d00'], // DATE_ADD(CONVERT('-01:23:45', TIME), INTERVAL 0.123456 SECOND)
10+
['-81:23:44.876544', '0f00000400000c010300000009172c00600d00'], // DATE_ADD(CONVERT('-81:23:45', TIME), INTERVAL 0.123456 SECOND)
11+
['81:23:45', '0b000004000008000300000009172d'], // CONVERT('81:23:45', TIME)
12+
['123:23:45.123456', '0f00000400000c000500000003172d40e20100'], // DATE_ADD(CONVERT('123:23:45', TIME), INTERVAL 0.123456 SECOND)
13+
['-121:23:45', '0b000004000008010500000001172d'], // CONVERT('-121:23:45', TIME)
14+
['-01:23:44.88', '0f00000400000c010000000001172c806d0d00'] //DATE_ADD(CONVERT('-01:23:45', TIME), INTERVAL 0.12 SECOND)
15+
].forEach(([expected, buffer]) => {
16+
const buf = Buffer.from(buffer, 'hex');
17+
const packet = new packets.Packet(4, buf, 0, buf.length);
18+
packet.readInt16(); // unused
19+
const d = packet.readTimeString(false);
20+
assert.equal(d, expected);
21+
});

0 commit comments

Comments
 (0)