diff --git a/moment-timezone.js b/moment-timezone.js index cd34f516..dbe7a03c 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -464,7 +464,8 @@ } function needsOffset (m) { - return !!(m._a && (m._tzm === undefined)); + var isUnixTimestamp = (m._f === 'X' || m._f === 'x'); + return !!(m._a && (m._tzm === undefined) && !isUnixTimestamp); } function logError (message) { diff --git a/tests/moment-timezone/needs-offset.js b/tests/moment-timezone/needs-offset.js index 5f2e81ad..c55f94e7 100644 --- a/tests/moment-timezone/needs-offset.js +++ b/tests/moment-timezone/needs-offset.js @@ -21,6 +21,12 @@ exports['needs-offset'] = { t.ok(needsOffset(moment.utc("Mar 4 2010", "MMM D YYYY")), 'Parsing a string and format needs an offset.'); t.done(); }, + + 'String + Timestamp Format': function(t) { + t.ok(!needsOffset(moment.utc("1267660800000", "x")), 'Parsing a string with timestamp format "x" does not need an offset.'); + t.ok(!needsOffset(moment.utc("1267660800", "X")), 'Parsing a string with timestamp format "X" does not need 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.'); @@ -36,6 +42,14 @@ exports['needs-offset'] = { t.ok(needsOffset(moment.utc("Mar 4 2010", formats)), 'Parsing a string and formats needs an offset.'); t.done(); }, + + 'String + Timestamp Formats': function(t) { + var formats = ["x", "X", "MMM D YYYY"]; + t.ok(!needsOffset(moment.utc("1267660800000", formats)), 'Parsing a string with timestamp format "x" does not need an offset.'); + t.ok(!needsOffset(moment.utc("1267660800", formats)), 'Parsing a string with timestamp format "X" does not need an offset.'); + t.ok(needsOffset(moment("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.'); diff --git a/tests/moment-timezone/parse.js b/tests/moment-timezone/parse.js index 75810d8a..3a0bdacc 100644 --- a/tests/moment-timezone/parse.js +++ b/tests/moment-timezone/parse.js @@ -250,6 +250,28 @@ exports.parse = { t.done(); }, + + "check needsOffset in moment.tz with timestamp formats" : function (t) { + var tests = [ + ["1338534000000", "x", "America/New_York", 1338534000000, "2012-06-01T03:00:00-04:00"], + ["1338534000.000", "X", "America/New_York", 1338534000000, "2012-06-01T03:00:00-04:00"], + ["1338534000000", "x", "America/Los_Angeles", 1338534000000, "2012-06-01T00:00:00-07:00"], + ["1338534000.000", "X", "America/Los_Angeles", 1338534000000, "2012-06-01T00:00:00-07:00"] + ], i, parsed, input, format, timezone, expectedValue, expectedFormat, actualValue, actualFormat; + + for (i = 0; i < tests.length; i++) { + input = tests[i][0]; + format = tests[i][1]; + timezone = tests[i][2]; + expectedValue = tests[i][3]; + expectedFormat = tests[i][4]; + parsed = moment.tz(input, format, true, timezone); + t.equal(parsed.valueOf(), expectedValue, "Parsing " + input + " with format " + format + " should have value " + expectedValue); + t.equal(parsed.format(), expectedFormat, "Parsing " + input + " with format " + format + " should equal " + expectedFormat); + } + + t.done(); + }, "parse in default zone" : function (t) { moment.tz.setDefault("America/New_York");