From e89a39bbb7d5c182712d70a5854881943420c98f Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 14 Jun 2019 23:05:04 +0800 Subject: [PATCH] fs: document the Date conversion in Stats objects Document why the dates are calculated with the timestamp in Numbers + 0.5. The comment was previously lost in a revert. Refs: https://github.com/nodejs/node/commit/ae6c7044c8d48cc8e7c267efca7429d5aa4df993 PR-URL: https://github.com/nodejs/node/pull/28224 Refs: https://github.com/nodejs/node/commit/ae6c7044c8d48cc8e7c267efca7429d5aa4df993 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater --- lib/internal/fs/utils.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 6cd6f7aceb2848..24d946edf7288a 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -286,6 +286,12 @@ function nsFromTimeSpecBigInt(sec, nsec) { return sec * kNsPerSecBigInt + nsec; } +// The Date constructor performs Math.floor() to the timestamp. +// https://www.ecma-international.org/ecma-262/#sec-timeclip +// Since there may be a precision loss when the timestamp is +// converted to a floating point number, we manually round +// the timestamp here before passing it to Date(). +// Refs: https://github.com/nodejs/node/pull/12607 function dateFromMs(ms) { return new Date(Number(ms) + 0.5); }