From 21bad5316b1c695a60041f6bada81632a9ca5964 Mon Sep 17 00:00:00 2001 From: Tim Wood Date: Mon, 16 Jun 2014 19:20:00 -0700 Subject: [PATCH] Add support for parsing into a timezone. Add moment.tz.needsOffset to check if a newly created moment was parsed without an offset. --- moment-timezone.js | 36 ++++++-- tests/moment-timezone/needs-offset.js | 73 +++++++++++++++++ tests/moment-timezone/parse.js | 114 ++++++++++++++++++++++++++ tests/moment-timezone/zone.js | 28 +++++++ tests/parse.js | 13 --- 5 files changed, 244 insertions(+), 20 deletions(-) create mode 100644 tests/moment-timezone/needs-offset.js create mode 100644 tests/moment-timezone/parse.js delete mode 100644 tests/parse.js diff --git a/moment-timezone.js b/moment-timezone.js index 181ce513..f9360a25 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -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)]; }, @@ -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; @@ -231,6 +252,7 @@ tz.Zone = Zone; tz.unpack = unpack; tz.unpackBase60 = unpackBase60; + tz.needsOffset = needsOffset; /************************************ Interface with Moment.js diff --git a/tests/moment-timezone/needs-offset.js b/tests/moment-timezone/needs-offset.js new file mode 100644 index 00000000..5f2e81ad --- /dev/null +++ b/tests/moment-timezone/needs-offset.js @@ -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(); + } +}; diff --git a/tests/moment-timezone/parse.js b/tests/moment-timezone/parse.js new file mode 100644 index 00000000..b26529f2 --- /dev/null +++ b/tests/moment-timezone/parse.js @@ -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(); + }, +}; diff --git a/tests/moment-timezone/zone.js b/tests/moment-timezone/zone.js index 15abc3df..d31cce2c 100644 --- a/tests/moment-timezone/zone.js +++ b/tests/moment-timezone/zone.js @@ -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(); } }; diff --git a/tests/parse.js b/tests/parse.js deleted file mode 100644 index b08d58e8..00000000 --- a/tests/parse.js +++ /dev/null @@ -1,13 +0,0 @@ -var moment = require("../index"); - -exports["parse"] = { - "parse" : function (t) { - t.equal(moment.tz("2013-01-01T00:00:00", "America/New_York").format(), "2013-01-01T00:00:00-05:00", "2013-01-01T00:00:00 in America/New_York should be 2013-01-01T00:00:00-05:00"); - t.equal(moment.tz("2013-01-01T00:00:00", "America/Los_Angeles").format(), "2013-01-01T00:00:00-08:00", "2013-01-01T00:00:00 in America/Los_Angeles should be 2013-01-01T00:00:00-08:00"); - t.equal(moment.tz("2013-01-01T00:00:00", "Europe/Paris").format(), "2013-01-01T00:00:00+01:00", "2013-01-01T00:00:00 in Europe/Paris should be 2013-01-01T00:00:00+01:00"); - t.equal(moment.tz("2013-01-01T00:00:00", "Asia/Seoul").format(), "2013-01-01T00:00:00+09:00", "2013-01-01T00:00:00 in Asia/Seoul should be 2013-01-01T00:00:00+09:00"); - - t.equal(moment.tz([2013, 0, 1, 0, 0, 0], "America/New_York").format(), "2013-01-01T00:00:00-05:00", "Array constructor respects argument tzid"); - t.done(); - } -}; \ No newline at end of file