Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: round nsec instead of truncating stat times #12607

Closed
wants to merge 10 commits into from
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ function Stats(
this.ino = ino;
this.size = size;
this.blocks = blocks;
this.atime = new Date(atim_msec);
this.mtime = new Date(mtim_msec);
this.ctime = new Date(ctim_msec);
this.birthtime = new Date(birthtim_msec);
this.atime = new Date(atim_msec + 0.5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know right.. wish it was my idea. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind placing a comment above these. I can see someone doing a refactor a year from now and accidentally removing this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest:

// + 0.5 is to protect values from being rounded down
// Ref: https://github.com/nodejs/node/pull/12607

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, these lines no longer exist though, but the +0.5s are still around and have been merged in. I'll add the comment. With #13173 the +0.5s aren't quite as needed anymore, since the goes away if you use mtimeMs etc. instead of mtime.

this.mtime = new Date(mtim_msec + 0.5);
this.ctime = new Date(ctim_msec + 0.5);
this.birthtime = new Date(birthtim_msec + 0.5);
}
fs.Stats = Stats;

Expand Down
6 changes: 3 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ void FillStatsArray(double* fields, const uv_stat_t* s) {
fields[9] = -1;
#endif
// Dates.
#define X(idx, name) \
fields[idx] = (static_cast<double>(s->st_##name.tv_sec) * 1000) + \
(static_cast<double>(s->st_##name.tv_nsec / 1000000)); \
#define X(idx, name) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the 4 spaces (just keep this line as it was)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's now 84 chars long, and would fail the linter.

fields[idx] = (s->st_##name.tv_sec * 1e3) + \
(s->st_##name.tv_nsec / 1e6); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh yeah, I didn't follow the braces, assumed it was the same as the line above...


X(10, atim)
X(11, mtim)
Expand Down
26 changes: 17 additions & 9 deletions test/parallel/test-fs-utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ function stat_resource(resource) {
}

function check_mtime(resource, mtime) {
mtime = fs._toUnixTimestamp(mtime);
const stats = stat_resource(resource);
const real_mtime = fs._toUnixTimestamp(stats.mtime);
// check up to single-second precision
// sub-second precision is OS and fs dependant
return mtime - real_mtime < 2;
if (common.isWindows) {
// check ms precision on windows.
return Math.floor(mtime) === Math.floor(stats.mtime);
} else {
mtime = fs._toUnixTimestamp(mtime);
const real_mtime = fs._toUnixTimestamp(stats.mtime);

// check up to single-second precision
// sub-second precision is OS and fs dependant
return mtime - real_mtime < 2;
}
}

function expect_errno(syscall, resource, err, errno) {
Expand Down Expand Up @@ -143,15 +149,17 @@ function testIt(atime, mtime, callback) {
const stats = fs.statSync(__filename);

// run tests
const runTest = common.mustCall(testIt, 5);
const runTest = common.mustCall(testIt, 6);

runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
runTest(new Date(), new Date(), function() {
runTest(123456.789, 123456.789, function() {
runTest(stats.mtime, stats.mtime, function() {
runTest('123456', -1, common.mustCall(function() {
// done
}));
runTest('123456', -1, function() {
runTest(1491674378008, 1491674378008, common.mustCall(function() {
// done
}));
});
});
});
});
Expand Down