Skip to content

Commit c756efb

Browse files
refackjasnell
authored andcommitted
fs: expose Stats times as Numbers
PR-URL: #13173 Fixes: #8276 Refs: #12607 Refs: #12818 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 4b2c756 commit c756efb

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

doc/api/fs.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,14 @@ argument to `fs.createReadStream()`. If `path` is passed as a string, then
301301
## Class: fs.Stats
302302
<!-- YAML
303303
added: v0.1.21
304+
changes:
305+
- version: REPLACEME
306+
pr-url: https://github.com/nodejs/node/pull/13173
307+
description: Added times as numbers.
304308
-->
305309

306-
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and their
307-
synchronous counterparts are of this type.
310+
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and
311+
their synchronous counterparts are of this type.
308312

309313
- `stats.isFile()`
310314
- `stats.isDirectory()`
@@ -329,20 +333,23 @@ Stats {
329333
size: 527,
330334
blksize: 4096,
331335
blocks: 8,
336+
atimeMs: 1318289051000.1,
337+
mtimeMs: 1318289051000.1,
338+
ctimeMs: 1318289051000.1,
339+
birthtimeMs: 1318289051000.1,
332340
atime: Mon, 10 Oct 2011 23:24:11 GMT,
333341
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
334342
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
335343
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
336344
```
337345

338-
Please note that `atime`, `mtime`, `birthtime`, and `ctime` are
339-
instances of [`Date`][MDN-Date] object and appropriate methods should be used
340-
to compare the values of these objects. For most general uses
341-
[`getTime()`][MDN-Date-getTime] will return the number of milliseconds elapsed
342-
since _1 January 1970 00:00:00 UTC_ and this integer should be sufficient for
343-
any comparison, however there are additional methods which can be used for
344-
displaying fuzzy information. More details can be found in the
345-
[MDN JavaScript Reference][MDN-Date] page.
346+
*Note*: `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs` are [numbers][MDN-Number]
347+
that hold the corresponding times in milliseconds. Their precision is platform
348+
specific. `atime`, `mtime`, `ctime`, and `birthtime` are [`Date`][MDN-Date]
349+
object alternate representations of the various times. The `Date` and number
350+
values are not connected. Assigning a new number value, or mutating the `Date`
351+
value, will not be reflected in the corresponding alternate representation.
352+
346353

347354
### Stat Time Values
348355

@@ -2841,6 +2848,7 @@ The following constants are meant for use with the [`fs.Stats`][] object's
28412848
[FS Constants]: #fs_fs_constants_1
28422849
[MDN-Date-getTime]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime
28432850
[MDN-Date]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
2851+
[MDN-Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
28442852
[MSDN-Rel-Path]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#fully_qualified_vs._relative_paths
28452853
[Readable Stream]: stream.html#stream_class_stream_readable
28462854
[Writable Stream]: stream.html#stream_class_stream_writable

lib/fs.js

+4
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ function Stats(
196196
this.ino = ino;
197197
this.size = size;
198198
this.blocks = blocks;
199+
this.atimeMs = atim_msec;
200+
this.mtimeMs = mtim_msec;
201+
this.ctimeMs = ctim_msec;
202+
this.birthtimeMs = birthtim_msec;
199203
this.atime = new Date(atim_msec + 0.5);
200204
this.mtime = new Date(mtim_msec + 0.5);
201205
this.ctime = new Date(ctim_msec + 0.5);

test/parallel/test-fs-stat.js

+37-20
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,55 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
6565
assert.fail(err);
6666
}
6767
if (stats) {
68-
console.dir(stats);
6968
assert.ok(stats.mtime instanceof Date);
7069
}
7170
fs.close(fd, assert.ifError);
7271
}));
7372

74-
console.log(`stating: ${__filename}`);
7573
fs.stat(__filename, common.mustCall(function(err, s) {
7674
assert.ifError(err);
77-
78-
console.dir(s);
79-
80-
console.log(`isDirectory: ${JSON.stringify(s.isDirectory())}`);
8175
assert.strictEqual(false, s.isDirectory());
82-
83-
console.log(`isFile: ${JSON.stringify(s.isFile())}`);
8476
assert.strictEqual(true, s.isFile());
85-
86-
console.log(`isSocket: ${JSON.stringify(s.isSocket())}`);
8777
assert.strictEqual(false, s.isSocket());
88-
89-
console.log(`isBlockDevice: ${JSON.stringify(s.isBlockDevice())}`);
9078
assert.strictEqual(false, s.isBlockDevice());
91-
92-
console.log(`isCharacterDevice: ${JSON.stringify(s.isCharacterDevice())}`);
9379
assert.strictEqual(false, s.isCharacterDevice());
94-
95-
console.log(`isFIFO: ${JSON.stringify(s.isFIFO())}`);
9680
assert.strictEqual(false, s.isFIFO());
97-
98-
console.log(`isSymbolicLink: ${JSON.stringify(s.isSymbolicLink())}`);
9981
assert.strictEqual(false, s.isSymbolicLink());
100-
101-
assert.ok(s.mtime instanceof Date);
82+
const keys = [
83+
'dev', 'mode', 'nlink', 'uid',
84+
'gid', 'rdev', 'ino', 'size',
85+
'atime', 'mtime', 'ctime', 'birthtime',
86+
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs'
87+
];
88+
if (!common.isWindows) {
89+
keys.push('blocks', 'blksize');
90+
}
91+
const numberFields = [
92+
'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'ino', 'size',
93+
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs'
94+
];
95+
const dateFields = ['atime', 'mtime', 'ctime', 'birthtime'];
96+
keys.forEach(function(k) {
97+
assert.ok(k in s, `${k} should be in Stats`);
98+
assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`);
99+
assert.notStrictEqual(s[k], null, `${k} should not be null`);
100+
});
101+
numberFields.forEach((k) => {
102+
assert.strictEqual(typeof s[k], 'number', `${k} should be a number`);
103+
});
104+
dateFields.forEach((k) => {
105+
assert.ok(s[k] instanceof Date, `${k} should be a Date`);
106+
});
107+
const jsonString = JSON.stringify(s);
108+
const parsed = JSON.parse(jsonString);
109+
keys.forEach(function(k) {
110+
assert.notStrictEqual(parsed[k], undefined, `${k} should not be undefined`);
111+
assert.notStrictEqual(parsed[k], null, `${k} should not be null`);
112+
});
113+
numberFields.forEach((k) => {
114+
assert.strictEqual(typeof parsed[k], 'number', `${k} should be a number`);
115+
});
116+
dateFields.forEach((k) => {
117+
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
118+
});
102119
}));

0 commit comments

Comments
 (0)