]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Rewrite for clarity, and add more unit tests
authorAsh Searle <ash@hexmen.com>
Tue, 8 Aug 2017 08:06:11 +0000 (09:06 +0100)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Oct 2017 22:47:18 +0000 (01:47 +0300)
src/lib/duration/iso-string.js
src/test/moment/duration.js

index 6f706c9b06edb11a8b7b866198c1ca7405be704c..067453975bd6e05b04f223f61811df2edde84417 100644 (file)
@@ -42,17 +42,17 @@ export function toISOString() {
     var s = seconds ? seconds.toFixed(3).replace(/\.?0+$/, '') : '';
     var total = this.asSeconds();
 
-    var totalSign = total < 0 ? '-' : '';
-    var ymSign = sign(total) === sign(this._months) ? '' : '-';
-    var daysSign = sign(total) === sign(this._days) ? '' : '-';
-    var hmsSign = sign(total) === sign(this._milliseconds) ? '' : '-';
-
     if (!total) {
         // this is the same as C#'s (Noda) and python (isodate)...
         // but not other JS (goog.date)
         return 'P0D';
     }
 
+    var totalSign = total < 0 ? '-' : '';
+    var ymSign = sign(this._months) != sign(total) ? '-' : '';
+    var daysSign = sign(this._days) != sign(total) ? '-' : '';
+    var hmsSign = sign(this._milliseconds) != sign(total) ? '-' : '';
+
     return totalSign + 'P' +
         (Y ? ymSign + Y + 'Y' : '') +
         (M ? ymSign + M + 'M' : '') +
index fb7c907ce1cf974d3c6653c3faefa22d3908660d..9ca6c216ec14f0a178867a512bdff4454a781640 100644 (file)
@@ -330,6 +330,11 @@ test('serialization to ISO 8601 duration strings', function (assert) {
     assert.equal(moment.duration({m: -1}).toISOString(), '-PT1M', 'one minute ago');
     assert.equal(moment.duration({s: -0.5}).toISOString(), '-PT0.5S', 'one half second ago');
     assert.equal(moment.duration({y: -1, M: 1}).toISOString(), '-P11M', 'a month after a year ago');
+    assert.equal(moment.duration({y: -1, h: 1}).toISOString(), '-P1YT-1H', 'an hour after a year ago');
+    assert.equal(moment.duration({y: -1, h: 1, m: -1}).toISOString(), '-P1YT-59M', '59 minutes after a year ago');
+    assert.equal(moment.duration({y: -1, h: 1, s: -1}).toISOString(), '-P1YT-59M-59S', '59 minutes 59 seconds after a year ago');
+    assert.equal(moment.duration({y: -1, h: -1, s: 1}).toISOString(), '-P1YT59M59S', '59 minutes 59 seconds after a year ago');
+    assert.equal(moment.duration({y: -1, d: 2}).toISOString(), '-P1Y-2D', '1 year less 2 days ago');
     assert.equal(moment.duration({M: +1}).toISOString(), 'P1M', 'one month ago');
     assert.equal(moment.duration({m: +1}).toISOString(), 'PT1M', 'one minute ago');
     assert.equal(moment.duration({s: +0.5}).toISOString(), 'PT0.5S', 'one half second ago');