From: Ash Searle Date: Wed, 30 Aug 2017 07:53:41 +0000 (+0100) Subject: Fix v2: Some numbers still led to floating-point errors X-Git-Tag: 2.19.0~10^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b5e21f5ea55a615283441b59fd93f99d08b675c;p=thirdparty%2Fmoment.git Fix v2: Some numbers still led to floating-point errors --- diff --git a/src/lib/duration/iso-string.js b/src/lib/duration/iso-string.js index ff783e0be..bac40bc6e 100644 --- a/src/lib/duration/iso-string.js +++ b/src/lib/duration/iso-string.js @@ -1,4 +1,5 @@ import absFloor from '../utils/abs-floor'; +import zeroFill from '../utils/zero-fill'; var abs = Math.abs; export function toISOString() { @@ -22,8 +23,8 @@ export function toISOString() { // 3600 seconds -> 60 minutes -> 1 hour minutes = absFloor(seconds / 60); hours = absFloor(minutes / 60); + milliseconds %= 1000; seconds %= 60; - seconds += (milliseconds % 1000) / 1000; minutes %= 60; // 12 months -> 1 year @@ -37,7 +38,7 @@ export function toISOString() { var D = days; var h = hours; var m = minutes; - var s = seconds; + var s = (seconds || milliseconds) ? seconds + (milliseconds ? '.' + zeroFill(milliseconds, 3).replace(/0+$/, '') : '') : ''; var total = this.asSeconds(); if (!total) { diff --git a/src/test/moment/duration.js b/src/test/moment/duration.js index 88747b106..4d2e1c925 100644 --- a/src/test/moment/duration.js +++ b/src/test/moment/duration.js @@ -337,6 +337,7 @@ test('serialization to ISO 8601 duration strings', function (assert) { assert.equal(moment.duration({}).toISOString(), 'P0D', 'zero duration'); assert.equal(moment.duration({M: 16, d:40, s: 86465}).toISOString(), 'P1Y4M40DT24H1M5S', 'all fields'); assert.equal(moment.duration({ms: 123456789}).toISOString(), 'PT34H17M36.789S', 'check floating-point errors'); + assert.equal(moment.duration({ms: 31952}).toISOString(), 'PT31.952S', 'check floating-point errors'); }); test('toString acts as toISOString', function (assert) {