From bde871707b0ca2f16ea416a605f45999f7e60828 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Mon, 14 Jan 2013 18:07:18 +0100 Subject: [PATCH] Storing timezone-less dates in local time instead of UTC Issue #225 caused such dates to be read, but not stored in local time. --- lib/utils.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 7bbbe5b..9dc5453 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -47,7 +47,7 @@ function arrayString(val) { //for complex types, etc... var prepareValue = function(val) { if(val instanceof Date) { - return JSON.stringify(val); + return dateToString(val); } if(typeof val === 'undefined') { return null; @@ -58,6 +58,33 @@ var prepareValue = function(val) { return val === null ? null : val.toString(); }; +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) + '-' + + pad(date.getMonth() + 1, 2) + '-' + + pad(date.getDate(), 2) + 'T' + + pad(date.getHours(), 2) + ':' + + pad(date.getMinutes(), 2) + ':' + + pad(date.getSeconds(), 2) + '.' + + pad(date.getMilliseconds(), 3); + + if(offset < 0) { + ret += "-"; + offset *= -1; + } + else + ret += "+"; + + return ret + pad(Math.floor(offset/60), 2) + ":" + pad(offset%60, 2); +} + function normalizeQueryConfig (config, values, callback) { //can take in strings or config objects config = (typeof(config) == 'string') ? { text: config } : config;