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

Added iso duration parsing #941

Merged
merged 2 commits into from Oct 2, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 30 additions & 9 deletions moment.js
Expand Up @@ -22,6 +22,7 @@
// ASP.NET json date format regex
aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
aspNetTimeSpanJsonRegex = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)\:(\d+)\.?(\d{3})?/,
isoDurationRegex = /^P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,

// format tokens
formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g,
Expand Down Expand Up @@ -1051,25 +1052,45 @@
var isDuration = moment.isDuration(input),
isNumber = (typeof input === 'number'),
duration = (isDuration ? input._input : (isNumber ? {} : input)),
matched = aspNetTimeSpanJsonRegex.exec(input),
// matching against regexp is expensive, do it on demand
aspMatched = null,
isoMatched = null,
sign,
ret;
ret,
parseIso;

if (isNumber) {
if (key) {
duration[key] = input;
} else {
duration.milliseconds = input;
}
} else if (matched) {
sign = (matched[1] === "-") ? -1 : 1;
} else if (!!(aspMatched = aspNetTimeSpanJsonRegex.exec(input))) {
sign = (aspMatched[1] === "-") ? -1 : 1;
duration = {
y: 0,
d: ~~matched[2] * sign,
h: ~~matched[3] * sign,
m: ~~matched[4] * sign,
s: ~~matched[5] * sign,
ms: ~~matched[6] * sign
d: ~~aspMatched[2] * sign,
h: ~~aspMatched[3] * sign,
m: ~~aspMatched[4] * sign,
s: ~~aspMatched[5] * sign,
ms: ~~aspMatched[6] * sign
};
} else if (!!(isoMatched = isoDurationRegex.exec(input))) {
parseIso = function (inp) {
// We'd normally use ~~inp for this, but unfortunately it also
// converts floats to ints.
// inp may be undefined, so careful calling replace on it.
var res = inp && parseFloat(inp.replace(',', '.'));
return isNaN(res) ? 0 : res;
};
duration = {
y: parseIso(isoMatched[1]),
M: parseIso(isoMatched[2]),
d: parseIso(isoMatched[3]),
h: parseIso(isoMatched[4]),
m: parseIso(isoMatched[5]),
s: parseIso(isoMatched[6]),
w: parseIso(isoMatched[7]),
};
}

Expand Down
11 changes: 11 additions & 0 deletions test/moment/duration.js
Expand Up @@ -198,6 +198,17 @@ exports.duration = {
test.done();
},

"instatiation from iso 8601 duration" : function (test) {
test.expect(6);
test.equal(moment.duration('P1Y2M3DT4H5M6S').asSeconds(), moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).asSeconds(), "all fields");
test.equal(moment.duration('P1W').asSeconds(), moment.duration({d: 7}).asSeconds(), "week field");
test.equal(moment.duration('P1M').asSeconds(), moment.duration({M: 1}).asSeconds(), "single month field");
test.equal(moment.duration('PT1M').asSeconds(), moment.duration({m: 1}).asSeconds(), "single minute field");
test.equal(moment.duration('P1MT2H').asSeconds(), moment.duration({M: 1, h: 2}).asSeconds(), "random fields missing");
test.equal(moment.duration('PY1MDT2HMS').asSeconds(), moment.duration({M: 1, h: 2}).asSeconds(), "random values missing");
test.done();
},

"humanize" : function (test) {
test.expect(32);
moment.lang('en');
Expand Down