From 2c012ad2c3327deb75bb61eb994b0f769e4a3ba4 Mon Sep 17 00:00:00 2001 From: Chaoran Yang Date: Thu, 11 Jul 2013 17:06:48 -0500 Subject: [PATCH 1/3] Add milliseconds part when converting Date objects. --- lib/protocol/SqlString.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/protocol/SqlString.js b/lib/protocol/SqlString.js index 98b3b3274..a8aafedfc 100644 --- a/lib/protocol/SqlString.js +++ b/lib/protocol/SqlString.js @@ -86,13 +86,14 @@ SqlString.dateToString = function(date, timeZone) { } var year = dt.getFullYear(); - var month = zeroPad(dt.getMonth() + 1); - var day = zeroPad(dt.getDate()); - var hour = zeroPad(dt.getHours()); - var minute = zeroPad(dt.getMinutes()); - var second = zeroPad(dt.getSeconds()); - - return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; + var month = zeroPad(dt.getMonth() + 1, 2); + var day = zeroPad(dt.getDate(), 2); + var hour = zeroPad(dt.getHours(), 2); + var minute = zeroPad(dt.getMinutes(), 2); + var second = zeroPad(dt.getSeconds(), 2); + var millisecond = zeroPad(dt.getMilliSeconds(), 3); + + return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milliseconds; }; SqlString.bufferToString = function(buffer) { @@ -124,8 +125,13 @@ SqlString.objectToValues = function(object, timeZone) { return values.join(', '); }; -function zeroPad(number) { - return (number < 10) ? '0' + number : number; +function zeroPad(number, length) { + number = number.toString(); + while (number.length < length) { + number = '0' + number; + } + + return number; } function convertTimezone(tz) { From a1cbf335729c89fb513c6d0171aac55930e48878 Mon Sep 17 00:00:00 2001 From: Chaoran Yang Date: Fri, 12 Jul 2013 09:06:04 -0500 Subject: [PATCH 2/3] Fix typos. --- lib/protocol/SqlString.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/protocol/SqlString.js b/lib/protocol/SqlString.js index a8aafedfc..8e698e54e 100644 --- a/lib/protocol/SqlString.js +++ b/lib/protocol/SqlString.js @@ -91,9 +91,9 @@ SqlString.dateToString = function(date, timeZone) { var hour = zeroPad(dt.getHours(), 2); var minute = zeroPad(dt.getMinutes(), 2); var second = zeroPad(dt.getSeconds(), 2); - var millisecond = zeroPad(dt.getMilliSeconds(), 3); + var millisecond = zeroPad(dt.getMilliseconds(), 3); - return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milliseconds; + return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + millisecond; }; SqlString.bufferToString = function(buffer) { From f52e99ef04d57ac04059e1996c639607e836fbf5 Mon Sep 17 00:00:00 2001 From: Chaoran Yang Date: Fri, 12 Jul 2013 10:03:33 -0500 Subject: [PATCH 3/3] Fix test-SqlString to include the milliseconds for Date objects. --- test/unit/protocol/test-SqlString.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/protocol/test-SqlString.js b/test/unit/protocol/test-SqlString.js index 64423e528..c25445162 100644 --- a/test/unit/protocol/test-SqlString.js +++ b/test/unit/protocol/test-SqlString.js @@ -85,9 +85,9 @@ test('SqlString.escape', { assert.equal(SqlString.escape('Sup"er'), "'Sup\\\"er'"); }, - 'dates are converted to YYYY-MM-DD HH:II:SS': function() { - var expected = '2012-05-07 11:42:03'; - var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3)); + 'dates are converted to YYYY-MM-DD HH:II:SS.sss': function() { + var expected = '2012-05-07 11:42:03.002'; + var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2)); var string = SqlString.escape(date); assert.strictEqual(string, "'" + expected + "'");