Skip to content

Commit

Permalink
Fix filerjs#736: use ms vs. s resolution on all times
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Feb 7, 2019
1 parent 0ec51a1 commit 9b496e7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/filesystem/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ function toUnixTimestamp(time) {
return time;
}
if (typeof time === 'object' && typeof time.getTime === 'function') {
return time.getTime() / 1000;
return time.getTime();
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
71 changes: 67 additions & 4 deletions tests/spec/fs.stat.spec.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
});
});
});
});

/**
Expand Down

0 comments on commit 9b496e7

Please sign in to comment.