Skip to content

Commit 9f8a8bd

Browse files
committed
fs: expose Stats times as Numbers
* convert ’ to ' to turn md file to ASCII Fixes: nodejs#8276 Refs: nodejs#12607 Refs: nodejs#12818 Refs: nodejs#13256
1 parent ae6c704 commit 9f8a8bd

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

doc/api/fs.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,14 @@ argument to `fs.createReadStream()`. If `path` is passed as a string, then
295295
## Class: fs.Stats
296296
<!-- YAML
297297
added: v0.1.21
298+
changes:
299+
- version: REPLACEME
300+
pr-url: https://github.com/nodejs/node/pull/13173
301+
description: Added times as numbers.
298302
-->
299303

300-
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and their
301-
synchronous counterparts are of this type.
304+
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and
305+
their synchronous counterparts are of this type.
302306

303307
- `stats.isFile()`
304308
- `stats.isDirectory()`
@@ -323,20 +327,23 @@ Stats {
323327
size: 527,
324328
blksize: 4096,
325329
blocks: 8,
330+
atimeMs: 1318289051000.1,
331+
mtimeMs: 1318289051000.1,
332+
ctimeMs: 1318289051000.1,
333+
birthtimeMs: 1318289051000.1,
326334
atime: Mon, 10 Oct 2011 23:24:11 GMT,
327335
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
328336
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
329337
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
330338
```
331339

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

341348
### Stat Time Values
342349

@@ -527,7 +534,7 @@ The "not recommended" examples above check for accessibility and then use the
527534
file; the "recommended" examples are better because they use the file directly
528535
and handle the error, if any.
529536

530-
In general, check for the accessibility of a file only if the file wont be
537+
In general, check for the accessibility of a file only if the file won't be
531538
used directly, for example when its accessibility is a signal from another
532539
process.
533540

@@ -959,7 +966,7 @@ The "not recommended" examples above check for existence and then use the
959966
file; the "recommended" examples are better because they use the file directly
960967
and handle the error, if any.
961968

962-
In general, check for the existence of a file only if the file wont be
969+
In general, check for the existence of a file only if the file won't be
963970
used directly, for example when its existence is a signal from another
964971
process.
965972

@@ -2835,6 +2842,7 @@ The following constants are meant for use with the [`fs.Stats`][] object's
28352842
[FS Constants]: #fs_fs_constants_1
28362843
[MDN-Date-getTime]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime
28372844
[MDN-Date]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
2845+
[MDN-Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
28382846
[Readable Stream]: stream.html#stream_class_stream_readable
28392847
[Writable Stream]: stream.html#stream_class_stream_writable
28402848
[inode]: https://en.wikipedia.org/wiki/Inode

lib/fs.js

Lines changed: 4 additions & 0 deletions
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

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525
const fs = require('fs');
26+
const util = require('util');
2627

2728
fs.stat('.', common.mustCall(function(err, stats) {
2829
assert.ifError(err);
@@ -65,38 +66,56 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
6566
assert.fail(err);
6667
}
6768
if (stats) {
68-
console.dir(stats);
6969
assert.ok(stats.mtime instanceof Date);
7070
}
7171
fs.close(fd, assert.ifError);
7272
}));
7373

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

0 commit comments

Comments
 (0)