Skip to content

Commit

Permalink
Merge pull request #348 from joekrill/issue-347-unix-timestamps-parse…
Browse files Browse the repository at this point in the history
…d-incorrectly

Check for timestamp formats when parsing
  • Loading branch information
mattjohnsonpint committed Sep 8, 2017
2 parents ac7e723 + 993ad2d commit 15d1343
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion moment-timezone.js
Expand Up @@ -469,7 +469,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) {
Expand Down
14 changes: 14 additions & 0 deletions tests/moment-timezone/needs-offset.js
Expand Up @@ -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.');
Expand All @@ -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.');
Expand Down
22 changes: 22 additions & 0 deletions tests/moment-timezone/parse.js
Expand Up @@ -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");
Expand Down

0 comments on commit 15d1343

Please sign in to comment.