Skip to content

Commit d9ffb9d

Browse files
committed
Export structured time values
1 parent de5096e commit d9ffb9d

File tree

5 files changed

+168
-200
lines changed

5 files changed

+168
-200
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ The `ZongJi` constructor accepts one argument of either:
7575

7676
If a `Connection` or `Pool` object is passed to the constructor, it will not be destroyed/ended by Zongji's `stop()` method.
7777

78-
If there is a `dateStrings` `mysql` configuration option in the connection details or connection, `ZongJi` will follow it.
79-
8078
Each instance includes the following methods:
8179

8280
| Method Name | Arguments | Description |
@@ -139,7 +137,6 @@ Neither method requires any arguments.
139137
- :star2: [All types allowed by `mysql`](https://github.com/mysqljs/mysql#type-casting) are supported by this package.
140138
- :speak_no_evil: 64-bit integer is supported via package big-integer(see #108). If an integer is within the safe range of JS number (-2^53, 2^53), a Number object will returned, otherwise, will return as String.
141139
- :point_right: `TRUNCATE` statement does not cause corresponding `DeleteRows` event. Use unqualified `DELETE FROM` for same effect.
142-
- When using fractional seconds with `DATETIME` and `TIMESTAMP` data types in MySQL > 5.6.4, only millisecond precision is available due to the limit of Javascript's `Date` object.
143140

144141
## Run Tests
145142

lib/common.js

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const iconv = require('iconv-lite');
22
const decodeJson = require('./json_decode');
3-
const dtDecode = require('./datetime_decode');
43
const bigInt = require('big-integer');
54

65
const MysqlTypes = (exports.MysqlTypes = {
@@ -332,27 +331,14 @@ const parseGeometryValue = function (buffer) {
332331
// Returns false, or an object describing the fraction of a second part of a
333332
// TIME, DATETIME, or TIMESTAMP.
334333
const readTemporalFraction = function (parser, fractionPrecision) {
335-
if (!fractionPrecision) return false;
334+
if (!fractionPrecision) return undefined;
336335
let fractionSize = Math.ceil(fractionPrecision / 2);
337336
let fraction = readIntBE(parser._buffer, parser._offset, fractionSize);
338337
parser._offset += fractionSize;
339338
if (fractionPrecision % 2 !== 0) fraction /= 10; // Not using full space
340339
if (fraction < 0) fraction *= -1; // Negative time, fraction not negative
341340

342-
let milliseconds;
343-
if (fractionPrecision > 3) {
344-
milliseconds = Math.floor(fraction / Math.pow(10, fractionPrecision - 3));
345-
} else if (fractionPrecision < 3) {
346-
milliseconds = fraction * Math.pow(10, 3 - fractionPrecision);
347-
} else {
348-
milliseconds = fraction;
349-
}
350-
351-
return {
352-
value: fraction, // the integer after the decimal place
353-
precision: fractionPrecision, // the number of digits after the decimal
354-
milliseconds: milliseconds // the unrounded 3 digits after the decimal
355-
};
341+
return { fraction, precision: fractionPrecision };
356342
};
357343

358344
// This function is used to read and interpret non-null values from parser.
@@ -485,12 +471,12 @@ exports.readMysqlValue = function (
485471
break;
486472
case MysqlTypes.DATE:
487473
raw = parseUInt24(parser);
488-
result = dtDecode.getDate(
489-
zongji.connection.config.dateStrings, // node-mysql dateStrings option
490-
sliceBits(raw, 9, 24), // year
491-
sliceBits(raw, 5, 9), // month
492-
sliceBits(raw, 0, 5) // day
493-
);
474+
475+
result = {
476+
year: sliceBits(raw, 9, 24),
477+
month: sliceBits(raw, 5, 9),
478+
day: sliceBits(raw, 0, 5)
479+
};
494480
break;
495481
case MysqlTypes.TIME:
496482
raw = parseUInt24(parser);
@@ -523,35 +509,31 @@ exports.readMysqlValue = function (
523509
minute = sliceBits(raw, 6, 12);
524510
second = sliceBits(raw, 0, 6);
525511

526-
if (isNegative && (fraction === false || fraction.value === 0)) {
512+
if (isNegative && (fraction === undefined || fraction.value === 0)) {
527513
second++;
528514
}
529515

530-
result =
531-
(isNegative ? '-' : '') +
532-
zeroPad(hour, hour > 99 ? 3 : 2) +
533-
':' +
534-
zeroPad(minute, 2) +
535-
':' +
536-
zeroPad(second, 2);
516+
result = {
517+
isNegative,
518+
hour,
519+
minute,
520+
second,
521+
fraction
522+
};
537523

538-
if (fraction !== false) {
539-
result += dtDecode.getFractionString(fraction);
540-
}
541524
break;
542525
case MysqlTypes.DATETIME:
543526
raw = parseUInt64(parser);
544527
date = Math.floor(raw / 1000000);
545528
time = raw % 1000000;
546-
result = dtDecode.getDateTime(
547-
zongji.connection.config.dateStrings, // node-mysql dateStrings option
548-
Math.floor(date / 10000), // year
549-
Math.floor((date % 10000) / 100), // month
550-
date % 100, // day
551-
Math.floor(time / 10000), // hour
552-
Math.floor((time % 10000) / 100), // minutes
553-
time % 100 // seconds
554-
);
529+
result = {
530+
year: Math.floor(date / 10000),
531+
month: Math.floor((date % 10000) / 100),
532+
day: date % 100,
533+
hour: Math.floor(time / 10000),
534+
minute: Math.floor((time % 10000) / 100),
535+
second: time % 100
536+
};
555537
break;
556538
case MysqlTypes.DATETIME2: {
557539
// Overlapping high-low to get all data in 32-bit numbers
@@ -561,31 +543,30 @@ exports.readMysqlValue = function (
561543
fraction = readTemporalFraction(parser, column.metadata.decimals);
562544

563545
yearMonth = sliceBits(rawHigh, 14, 31);
564-
result = dtDecode.getDateTime(
565-
zongji.connection.config.dateStrings, // node-mysql dateStrings option
566-
Math.floor(yearMonth / 13), // year
567-
yearMonth % 13, // month
568-
sliceBits(rawLow, 17, 22), // day
569-
sliceBits(rawLow, 12, 17), // hour
570-
sliceBits(rawLow, 6, 12), // minutes
571-
sliceBits(rawLow, 0, 6), // seconds
572-
fraction // fraction of a second object
573-
);
546+
result = {
547+
year: Math.floor(yearMonth / 13),
548+
month: yearMonth % 13,
549+
day: sliceBits(rawLow, 17, 22),
550+
hour: sliceBits(rawLow, 12, 17),
551+
minute: sliceBits(rawLow, 6, 12),
552+
second: sliceBits(rawLow, 0, 6),
553+
fraction
554+
};
574555
break;
575556
}
576557
case MysqlTypes.TIMESTAMP:
577558
raw = parser.parseUnsignedNumber(4);
578-
result = dtDecode.getTimeStamp(zongji.connection.config.dateStrings, raw);
559+
result = {
560+
secondsFromEpoch: raw
561+
};
579562
break;
580563
case MysqlTypes.TIMESTAMP2:
581564
raw = readIntBE(parser._buffer, parser._offset, 4);
582565
parser._offset += 4;
583-
fraction = readTemporalFraction(parser, column.metadata.decimals);
584-
result = dtDecode.getTimeStamp(
585-
zongji.connection.config.dateStrings,
586-
raw, // seconds from epoch
587-
fraction
588-
); // fraction of a second object
566+
result = {
567+
secondsFromEpoch: raw,
568+
fraction: readTemporalFraction(parser, column.metadata.decimals)
569+
};
589570
break;
590571
case MysqlTypes.YEAR:
591572
raw = parser.parseUnsignedNumber(1);

lib/datetime_decode.js

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)