diff --git a/src/node.js b/src/node.js index 726820fb1ea70b..67f6cdb5ceda8c 100644 --- a/src/node.js +++ b/src/node.js @@ -193,10 +193,9 @@ if (typeof ar !== 'undefined') { if (Array.isArray(ar)) { - return [ - (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0], - hrValues[2] - ar[1] - ]; + const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0]; + const nsec = hrValues[2] - ar[1]; + return [nsec < 0 ? sec - 1 : sec, nsec < 0 ? nsec + 1e9 : nsec]; } throw new TypeError('process.hrtime() only accepts an Array tuple'); diff --git a/test/parallel/test-process-hrtime.js b/test/parallel/test-process-hrtime.js index ad186a3507d1e7..db16be0ad9e812 100644 --- a/test/parallel/test-process-hrtime.js +++ b/test/parallel/test-process-hrtime.js @@ -24,3 +24,6 @@ function validateTuple(tuple) { assert(isFinite(v)); }); } + +const diff = process.hrtime([0, 1e9 - 1]); +assert(diff[1] >= 0); // https://github.com/nodejs/node/issues/4751