11const iconv = require ( 'iconv-lite' ) ;
22const decodeJson = require ( './json_decode' ) ;
3+ const dtDecode = require ( './datetime_decode' ) ;
34const bigInt = require ( 'big-integer' ) ;
45
56const MysqlTypes = ( exports . MysqlTypes = {
@@ -331,14 +332,27 @@ const parseGeometryValue = function (buffer) {
331332// Returns false, or an object describing the fraction of a second part of a
332333// TIME, DATETIME, or TIMESTAMP.
333334const readTemporalFraction = function ( parser , fractionPrecision ) {
334- if ( ! fractionPrecision ) return undefined ;
335+ if ( ! fractionPrecision ) return false ;
335336 let fractionSize = Math . ceil ( fractionPrecision / 2 ) ;
336337 let fraction = readIntBE ( parser . _buffer , parser . _offset , fractionSize ) ;
337338 parser . _offset += fractionSize ;
338339 if ( fractionPrecision % 2 !== 0 ) fraction /= 10 ; // Not using full space
339340 if ( fraction < 0 ) fraction *= - 1 ; // Negative time, fraction not negative
340341
341- return { fraction, precision : fractionPrecision } ;
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+ } ;
342356} ;
343357
344358// This function is used to read and interpret non-null values from parser.
@@ -471,12 +485,12 @@ exports.readMysqlValue = function (
471485 break ;
472486 case MysqlTypes . DATE :
473487 raw = parseUInt24 ( parser ) ;
474-
475- result = {
476- year : sliceBits ( raw , 9 , 24 ) ,
477- month : sliceBits ( raw , 5 , 9 ) ,
478- day : sliceBits ( raw , 0 , 5 )
479- } ;
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+ ) ;
480494 break ;
481495 case MysqlTypes . TIME :
482496 raw = parseUInt24 ( parser ) ;
@@ -509,31 +523,35 @@ exports.readMysqlValue = function (
509523 minute = sliceBits ( raw , 6 , 12 ) ;
510524 second = sliceBits ( raw , 0 , 6 ) ;
511525
512- if ( isNegative && ( fraction === undefined || fraction . value === 0 ) ) {
526+ if ( isNegative && ( fraction === false || fraction . value === 0 ) ) {
513527 second ++ ;
514528 }
515529
516- result = {
517- isNegative,
518- hour,
519- minute ,
520- second ,
521- fraction
522- } ;
530+ result =
531+ ( isNegative ? '-' : '' ) +
532+ zeroPad ( hour , hour > 99 ? 3 : 2 ) +
533+ ':' +
534+ zeroPad ( minute , 2 ) +
535+ ':' +
536+ zeroPad ( second , 2 ) ;
523537
538+ if ( fraction !== false ) {
539+ result += dtDecode . getFractionString ( fraction ) ;
540+ }
524541 break ;
525542 case MysqlTypes . DATETIME :
526543 raw = parseUInt64 ( parser ) ;
527544 date = Math . floor ( raw / 1000000 ) ;
528545 time = raw % 1000000 ;
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- } ;
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+ ) ;
537555 break ;
538556 case MysqlTypes . DATETIME2 : {
539557 // Overlapping high-low to get all data in 32-bit numbers
@@ -543,30 +561,31 @@ exports.readMysqlValue = function (
543561 fraction = readTemporalFraction ( parser , column . metadata . decimals ) ;
544562
545563 yearMonth = sliceBits ( rawHigh , 14 , 31 ) ;
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- } ;
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+ ) ;
555574 break ;
556575 }
557576 case MysqlTypes . TIMESTAMP :
558577 raw = parser . parseUnsignedNumber ( 4 ) ;
559- result = {
560- secondsFromEpoch : raw
561- } ;
578+ result = dtDecode . getTimeStamp ( zongji . connection . config . dateStrings , raw ) ;
562579 break ;
563580 case MysqlTypes . TIMESTAMP2 :
564581 raw = readIntBE ( parser . _buffer , parser . _offset , 4 ) ;
565582 parser . _offset += 4 ;
566- result = {
567- secondsFromEpoch : raw ,
568- fraction : readTemporalFraction ( parser , column . metadata . decimals )
569- } ;
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
570589 break ;
571590 case MysqlTypes . YEAR :
572591 raw = parser . parseUnsignedNumber ( 1 ) ;
0 commit comments