Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to parse input javascript Dates as UTC #943

Merged
merged 1 commit into from Feb 25, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/defaults.js
Expand Up @@ -40,8 +40,10 @@ var defaults = module.exports = {

ssl: false,

application_name : undefined,
fallback_application_name: undefined
application_name: undefined,
fallback_application_name: undefined,

parseInputDatesAsUTC: false
};

//parse int8 so you can get your count values as actual numbers
Expand Down
33 changes: 26 additions & 7 deletions lib/utils.js
@@ -1,3 +1,4 @@
var defaults = require('./defaults');

// convert a JS array to a postgres array literal
// uses comma separator so won't work for types like box that use
Expand Down Expand Up @@ -32,7 +33,11 @@ var prepareValue = function(val, seen) {
return val;
}
if(val instanceof Date) {
return dateToString(val);
if(defaults.parseInputDatesAsUTC) {
return dateToStringUTC(val);
} else {
return dateToString(val);
}
}
if(Array.isArray(val)) {
return arrayString(val);
Expand All @@ -59,13 +64,14 @@ function prepareObject(val, seen) {
return JSON.stringify(val);
}

function pad(number, digits) {
number = "" +number;
while(number.length < digits)
number = "0" + number;
return number;
}

function dateToString(date) {
function pad(number, digits) {
number = ""+number;
while(number.length < digits)
number = "0"+number;
return number;
}

var offset = -date.getTimezoneOffset();
var ret = pad(date.getFullYear(), 4) + '-' +
Expand All @@ -86,6 +92,19 @@ function dateToString(date) {
return ret + pad(Math.floor(offset/60), 2) + ":" + pad(offset%60, 2);
}

function dateToStringUTC(date) {

var ret = pad(date.getUTCFullYear(), 4) + '-' +
pad(date.getUTCMonth() + 1, 2) + '-' +
pad(date.getUTCDate(), 2) + 'T' +
pad(date.getUTCHours(), 2) + ':' +
pad(date.getUTCMinutes(), 2) + ':' +
pad(date.getUTCSeconds(), 2) + '.' +
pad(date.getUTCMilliseconds(), 3);

return ret + "+00:00";
}

function normalizeQueryConfig (config, values, callback) {
//can take in strings or config objects
config = (typeof(config) == 'string') ? { text: config } : config;
Expand Down
11 changes: 11 additions & 0 deletions test/unit/utils-tests.js
Expand Up @@ -65,6 +65,17 @@ test('prepareValues: date prepared properly', function() {
helper.resetTimezoneOffset();
});

test('prepareValues: date prepared properly as UTC', function() {
defaults.parseInputDatesAsUTC = true;

// make a date in the local timezone that represents a specific UTC point in time
var date = new Date(Date.UTC(2014, 1, 1, 11, 11, 1, 7));
var out = utils.prepareValue(date);
assert.strictEqual(out, "2014-02-01T11:11:01.007+00:00");

defaults.parseInputDatesAsUTC = false;
});

test('prepareValues: undefined prepared properly', function() {
var out = utils.prepareValue(void 0);
assert.strictEqual(out, null);
Expand Down