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

Parsing ambiguous input with a timezone (Redux) #93

Merged
merged 1 commit into from
Jun 19, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@
}
},

parse : function (timestamp) {
var target = +timestamp,
offsets = this.offsets,
untils = this.untils,
i;

for (i = 0; i < untils.length; i++) {
if (target < untils[i] - (offsets[i] * 60000)) {
return offsets[i];
}
}
},

abbr : function (mom) {
return this.abbrs[this._index(mom)];
},
Expand Down Expand Up @@ -206,19 +219,27 @@
}
}

function needsOffset (m) {
return !!(m._a && (m._tzm === undefined));
}

/************************************
moment.tz namespace
************************************/

function tz () {
var args = [], i, len = arguments.length - 1;
for (i = 0; i < len; i++) {
args[i] = arguments[i];
var args = Array.prototype.slice.call(arguments, 0, -1),
name = arguments[arguments.length - 1],
zone = getZone(name),
out = moment.utc.apply(null, args);

if (zone && needsOffset(out)) {
out.add('minutes', zone.parse(out));
}
var m = moment.apply(null, args);
var preTzOffset = m.zone();
m.tz(arguments[len]);
return m.add('minutes', m.zone() - preTzOffset);

out.tz(name);

return out;
}

tz.version = VERSION;
Expand All @@ -231,6 +252,7 @@
tz.Zone = Zone;
tz.unpack = unpack;
tz.unpackBase60 = unpackBase60;
tz.needsOffset = needsOffset;

/************************************
Interface with Moment.js
Expand Down
73 changes: 73 additions & 0 deletions tests/moment-timezone/needs-offset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use strict";

var moment = require("../../index"),
needsOffset = moment.tz.needsOffset;

exports['needs-offset'] = {
'Array' : function (t) {
t.ok(needsOffset(moment([2010, 0, 1])), 'Parsing an array needs an offset.');
t.ok(needsOffset(moment.utc([2010, 0, 1])), 'Parsing an array needs an offset.');
t.done();
},

'Now' : function (t) {
t.ok(!needsOffset(moment()), 'Parsing now does not need an offset.');
t.ok(!needsOffset(moment.utc()), 'Parsing now does not need an offset.');
t.done();
},

'String + Format' : function (t) {
t.ok(needsOffset(moment("Mar 4 2010", "MMM D YYYY")), 'Parsing a string and format needs an offset.');
t.ok(needsOffset(moment.utc("Mar 4 2010", "MMM D YYYY")), 'Parsing a string and format needs an offset.');
t.done();
},

'String + Format + Offset' : function (t) {
t.ok(!needsOffset(moment("Mar 4 2010 +1000", "MMM D YYYY Z")), 'Parsing a string and format and offset does not need an offset.');
t.ok(!needsOffset(moment.utc("Mar 4 2010 +1000", "MMM D YYYY Z")), 'Parsing a string and format and offset does not need an offset.');
t.ok(!needsOffset(moment("Mar 4 2010 +10:00", "MMM D YYYY ZZ")), 'Parsing a string and format and offset does not need an offset.');
t.ok(!needsOffset(moment.utc("Mar 4 2010 +10:00", "MMM D YYYY ZZ")), 'Parsing a string and format and offset does not need an offset.');
t.done();
},

'String + Formats' : function (t) {
var formats = ["YYYY-MM-DD", "MMM D YYYY"];
t.ok(needsOffset(moment("Mar 4 2010", formats)), 'Parsing a string and formats needs an offset.');
t.ok(needsOffset(moment.utc("Mar 4 2010", formats)), 'Parsing a string and formats needs an offset.');
t.done();
},

'ISO 8601 String' : function (t) {
t.ok(needsOffset(moment("2011-10-10 10:10:10")), 'Parsing an ISO 8601 string without an offset needs an offset.');
t.ok(needsOffset(moment.utc("2011-10-10 10:10:10")), 'Parsing an ISO 8601 string without an offset needs an offset.');
t.ok(!needsOffset(moment("2011-10-10 10:10:10+10:00")), 'Parsing an ISO 8601 string with an offset does not need an offset.');
t.ok(!needsOffset(moment.utc("2011-10-10 10:10:10+10:00")), 'Parsing an ISO 8601 string with an offset does not need an offset.');
t.ok(!needsOffset(moment("2011-10-10 10:10:10+00:00")), 'Parsing an ISO 8601 string with an offset does not need an offset.');
t.ok(!needsOffset(moment.utc("2011-10-10 10:10:10+00:00")), 'Parsing an ISO 8601 string with an offset does not need an offset.');
t.done();
},

'Object' : function (t) {
t.ok(needsOffset(moment({y : 2010, M : 3, d : 1})), 'Parsing an object needs an offset.');
t.ok(needsOffset(moment({year : 2010, month : 3, day : 1})), 'Parsing an object needs an offset.');
t.ok(needsOffset(moment.utc({y : 2010, M : 3, d : 1})), 'Parsing an object needs an offset.');
t.done();
},

'Unix Offset' : function (t) {
t.ok(!needsOffset(moment(1318781876406)), 'Parsing unix timestamp in milliseconds does not need an offset.');
t.ok(!needsOffset(moment.utc(1318781876406)), 'Parsing unix timestamp in milliseconds does not need an offset.');
t.done();
},

'Unix Timestamp' : function (t) {
t.ok(!needsOffset(moment.unix(1318781876)), 'Parsing unix timestamp in seconds does not need an offset.');
t.done();
},

'Date' : function (t) {
t.ok(!needsOffset(moment(new Date())), 'Parsing a date object does not need an offset.');
t.ok(!needsOffset(moment.utc(new Date())), 'Parsing a date object does not need an offset.');
t.done();
}
};
114 changes: 114 additions & 0 deletions tests/moment-timezone/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"use strict";

var moment = require("../../index");

var Los_Angeles = "America/Los_Angeles|PST PDT PWT PPT|80 70 70 70|010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261q0 1nX0 11B0 1nX0 SgN0 8x10 iy0 5Wp0 1Vb0 3dB0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0";
var New_York = "America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0";

exports.parse = {
setUp : function (done) {
moment.tz.add([Los_Angeles, New_York]);
done();
},

"ambiguous input losing an hour - America/Los_Angeles" : function (t) {
// the hour from 2am to 3am does not exist on March 11 2011 in America/Los_Angeles
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "01 59 59 -08:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -08:00", "During the lost hour, the time should fall back to the previous time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -08:00", "During the lost hour, the time should fall back to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -07:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 480, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 480, "During the lost hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 480, "During the lost hour, the offset should match the non-dst offset");
t.equal( after.zone(), 420, "After the lost hour, the time should match the dst offset");

t.done();
},

"ambiguous input losing an hour - America/New_York" : function (t) {
// the hour from 2am to 3am does not exist on March 11 2011 in America/New_York
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/New_York"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "01 59 59 -05:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -05:00", "During the lost hour, the time should fall back to the previous time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -05:00", "During the lost hour, the time should fall back to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -04:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 300, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 300, "During the lost hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 300, "During the lost hour, the offset should match the non-dst offset");
t.equal( after.zone(), 240, "After the lost hour, the time should match the dst offset");

t.done();
},

"ambiguous input gaining an hour - America/Los_Angeles" : function (t) {
// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "00 59 59 -07:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -07:00", "During the duplicated hour, the time should match the input time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -07:00", "During the duplicated hour, the time should match the input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -08:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 420, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 420, "During the duplicated hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 420, "During the duplicated hour, the offset should match the dst offset");
t.equal( after.zone(), 480, "After the duplicated hour, the time should match the non-dst offset");

t.done();
},

"ambiguous input gaining an hour - America/New_York" : function (t) {
// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/New_York"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "00 59 59 -04:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -04:00", "During the duplicated hour, the time should match the input time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -04:00", "During the duplicated hour, the time should match the input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -05:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 240, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 240, "During the duplicated hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 240, "During the duplicated hour, the offset should match the dst offset");
t.equal( after.zone(), 300, "After the duplicated hour, the time should match the non-dst offset");

t.done();
},

"check needsOffset in moment.tz" : function (t) {
var name = "America/Los_Angeles",
tests = [
[moment.tz([2012, 5, 1], name), "2012-06-01 00:00:00 -07:00", "[2012, 5, 1]"],
[moment.tz("2012-06-01", name), "2012-06-01 00:00:00 -07:00", "2012-06-01"],
[moment.tz("2012-06-01 07:00:00+00:00", name), "2012-06-01 00:00:00 -07:00", "2012-06-01 00:00:00+00:00"],
[moment.tz(1338534000000, name), "2012-06-01 00:00:00 -07:00", "1338534000000"],
[moment.tz(new Date(1338534000000), name), "2012-06-01 00:00:00 -07:00", "new Date(1338534000000)"],
[moment.tz({y : 2012, M : 5, d : 1}, name), "2012-06-01 00:00:00 -07:00", "{y : 2012, M : 5, d : 1}"]
], i, actual, message, expected;

for (i = 0; i < tests.length; i++) {
actual = tests[i][0].format('YYYY-MM-DD HH:mm:ss Z');
expected = tests[i][1];
message = tests[i][2];
t.equal(actual, expected, "Parsing " + message + " in America/Los_Angeles should equal " + expected);
}

t.done();
},
};
28 changes: 28 additions & 0 deletions tests/moment-timezone/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ exports.zone = {
test.equal(zone._index(source), expected, "The _index for " + source + " should be " + expected);
}

test.done();
},

parse : function (test) {
var zone = new tz.Zone(PACKED),
tests = [
[( 999 - 360.5) * 60000, 360.5],
[(1000 - 360.5) * 60000, 300],

[(1099 - 300) * 60000, 300],
[(1100 - 300) * 60000, 360],

[(1239 - 360) * 60000, 360],
[(1240 - 360) * 60000, 300],

[(1339 - 300) * 60000, 300],
[(1340 - 300) * 60000, 360.5],

[(1479 - 360.5) * 60000, 360.5],
[(1480 - 360.5) * 60000, 300]
], i, source, expected;

for (i = 0; i < tests.length; i++) {
source = tests[i][0];
expected = tests[i][1];
test.equal(zone.parse(source), expected, "The parse for " + source + " should be " + expected);
}

test.done();
}
};
13 changes: 0 additions & 13 deletions tests/parse.js

This file was deleted.