diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2016-01-19 13:40:13 +0300 |
---|---|---|
committer | Evan Lucas <evanlucas@me.com> | 2016-01-21 01:06:10 +0300 |
commit | a2c257a3ef341be7b594f31a3bef097b91e492af (patch) | |
tree | 8f9cca8a37ffcf51b45a6c1d1d53701a1e635dbc | |
parent | fe23f4241f32eed474f45b48b6dc170e7ee446b8 (diff) |
src: fix negative values in process.hrtime()
Fix a regression introduced in commit 89f056b ("node: improve
performance of hrtime()") where the nanosecond field sometimes
had a negative value when calculating the difference between two
timestamps.
Fixes: https://github.com/nodejs/node/issues/4751
PR-URL: https://github.com/nodejs/node/pull/4757
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
-rw-r--r-- | src/node.js | 7 | ||||
-rw-r--r-- | test/parallel/test-process-hrtime.js | 3 |
2 files changed, 6 insertions, 4 deletions
diff --git a/src/node.js b/src/node.js index 726820fb1ea..67f6cdb5ced 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 ad186a3507d..db16be0ad9e 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 |