From 9b496e792f65b8149db202f483332e35e7e958b3 Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Thu, 7 Feb 2019 16:28:20 -0500 Subject: [PATCH] Fix #736: use ms vs. s resolution on all times --- src/filesystem/implementation.js | 2 +- src/stats.js | 13 +++--- tests/spec/fs.stat.spec.js | 71 ++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 79275ce8..dc2677b5 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 5a8b274d..36658c47 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; diff --git a/tests/spec/fs.stat.spec.js b/tests/spec/fs.stat.spec.js index e2ad90b5..b14d109e 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(); + }); + }); + }); + }); }); /**