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

Check for timestamp formats when parsing #348

Merged
merged 1 commit into from
Sep 8, 2017
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
3 changes: 2 additions & 1 deletion moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions tests/moment-timezone/needs-offset.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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