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>
This commit is contained in:
parent
d50a8a9848
commit
1e5a02628c
@ -193,10 +193,9 @@
|
|||||||
|
|
||||||
if (typeof ar !== 'undefined') {
|
if (typeof ar !== 'undefined') {
|
||||||
if (Array.isArray(ar)) {
|
if (Array.isArray(ar)) {
|
||||||
return [
|
const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0];
|
||||||
(hrValues[0] * 0x100000000 + hrValues[1]) - ar[0],
|
const nsec = hrValues[2] - ar[1];
|
||||||
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');
|
throw new TypeError('process.hrtime() only accepts an Array tuple');
|
||||||
|
@ -24,3 +24,6 @@ function validateTuple(tuple) {
|
|||||||
assert(isFinite(v));
|
assert(isFinite(v));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const diff = process.hrtime([0, 1e9 - 1]);
|
||||||
|
assert(diff[1] >= 0); // https://github.com/nodejs/node/issues/4751
|
||||||
|
Loading…
x
Reference in New Issue
Block a user