Skip to content

Commit

Permalink
lib: remove file timestamp rounding hack
Browse files Browse the repository at this point in the history
Introduced in commit 9836cf5 ("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: nodejs#32369
  • Loading branch information
bnoordhuis committed Mar 21, 2020
1 parent 083b4a5 commit a05e273
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a05e273

Please sign in to comment.