]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix v2: Some numbers still led to floating-point errors
authorAsh Searle <ash@hexmen.com>
Wed, 30 Aug 2017 07:53:41 +0000 (08:53 +0100)
committerAsh Searle <ash@hexmen.com>
Wed, 30 Aug 2017 07:53:41 +0000 (08:53 +0100)
src/lib/duration/iso-string.js
src/test/moment/duration.js

index ff783e0be765f64499e4a475632a7d04d7e44af4..bac40bc6e4ce8d5a226b1adf3aa4d618512f49df 100644 (file)
@@ -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) {
index 88747b1065f65303e7ada7c967b5fee067a68a53..4d2e1c925b30e657d6c17298c4e557a1eee89453 100644 (file)
@@ -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) {