Skip to content

Commit 1c6da45

Browse files
committedFeb 25, 2016
Merge pull request #943 from RivalIQ/utc-date-input
add option to parse input javascript Dates as UTC
2 parents f558ce4 + 909c0f1 commit 1c6da45

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed
 

‎lib/defaults.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ var defaults = module.exports = {
4040

4141
ssl: false,
4242

43-
application_name : undefined,
44-
fallback_application_name: undefined
43+
application_name: undefined,
44+
fallback_application_name: undefined,
45+
46+
parseInputDatesAsUTC: false
4547
};
4648

4749
//parse int8 so you can get your count values as actual numbers

‎lib/utils.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var defaults = require('./defaults');
12

23
// convert a JS array to a postgres array literal
34
// uses comma separator so won't work for types like box that use
@@ -32,7 +33,11 @@ var prepareValue = function(val, seen) {
3233
return val;
3334
}
3435
if(val instanceof Date) {
35-
return dateToString(val);
36+
if(defaults.parseInputDatesAsUTC) {
37+
return dateToStringUTC(val);
38+
} else {
39+
return dateToString(val);
40+
}
3641
}
3742
if(Array.isArray(val)) {
3843
return arrayString(val);
@@ -59,13 +64,14 @@ function prepareObject(val, seen) {
5964
return JSON.stringify(val);
6065
}
6166

67+
function pad(number, digits) {
68+
number = "" +number;
69+
while(number.length < digits)
70+
number = "0" + number;
71+
return number;
72+
}
73+
6274
function dateToString(date) {
63-
function pad(number, digits) {
64-
number = ""+number;
65-
while(number.length < digits)
66-
number = "0"+number;
67-
return number;
68-
}
6975

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

95+
function dateToStringUTC(date) {
96+
97+
var ret = pad(date.getUTCFullYear(), 4) + '-' +
98+
pad(date.getUTCMonth() + 1, 2) + '-' +
99+
pad(date.getUTCDate(), 2) + 'T' +
100+
pad(date.getUTCHours(), 2) + ':' +
101+
pad(date.getUTCMinutes(), 2) + ':' +
102+
pad(date.getUTCSeconds(), 2) + '.' +
103+
pad(date.getUTCMilliseconds(), 3);
104+
105+
return ret + "+00:00";
106+
}
107+
89108
function normalizeQueryConfig (config, values, callback) {
90109
//can take in strings or config objects
91110
config = (typeof(config) == 'string') ? { text: config } : config;

‎test/unit/utils-tests.js

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ test('prepareValues: date prepared properly', function() {
6565
helper.resetTimezoneOffset();
6666
});
6767

68+
test('prepareValues: date prepared properly as UTC', function() {
69+
defaults.parseInputDatesAsUTC = true;
70+
71+
// make a date in the local timezone that represents a specific UTC point in time
72+
var date = new Date(Date.UTC(2014, 1, 1, 11, 11, 1, 7));
73+
var out = utils.prepareValue(date);
74+
assert.strictEqual(out, "2014-02-01T11:11:01.007+00:00");
75+
76+
defaults.parseInputDatesAsUTC = false;
77+
});
78+
6879
test('prepareValues: undefined prepared properly', function() {
6980
var out = utils.prepareValue(void 0);
7081
assert.strictEqual(out, null);

0 commit comments

Comments
 (0)
Please sign in to comment.