Skip to content

Commit 9eb700c

Browse files
committed
fs: add Temporal.Instant support to Stats and BigIntStats
1 parent eb6cb5b commit 9eb700c

File tree

3 files changed

+162
-17
lines changed

3 files changed

+162
-17
lines changed

doc/api/fs.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,7 +4494,11 @@ Stats {
44944494
atime: 2019-06-22T03:37:33.072Z,
44954495
mtime: 2019-06-22T03:36:54.583Z,
44964496
ctime: 2019-06-22T03:37:06.624Z,
4497-
birthtime: 2019-06-22T03:28:46.937Z
4497+
birthtime: 2019-06-22T03:28:46.937Z,
4498+
atimeInstant: 2019-06-22T03:37:33.071963Z,
4499+
mtimeInstant: 2019-06-22T03:36:54.5833518Z,
4500+
ctimeInstant: 2019-06-22T03:37:06.6235366Z,
4501+
birthtimeInstant: 2019-06-22T03:28:46.9372893Z
44984502
}
44994503
false
45004504
Stats {
@@ -4515,7 +4519,11 @@ Stats {
45154519
atime: 2019-06-22T03:36:56.619Z,
45164520
mtime: 2019-06-22T03:36:54.584Z,
45174521
ctime: 2019-06-22T03:36:54.584Z,
4518-
birthtime: 2019-06-22T03:26:47.711Z
4522+
birthtime: 2019-06-22T03:26:47.711Z,
4523+
atimeInstant: 2019-06-22T03:36:56.6188555Z,
4524+
mtimeInstant: 2019-06-22T03:36:54.584Z,
4525+
ctimeInstant: 2019-06-22T03:36:54.5838145Z,
4526+
birthtimeInstant: 2019-06-22T03:26:47.7107478Z
45194527
}
45204528
```
45214529
@@ -7203,6 +7211,9 @@ i.e. before the `'ready'` event is emitted.
72037211
<!-- YAML
72047212
added: v0.1.21
72057213
changes:
7214+
- version: REPLACEME
7215+
pr-url: https://github.com/nodejs/node/pull/00000
7216+
description: Added Temporal.Instant support.
72067217
- version:
72077218
- v22.0.0
72087219
- v20.13.0
@@ -7238,10 +7249,19 @@ Stats {
72387249
mtimeMs: 1318289051000.1,
72397250
ctimeMs: 1318289051000.1,
72407251
birthtimeMs: 1318289051000.1,
7252+
7253+
// Instances of Date
72417254
atime: Mon, 10 Oct 2011 23:24:11 GMT,
72427255
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
72437256
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
7244-
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
7257+
birthtime: Mon, 10 Oct 2011 23:24:11 GMT,
7258+
7259+
// Instances of Temporal.Instant
7260+
atimeInstant: 2011-10-10T23:24:11.0001Z,
7261+
mtimeInstant: 2011-10-10T23:24:11.0001Z,
7262+
ctimeInstant: 2011-10-10T23:24:11.0001Z,
7263+
birthtimeInstant: 2011-10-10T23:24:11.0001Z
7264+
}
72457265
```
72467266
72477267
`bigint` version:
@@ -7266,10 +7286,19 @@ BigIntStats {
72667286
mtimeNs: 1318289051000000000n,
72677287
ctimeNs: 1318289051000000000n,
72687288
birthtimeNs: 1318289051000000000n,
7289+
7290+
// Instances of Date
72697291
atime: Mon, 10 Oct 2011 23:24:11 GMT,
72707292
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
72717293
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
7272-
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
7294+
birthtime: Mon, 10 Oct 2011 23:24:11 GMT,
7295+
7296+
// Instances of Temporal.Instant
7297+
atimeInstant: 2011-10-10T23:24:11Z,
7298+
mtimeInstant: 2011-10-10T23:24:11Z,
7299+
ctimeInstant: 2011-10-10T23:24:11Z,
7300+
birthtimeInstant: 2011-10-10T23:24:11Z
7301+
}
72737302
```
72747303
72757304
#### `stats.isBlockDevice()`

lib/internal/fs/utils.js

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
DatePrototypeGetTime,
99
ErrorCaptureStackTrace,
1010
FunctionPrototypeCall,
11+
MathFloor,
1112
MathMin,
1213
MathRound,
1314
Number,
@@ -63,6 +64,10 @@ const {
6364
const pathModule = require('path');
6465
const kType = Symbol('type');
6566
const kStats = Symbol('stats');
67+
const kPartialAtimeNs = Symbol('partialAtimeNs');
68+
const kPartialMtimeNs = Symbol('partialMtimeNs');
69+
const kPartialCtimeNs = Symbol('partialCtimeNs');
70+
const kPartialBirthtimeNs = Symbol('kPartialBirthtimeNs');
6671
const assert = require('internal/assert');
6772

6873
const {
@@ -431,6 +436,11 @@ function nsFromTimeSpecBigInt(sec, nsec) {
431436
return sec * kNsPerSecBigInt + nsec;
432437
}
433438

439+
function instantFromTimeSpecMs(msec, nsec) {
440+
// TODO(LiviaMedeiros): TemporalInstant primordial
441+
return new Temporal.Instant(BigInt(MathFloor(msec / kMsPerSec)) * kNsPerSecBigInt + BigInt(nsec));
442+
}
443+
434444
// The Date constructor performs Math.floor() on the absolute value
435445
// of the timestamp: https://tc39.es/ecma262/#sec-timeclip
436446
// Since there may be a precision loss when the timestamp is
@@ -491,6 +501,102 @@ const lazyDateFields = {
491501
},
492502
};
493503

504+
const lazyTemporalFields = {
505+
__proto__: null,
506+
atimeInstant: {
507+
__proto__: null,
508+
enumerable: true,
509+
configurable: true,
510+
get() {
511+
return this.atimeInstant = instantFromTimeSpecMs(this.atimeMs, this[kPartialAtimeNs]);
512+
},
513+
set(value) {
514+
ObjectDefineProperty(this, 'atimeInstant', { __proto__: null, value, writable: true });
515+
},
516+
},
517+
mtimeInstant: {
518+
__proto__: null,
519+
enumerable: true,
520+
configurable: true,
521+
get() {
522+
return this.mtimeInstant = instantFromTimeSpecMs(this.mtimeMs, this[kPartialMtimeNs]);
523+
},
524+
set(value) {
525+
ObjectDefineProperty(this, 'mtimeInstant', { __proto__: null, value, writable: true });
526+
},
527+
},
528+
ctimeInstant: {
529+
__proto__: null,
530+
enumerable: true,
531+
configurable: true,
532+
get() {
533+
return this.ctimeInstant = instantFromTimeSpecMs(this.ctimeMs, this[kPartialCtimeNs]);;
534+
},
535+
set(value) {
536+
ObjectDefineProperty(this, 'ctimeInstant', { __proto__: null, value, writable: true });
537+
},
538+
},
539+
birthtimeInstant: {
540+
__proto__: null,
541+
enumerable: true,
542+
configurable: true,
543+
get() {
544+
return this.birthtimeInstant = instantFromTimeSpecMs(this.birthtimeMs, this[kPartialBirthtimeNs]);;
545+
},
546+
set(value) {
547+
ObjectDefineProperty(this, 'birthtimeInstant', { __proto__: null, value, writable: true });
548+
},
549+
},
550+
};
551+
552+
const lazyTemporalBigIntFields = {
553+
__proto__: null,
554+
atimeInstant: {
555+
__proto__: null,
556+
enumerable: true,
557+
configurable: true,
558+
get() {
559+
return this.atimeInstant = new Temporal.Instant(this.atimeNs);
560+
},
561+
set(value) {
562+
ObjectDefineProperty(this, 'atimeInstant', { __proto__: null, value, writable: true });
563+
},
564+
},
565+
mtimeInstant: {
566+
__proto__: null,
567+
enumerable: true,
568+
configurable: true,
569+
get() {
570+
return this.mtimeInstant = new Temporal.Instant(this.mtimeNs);
571+
},
572+
set(value) {
573+
ObjectDefineProperty(this, 'mtimeInstant', { __proto__: null, value, writable: true });
574+
},
575+
},
576+
ctimeInstant: {
577+
__proto__: null,
578+
enumerable: true,
579+
configurable: true,
580+
get() {
581+
return this.ctimeInstant = new Temporal.Instant(this.ctimeNs);
582+
},
583+
set(value) {
584+
ObjectDefineProperty(this, 'ctimeInstant', { __proto__: null, value, writable: true });
585+
},
586+
},
587+
birthtimeInstant: {
588+
__proto__: null,
589+
enumerable: true,
590+
configurable: true,
591+
get() {
592+
return this.birthtimeInstant = new Temporal.Instant(this.birthtimeNs);
593+
},
594+
set(value) {
595+
ObjectDefineProperty(this, 'birthtimeInstant', { __proto__: null, value, writable: true });
596+
},
597+
},
598+
};
599+
494600
function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
495601
ino, size, blocks,
496602
atimeNs, mtimeNs, ctimeNs, birthtimeNs) {
@@ -510,6 +616,7 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
510616
ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype);
511617
ObjectSetPrototypeOf(BigIntStats, StatsBase);
512618
ObjectDefineProperties(BigIntStats.prototype, lazyDateFields);
619+
ObjectDefineProperties(BigIntStats.prototype, lazyTemporalBigIntFields);
513620

514621
BigIntStats.prototype._checkModeProperty = function(property) {
515622
if (isWindows && (property === S_IFIFO || property === S_IFBLK ||
@@ -521,18 +628,23 @@ BigIntStats.prototype._checkModeProperty = function(property) {
521628

522629
function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
523630
ino, size, blocks,
524-
atimeMs, mtimeMs, ctimeMs, birthtimeMs) {
631+
atimeS, atimeNs, mtimeS, mtimeNs, ctimeS, ctimeNs, birthtimeS, birthtimeNs) {
525632
FunctionPrototypeCall(StatsBase, this, dev, mode, nlink, uid, gid, rdev,
526633
blksize, ino, size, blocks);
527-
this.atimeMs = atimeMs;
528-
this.mtimeMs = mtimeMs;
529-
this.ctimeMs = ctimeMs;
530-
this.birthtimeMs = birthtimeMs;
634+
this.atimeMs = msFromTimeSpec(atimeS, atimeNs);
635+
this.mtimeMs = msFromTimeSpec(mtimeS, mtimeNs);
636+
this.ctimeMs = msFromTimeSpec(ctimeS, ctimeNs);
637+
this.birthtimeMs = msFromTimeSpec(birthtimeS, birthtimeNs);
638+
this[kPartialAtimeNs] = atimeNs;
639+
this[kPartialMtimeNs] = mtimeNs;
640+
this[kPartialCtimeNs] = ctimeNs;
641+
this[kPartialBirthtimeNs] = birthtimeNs;
531642
}
532643

533644
ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);
534645
ObjectSetPrototypeOf(Stats, StatsBase);
535646
ObjectDefineProperties(Stats.prototype, lazyDateFields);
647+
ObjectDefineProperties(Stats.prototype, lazyTemporalFields);
536648

537649
Stats.prototype._checkModeProperty = function(property) {
538650
if (isWindows && (property === S_IFIFO || property === S_IFBLK ||
@@ -565,10 +677,10 @@ function getStatsFromBinding(stats, offset = 0) {
565677
stats[3 + offset], stats[4 + offset], stats[5 + offset],
566678
stats[6 + offset], stats[7 + offset], stats[8 + offset],
567679
stats[9 + offset],
568-
msFromTimeSpec(stats[10 + offset], stats[11 + offset]),
569-
msFromTimeSpec(stats[12 + offset], stats[13 + offset]),
570-
msFromTimeSpec(stats[14 + offset], stats[15 + offset]),
571-
msFromTimeSpec(stats[16 + offset], stats[17 + offset]),
680+
stats[10 + offset], stats[11 + offset], // atime
681+
stats[12 + offset], stats[13 + offset], // mtime
682+
stats[14 + offset], stats[15 + offset], // ctime
683+
stats[16 + offset], stats[17 + offset], // birthtime
572684
);
573685
}
574686

test/parallel/test-fs-watchfile.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ const expectedStatObject = new fs.Stats(
4242
0, // ino
4343
0, // size
4444
0, // blocks
45-
Date.UTC(1970, 0, 1, 0, 0, 0), // atime
46-
Date.UTC(1970, 0, 1, 0, 0, 0), // mtime
47-
Date.UTC(1970, 0, 1, 0, 0, 0), // ctime
48-
Date.UTC(1970, 0, 1, 0, 0, 0) // birthtime
45+
0, // atimeS
46+
0, // atimeNs
47+
0, // mtimeS
48+
0, // mtimeNs
49+
0, // ctime
50+
0, // ctimeNs
51+
0, // birthtime
52+
0, // birthtimeNs
4953
);
5054

5155
tmpdir.refresh();

0 commit comments

Comments
 (0)