Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/protocol/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ exports = module.exports = Writer;

var REGEX = {
DATE: /(\d{4})-(\d{2})-(\d{2})/,
TIME: /(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/,
TIMESTAMP: /(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/,
TIME: /(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?/,
TIMESTAMP: /(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?/,
// DECIMAL will match "" and ".", both of which are invalid, requires an
// additional check
DECIMAL: /^([+-])?(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/
Expand Down Expand Up @@ -728,7 +728,8 @@ Writer.prototype[TypeCode.TIME] = function writeTime(value) {
}
hours = ~~time[1];
minutes = ~~time[2];
milliseconds = Math.round(time[3] * 1000);
var decMilli = strToNumLen(time[4], 3);
milliseconds = time[3] * 1000 + decMilli;
} else {
throw createInputError('TIME');
}
Expand Down Expand Up @@ -776,7 +777,8 @@ Writer.prototype[TypeCode.TIMESTAMP] = function writeTimestamp(value) {
day = ~~ts[3];
hours = ~~ts[4];
minutes = ~~ts[5];
milliseconds = Math.round(ts[6] * 1000);
var decMilli = strToNumLen(ts[7], 3);
milliseconds = ts[6] * 1000 + decMilli;
} else {
throw createInputError('TIMESTAMP');
}
Expand Down Expand Up @@ -861,7 +863,7 @@ Writer.prototype[TypeCode.LONGDATE] = function writeLongDate(value) {
hours = ~~ts[4];
minutes = ~~ts[5];
seconds = ~~ts[6];
nanoseconds = ~~((ts[6] * 1000000000) % 1000000000);
nanoseconds = strToNumLen(ts[7], 9);
} else {
throw createInputError('LONGDATE');
}
Expand Down Expand Up @@ -1105,6 +1107,14 @@ function truncateDecimalToExp(decimal, minExp) {
};
}

// Truncates / pads a decimal string to get a fixed number of decimal places as an integer
function strToNumLen(str, len) {
if (str === undefined) {
return 0;
}
return Number(str.length > len ? str.substring(0, len) : str + util.ZEROS[len - str.length]);
}

function createInputError(type) {
return new Error(util.format('Wrong input for %s type', type));
}
Expand Down
Loading