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

Assume local timezone when unspecified #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var BBEvents = require('backbone-events-standalone');
var KeyTree = require('key-tree-store');
var arrayNext = require('array-next');
var changeRE = /^change:/;
var timezoneRE = /Z|[-+]\d\d:\d\d/;

function Base(attrs, options) {
options || (options = {});
Expand Down Expand Up @@ -594,6 +595,11 @@ var dataTypes = {
// If the newVal cant be parsed, then try parseInt first
dateVal = new Date(parseInt(newVal, 10)).valueOf();
if (isNaN(dateVal)) throw TypeError;
} else if (isNaN(Number(newVal)) && !timezoneRE.test(newVal)) {
// Interpret ISO 8601 Date strings that do not specify a
// time zone in the local system time zone, as per ECMAScript 6:
// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-date-time-string-format
dateVal += new Date().getTimezoneOffset() * 60 * 1000;
}
newVal = dateVal;
newType = 'date';
Expand Down
6 changes: 5 additions & 1 deletion test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1480,9 +1480,13 @@ test("#99 #101 - string dates can be parsed", function(t) {
t.ok(isDate(model.today));
t.equal(model.today.toJSON(), isoString, 'date should accept a native date object');

model.today = 0;
t.ok(isDate(model.today));
t.equal(model.today.toJSON(), new Date(0).toJSON(), 'date should accept zero');

model.today = '2014-11-13';
t.ok(isDate(model.today));
t.equal(model.today.toJSON(), '2014-11-13T00:00:00.000Z', 'date should accept YYYY-MM-DD');
t.equal(model.today.toJSON(), new Date(2014, 10, 13).toJSON(), 'date should accept YYYY-MM-DD');

model.today = '2014-11-13T21:01Z';
t.ok(isDate(model.today));
Expand Down