fs: consider NaN/Infinity in toUnixTimestamp
authorYazhong Liu <yorkiefixer@gmail.com>
Sat, 15 Aug 2015 16:30:31 +0000 (00:30 +0800)
committerJeremiah Senkpiel <fishrock123@rocketmail.com>
Tue, 15 Sep 2015 17:53:00 +0000 (13:53 -0400)
PR-URL: https://github.com/nodejs/node/pull/2387
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
doc/api/fs.markdown
lib/fs.js
test/parallel/test-fs-utimes.js

index 643f9d9..0b5adab 100644 (file)
@@ -363,6 +363,14 @@ 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 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`.
index 10e5b63..eeb3806 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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)) {
index 292636b..a5ec0b2 100644 (file)
@@ -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);