Fix #736: use ms vs. s resolution on all times
This commit is contained in:
parent
0ec51a198d
commit
8a2e64f9ea
|
@ -2160,7 +2160,7 @@ function toUnixTimestamp(time) {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
if (typeof time === 'object' && typeof time.getTime === 'function') {
|
if (typeof time === 'object' && typeof time.getTime === 'function') {
|
||||||
return time.getTime() / 1000;
|
return time.getTime();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/stats.js
13
src/stats.js
|
@ -3,9 +3,8 @@
|
||||||
const Constants = require('./constants.js');
|
const Constants = require('./constants.js');
|
||||||
const Path = require('./path.js');
|
const Path = require('./path.js');
|
||||||
|
|
||||||
// https://github.com/nodejs/node/blob/4f1297f259b09d129ac01afbd4c674263b7ac124/lib/internal/fs/utils.js#L231
|
function dateFromMs(ms) {
|
||||||
function dateFromNumeric(num) {
|
return new Date(Number(ms));
|
||||||
return new Date(Number(num) * 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Stats(path, fileNode, devName) {
|
function Stats(path, fileNode, devName) {
|
||||||
|
@ -15,10 +14,10 @@ function Stats(path, fileNode, devName) {
|
||||||
this.size = fileNode.size;
|
this.size = fileNode.size;
|
||||||
this.nlinks = fileNode.nlinks;
|
this.nlinks = fileNode.nlinks;
|
||||||
// Date objects
|
// Date objects
|
||||||
this.atime = dateFromNumeric(fileNode.atime);
|
this.atime = dateFromMs(fileNode.atime);
|
||||||
this.mtime = dateFromNumeric(fileNode.mtime);
|
this.mtime = dateFromMs(fileNode.mtime);
|
||||||
this.ctime = dateFromNumeric(fileNode.ctime);
|
this.ctime = dateFromMs(fileNode.ctime);
|
||||||
// Unix timestamp Numbers
|
// Unix timestamp MS Numbers
|
||||||
this.atimeMs = fileNode.atime;
|
this.atimeMs = fileNode.atime;
|
||||||
this.mtimeMs = fileNode.mtime;
|
this.mtimeMs = fileNode.mtime;
|
||||||
this.ctimeMs = fileNode.ctime;
|
this.ctimeMs = fileNode.ctime;
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
var util = require('../lib/test-utils.js');
|
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() {
|
describe('fs.stat', function() {
|
||||||
beforeEach(util.setup);
|
beforeEach(util.setup);
|
||||||
|
@ -55,9 +57,9 @@ describe('fs.stat', function() {
|
||||||
expect(result['mtime']).to.be.a('date');
|
expect(result['mtime']).to.be.a('date');
|
||||||
expect(result['ctime']).to.be.a('date');
|
expect(result['ctime']).to.be.a('date');
|
||||||
|
|
||||||
expect(result.atime.getTime()/1000).to.equal(result.atimeMs);
|
expect(result.atime.getTime()).to.equal(result.atimeMs);
|
||||||
expect(result.mtime.getTime()/1000).to.equal(result.mtimeMs);
|
expect(result.mtime.getTime()).to.equal(result.mtimeMs);
|
||||||
expect(result.ctime.getTime()/1000).to.equal(result.ctimeMs);
|
expect(result.ctime.getTime()).to.equal(result.ctimeMs);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
@ -152,6 +154,67 @@ describe('fs.stat', function() {
|
||||||
expect(result.isDirectory()).to.be.true;
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue