From a05e273fdeeda72054f144c4a23d388a2d03c00b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 21 Mar 2020 14:07:21 +0100 Subject: [PATCH] lib: remove file timestamp rounding hack Introduced in commit 9836cf5717 ("lib: lazy instantiation of fs.Stats dates") without providing any rationale - that wasn't added until later, by someone else - and it doesn't look the least bit correct to me. It certainly makes an upcoming regression test fail because a timestamp in the deep past doesn't round-trip correctly, it's off by a fraction of a second. Refs: https://github.com/nodejs/node/issues/32369 --- lib/internal/fs/utils.js | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 763a940f6e98a7..b7e7c5e398c778 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -362,16 +362,6 @@ 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); -} - function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeNs, mtimeNs, ctimeNs, birthtimeNs) { @@ -386,10 +376,10 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, this.mtimeNs = mtimeNs; this.ctimeNs = ctimeNs; this.birthtimeNs = birthtimeNs; - this.atime = dateFromMs(this.atimeMs); - this.mtime = dateFromMs(this.mtimeMs); - this.ctime = dateFromMs(this.ctimeMs); - this.birthtime = dateFromMs(this.birthtimeMs); + this.atime = new Date(Number(this.atimeMs)); + this.mtime = new Date(Number(this.mtimeMs)); + this.ctime = new Date(Number(this.ctimeMs)); + this.birthtime = new Date(Number(this.birthtimeMs)); } ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype); @@ -412,10 +402,10 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize, this.mtimeMs = mtimeMs; this.ctimeMs = ctimeMs; this.birthtimeMs = birthtimeMs; - this.atime = dateFromMs(atimeMs); - this.mtime = dateFromMs(mtimeMs); - this.ctime = dateFromMs(ctimeMs); - this.birthtime = dateFromMs(birthtimeMs); + this.atime = new Date(atimeMs); + this.mtime = new Date(mtimeMs); + this.ctime = new Date(ctimeMs); + this.birthtime = new Date(birthtimeMs); } ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);