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

fs: consider NaN/Infinity in toUnixTimestamp #2387

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ descriptor.

Change file timestamps of the file referenced by the supplied path.

Note: the arguments `atime` and `mtime` of the following related functions does follow the
Copy link
Contributor

Choose a reason for hiding this comment

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

Grammar seems a bit off here.

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 guess the lander could help me with fixing my grammar errors when landing my commits :(

below rules:

- If the value is a numberable string like "123456789", the value would get converted to
corresponding number.
- If the value is `NaN` or `Infinity`, the value would get converted to `Date.now()`.

## fs.utimesSync(path, atime, mtime)

Synchronous version of `fs.utimes()`. Returns `undefined`.
Expand Down
6 changes: 6 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,13 @@ fs.chownSync = function(path, uid, gid) {

// converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) {
if (typeof time === 'string' && +time == time) {
return +time;
}
if (typeof time === 'number') {
if (!Number.isFinite(time) || time < 0) {
return Date.now() / 1000;
}
return time;
}
if (util.isDate(time)) {
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-fs-utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,22 @@ function runTest(atime, mtime, callback) {

var stats = fs.statSync(__filename);

// run tests
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() {
// done
runTest(NaN, Infinity, function() {
runTest('123456', -1, function() {
// done
});
});
});
});
});
});


process.on('exit', function() {
console.log('Tests run / ok:', tests_run, '/', tests_ok);
assert.equal(tests_ok, tests_run);
Expand Down