From: Nicholas Bollweg Date: Wed, 14 Aug 2013 18:01:50 +0000 (-0400) Subject: adding duration toIsoString, tests X-Git-Tag: 2.3.0~10^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed57803fa182c730283122ca7462f2890a347cb9;p=thirdparty%2Fmoment.git adding duration toIsoString, tests --- diff --git a/moment.js b/moment.js index 63c9252cc..7dd6ddcaa 100644 --- a/moment.js +++ b/moment.js @@ -282,7 +282,6 @@ this._bubble(); } - /************************************ Helpers ************************************/ @@ -1640,7 +1639,21 @@ return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's'](); }, - lang : moment.fn.lang + lang : moment.fn.lang, + + toIsoString : function () { + // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js + return (this.asSeconds() < 0 ? '-' : '') + + 'P' + + (this.years() ? Math.abs(this.years()) + 'Y' : '') + + (this.months() ? Math.abs(this.months()) + 'M' : '') + + (this.days() ? Math.abs(this.days()) + 'D' : '') + + ((this.hours() || this.minutes() || this.seconds()) ? 'T' : '') + + (this.hours() ? Math.abs(this.hours()) + 'H' : '') + + (this.minutes() ? Math.abs(this.minutes()) + 'M' : '') + + ((this.seconds() || this.milliseconds()) ? + (Math.abs(this.seconds() + this.milliseconds() / 1000.0)) + 'S' : ''); + } }; function makeDurationGetter(name) { diff --git a/test/moment/duration.js b/test/moment/duration.js index 5d2953d95..1cee4fc6f 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -198,17 +198,26 @@ exports.duration = { test.done(); }, - "instatiation from iso 8601 duration" : function (test) { - test.expect(7); + "instantiation 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.equal(moment.duration("-P60D").asSeconds(), moment.duration({d: -60}).asSeconds(), "negative days"); + test.equal(moment.duration("PT0.5S").asSeconds(), moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).asSeconds(), "fractional seconds"); test.done(); }, + + "serialization to ISO 8601 duration strings" : function (test) { + test.expect(4); + test.equal(moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).toIsoString() , "P1Y2M3DT4H5M6S", "all fields"); + test.equal(moment.duration({M: -1}).toIsoString() , "-P1M", "one month ago"); + test.equal(moment.duration({m: -1}).toIsoString() , "-PT1M", "one minute ago"); + test.equal(moment.duration({s: -1}).toIsoString() , "-PT0.5S", "one half second ago"); + test.equal(moment.duration({y: -0.5, M: 1}).toIsoString() , "-P5M", "a month after half a year ago"); + test.done(); + }, "humanize" : function (test) { test.expect(32);