Fix #736: use ms vs. s resolution on all times

This commit is contained in:
David Humphrey 2019-02-07 16:28:20 -05:00
parent 0ec51a198d
commit 8a2e64f9ea
3 changed files with 74 additions and 12 deletions

View File

@ -2160,7 +2160,7 @@ function toUnixTimestamp(time) {
return time;
}
if (typeof time === 'object' && typeof time.getTime === 'function') {
return time.getTime() / 1000;
return time.getTime();
}
}

View File

@ -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;

View File

@ -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();
});
});
});
});
});
/**