From 8d44a8352029a4702bf3608d981139cb0566ec3d Mon Sep 17 00:00:00 2001 From: Jacob Page Date: Thu, 30 Oct 2014 18:51:46 -0700 Subject: [PATCH] Store all dates as UTC for SQLite Dates are stored in SQLite as Unix timestamps. Dates are represented in JavaScript as Unix timestamps. Thus, there need not be any sort of conversions. Client-side timezones are irrelevant; they should only affect the client-side display of dates. The existing code is incorrect because it's storing timezone-offset dates _as UTC dates_ (with the "Z" suffix). This change simplifies the type conversion; a Date is stored as an ISO-8601 date string, which is understood by both JavaScript's `Date` constructor and SQLite. --- .gitignore | 1 + lib/Helpers.js | 4 ++++ test/integration/test-dialect-sqlite.js | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a58cf7a..5a6f16b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.sublime-* *.njsproj +.idea .DS_Store node_modules test.js diff --git a/lib/Helpers.js b/lib/Helpers.js index 6cd10ad..05285a5 100644 --- a/lib/Helpers.js +++ b/lib/Helpers.js @@ -18,6 +18,10 @@ module.exports.escapeQuery = function (Dialect, query, args) { module.exports.dateToString = function (date, timeZone, opts) { var dt = new Date(date); + if (opts.dialect === 'sqlite') { + return dt.toISOString(); + } + if (timeZone != 'local') { var tz = convertTimezone(timeZone); diff --git a/test/integration/test-dialect-sqlite.js b/test/integration/test-dialect-sqlite.js index 3f45662..9f8ba64 100644 --- a/test/integration/test-dialect-sqlite.js +++ b/test/integration/test-dialect-sqlite.js @@ -71,7 +71,7 @@ assert.equal( assert.equal( dialect.escapeVal(new Date(d.getTime() + tzOffsetMillis)), - "'2013-09-04T19:15:11.133Z'" + "'" + new Date(d.getTime() + tzOffsetMillis).toISOString() + "'" ); assert.equal( @@ -86,7 +86,7 @@ assert.equal( assert.equal( dialect.escapeVal(new Date(d.getTime()), '-0400'), - "'2013-09-04T15:15:11.133Z'" + "'2013-09-04T19:15:11.133Z'" ); assert.equal(