Repository: filerjs/filer (js)

Issue:
Bug in atime, ctime, mtime values
```
atime: Tue Nov 18 51073 14:00:03 GMT-0500 (Eastern Standard Time)
atimeMs: 1549568862003
```

I broke this adding support for `*Ms` versions of times similar to node.  I'm multiplying values already in ms by 1000 when I shouldn't be.

Pull request:
Fix #736: use ms vs. s resolution on all times
This corrects my mistake when porting node's time handling code.  They assume seconds in a lot of places, and have to multiply/divide by 1000 to get proper units.  We always use ms times, so this fixes things everywhere.  I've added better test coverage for it too.

Patch:
diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js
index 79275ce..dc2677b 100644
--- a/src/filesystem/implementation.js
+++ b/src/filesystem/implementation.js
@@ -2160,7 +2160,7 @@ function toUnixTimestamp(time) {
     return time;
   }
   if (typeof time === 'object' && typeof time.getTime === 'function') {
-    return time.getTime() / 1000;
+    return time.getTime();
   }
 }
 
diff --git a/src/stats.js b/src/stats.js
index 5a8b274..36658c4 100644
--- a/src/stats.js
+++ b/src/stats.js
@@ -3,9 +3,8 @@
 const Constants = require('./constants.js');
 const Path = require('./path.js');
 
-// https://github.com/nodejs/node/blob/4f1297f259b09d129ac01afbd4c674263b7ac124/lib/internal/fs/utils.js#L231
-function dateFromNumeric(num) {
-  return new Date(Number(num) * 1000);
+function dateFromMs(ms) {
+  return new Date(Number(ms));
 }
 
 function Stats(path, fileNode, devName) {
@@ -15,10 +14,10 @@ function Stats(path, fileNode, devName) {
   this.size = fileNode.size;
   this.nlinks = fileNode.nlinks;
   // Date objects
-  this.atime = dateFromNumeric(fileNode.atime);
-  this.mtime = dateFromNumeric(fileNode.mtime);
-  this.ctime = dateFromNumeric(fileNode.ctime);
-  // Unix timestamp Numbers
+  this.atime = dateFromMs(fileNode.atime);
+  this.mtime = dateFromMs(fileNode.mtime);
+  this.ctime = dateFromMs(fileNode.ctime);
+  // Unix timestamp MS Numbers
   this.atimeMs = fileNode.atime;
   this.mtimeMs = fileNode.mtime;
   this.ctimeMs = fileNode.ctime;


Test patch:
diff --git a/tests/spec/fs.stat.spec.js b/tests/spec/fs.stat.spec.js
index e2ad90b..b14d109 100644
--- a/tests/spec/fs.stat.spec.js
+++ b/tests/spec/fs.stat.spec.js
@@ -1,5 +1,7 @@
 var util = require('../lib/test-utils.js');
-var expect = require('chai').expect;
+var chai = require('chai');
+chai.use(require('chai-datetime'));
+var expect = chai.expect;
 
 describe('fs.stat', function() {
   beforeEach(util.setup);
@@ -55,9 +57,9 @@ describe('fs.stat', function() {
       expect(result['mtime']).to.be.a('date');
       expect(result['ctime']).to.be.a('date');
 
-      expect(result.atime.getTime()/1000).to.equal(result.atimeMs);
-      expect(result.mtime.getTime()/1000).to.equal(result.mtimeMs);
-      expect(result.ctime.getTime()/1000).to.equal(result.ctimeMs);
+      expect(result.atime.getTime()).to.equal(result.atimeMs);
+      expect(result.mtime.getTime()).to.equal(result.mtimeMs);
+      expect(result.ctime.getTime()).to.equal(result.ctimeMs);
 
       done();
     });
@@ -152,6 +154,67 @@ describe('fs.stat', function() {
         expect(result.isDirectory()).to.be.true;
       }); 
   });
+
+  it('should set appropriate time and timeMs values when creating a file', function(done) {
+    var fs = util.fs();
+
+    // Make sure that all times on a file node are within a 1 minute window
+    var before = new Date();
+    var oneMinuteLater = new Date();
+    oneMinuteLater.setMinutes(oneMinuteLater.getMinutes() + 1);
+
+    fs.writeFile('/file', 'data', function(error) {
+      if(error) throw error;
+
+      fs.stat('/file', function(error, stats) {
+        if(error) throw error;
+
+        expect(new Date(stats.ctimeMs)).to.be.withinDate(before, oneMinuteLater);
+        expect(stats.ctime).to.be.withinDate(before, oneMinuteLater);
+
+        expect(new Date(stats.atimeMs)).to.be.withinDate(before, oneMinuteLater);
+        expect(stats.atime).to.be.withinDate(before, oneMinuteLater);
+
+        expect(new Date(stats.mtimeMs)).to.be.withinDate(before, oneMinuteLater);
+        expect(stats.mtime).to.be.withinDate(before, oneMinuteLater);
+
+        done();
+      });
+    });
+  });
+
+  it('should set appropriate time and timeMs values when creating a file', function(done) {
+    var fs = util.fs();
+
+    fs.writeFile('/file', 'data', function(error) {
+      if(error) throw error;
+
+      var newAtime = new Date('1 Oct 2000 15:33:22');
+      var newMtime = new Date('30 Sep 2000 06:43:54');
+
+      fs.utimes('/file', newAtime, newMtime, function(error) {
+        if(error) throw error;
+
+        fs.stat('/file', function(error, stats) {
+          if(error) throw error;
+
+          // ctime should match newMtime
+          expect(stats.ctimeMs).to.equal(newMtime.getTime());
+          expect(stats.ctime).to.equalDate(newMtime);
+
+          // atime should match newAtime
+          expect(stats.atimeMs).to.equal(newAtime.getTime());
+          expect(stats.atime).to.equalDate(newAtime);
+
+          // mtime should match newMtime
+          expect(stats.mtimeMs).to.equal(newMtime.getTime());
+          expect(stats.mtime).to.equalDate(newMtime);
+
+          done();
+        });
+      });
+    });
+  });
 });
 
 /**