Skip to content

Commit

Permalink
Merge pull request #1187 from icambron/fix-trailing-z
Browse files Browse the repository at this point in the history
fixes for Z in ISO parser
  • Loading branch information
ichernev committed Oct 15, 2013
2 parents 6b2784c + d7c134a commit d3f7978
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 5 additions & 4 deletions moment.js
Expand Up @@ -54,7 +54,7 @@

// preliminary iso regex
// 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000)
isoRegex = /^\s*\d{4}-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d\d?\d?)?)?)?)?([\+\-]\d\d:?\d\d)?)?$/,
isoRegex = /^\s*\d{4}-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d\d?\d?)?)?)?)?([\+\-]\d\d:?\d\d|Z)?)?$/,

isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',

Expand Down Expand Up @@ -539,7 +539,8 @@
nullInput : false,
invalidMonth : null,
invalidFormat : false,
userInvalidated : false
userInvalidated : false,
iso: false
};
}

Expand Down Expand Up @@ -1310,6 +1311,7 @@
match = isoRegex.exec(string);

if (match) {
config._pf.iso = true;
for (i = 4; i > 0; i--) {
if (match[i]) {
// match[5] should be "T" or undefined
Expand All @@ -1324,7 +1326,7 @@
}
}
if (parseTokenTimezone.exec(string)) {
config._f += " Z";
config._f += "Z";
}
makeDateFromStringAndFormat(config);
}
Expand Down Expand Up @@ -1491,7 +1493,6 @@
config._i = input = getLangDefinition().preparse(input);
}


if (moment.isMoment(input)) {
config = extend({}, input);

Expand Down
29 changes: 29 additions & 0 deletions test/moment/create.js
Expand Up @@ -485,6 +485,35 @@ exports.create = {
test.done();
},

"parsing ISO with Z" : function (test) {
var i, mom, formats = [
['2011-10-08T18:04Z', '2011-10-08T18:04:00.000'],
['2011-10-08T18:04:20Z', '2011-10-08T18:04:20.000'],
['2011-10-08T18:04:20.1Z', '2011-10-08T18:04:20.100'],
['2011-10-08T18:04:20.11Z', '2011-10-08T18:04:20.110'],
['2011-10-08T18:04:20.111Z', '2011-10-08T18:04:20.111'],
['2011-W40-6T18Z', '2011-10-08T18:00:00.000'],
['2011-W40-6T18:04Z', '2011-10-08T18:04:00.000'],
['2011-W40-6T18:04:20Z', '2011-10-08T18:04:20.000'],
['2011-W40-6T18:04:20.1Z', '2011-10-08T18:04:20.100'],
['2011-W40-6T18:04:20.11Z', '2011-10-08T18:04:20.110'],
['2011-W40-6T18:04:20.111Z', '2011-10-08T18:04:20.111'],
['2011-281T18Z', '2011-10-08T18:00:00.000'],
['2011-281T18:04Z', '2011-10-08T18:04:00.000'],
['2011-281T18:04:20Z', '2011-10-08T18:04:20.000'],
['2011-281T18:04:20Z', '2011-10-08T18:04:20.000'],
['2011-281T18:04:20.1Z', '2011-10-08T18:04:20.100'],
['2011-281T18:04:20.11Z', '2011-10-08T18:04:20.110'],
['2011-281T18:04:20.111Z', '2011-10-08T18:04:20.111']
];

for (i = 0; i < formats.length; i++) {
mom = moment(formats[i][0]).utc();
test.equal(mom.format('YYYY-MM-DDTHH:mm:ss.SSS'), formats[i][1], "moment should be able to parse ISO in UTC " + formats[i][0]);
}
test.done();
},

"parsing iso with T" : function (test) {
test.expect(8);

Expand Down

0 comments on commit d3f7978

Please sign in to comment.