-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreciseDate.js
More file actions
176 lines (143 loc) · 5.64 KB
/
Copy pathPreciseDate.js
File metadata and controls
176 lines (143 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class PreciseDate extends Date {
#microseconds = 0;
#nanoseconds = 0;
constructor(...args) {
if (args.length === 0) {
const nanosecondsTotal = PreciseDate.now();
const milliseconds = Number(nanosecondsTotal / BigInt(1e6));
super(milliseconds);
this.#microseconds = Number((nanosecondsTotal / BigInt(1e3)) % BigInt(1e3));
this.#nanoseconds = Number(nanosecondsTotal % BigInt(1e3));
} else if (args.length === 1 && typeof args[0] === 'string') {
super(args[0]);
if (!isNaN(super.getTime())) {
const isoRegex = /\.(\d{3,9})Z$/;
const match = isoRegex.exec(args[0]);
if (match) {
const fraction = match[1].padEnd(9, '0'); // Ensure it's 9 digits
this.#microseconds = parseInt(fraction.substr(3, 3));
this.#nanoseconds = parseInt(fraction.substr(6, 3));
} else {
this.#microseconds = 0;
this.#nanoseconds = 0;
}
}
} else if (args.length === 1 && args[0] instanceof PreciseDate) {
super(args[0]);
this.#microseconds = args[0].getMicroseconds();
this.#nanoseconds = args[0].getNanoseconds();
} else if (args.length === 1 && args[0] instanceof Date) {
super(args[0]);
this.#microseconds = 0;
this.#nanoseconds = 0;
} else if (args.length === 1 && typeof args[0] === 'number') {
super(args[0]);
this.#microseconds = 0;
this.#nanoseconds = 0;
} else if (args.length === 1 && typeof args[0] === 'bigint') {
const nanosecondsTotal = args[0];
const milliseconds = Number(nanosecondsTotal / BigInt(1e6));
super(milliseconds);
this.#microseconds = Number((nanosecondsTotal / BigInt(1e3)) % BigInt(1e3));
this.#nanoseconds = Number(nanosecondsTotal % BigInt(1e3));
} else {
super(...args.slice(0, 7));
if (args.length >= 8 && typeof args[7] === 'number') {
this.setMicroseconds(args[7]);
}
if (args.length >= 9 && typeof args[8] === 'number') {
this.setNanoseconds(args[8]);
}
}
}
getMicroseconds() {
return this.#microseconds;
}
setMicroseconds(value) {
if (!isNaN(value)) {
if (value >= 1e3) {
let extraMilliseconds = Math.floor(value / 1e3);
this.setMilliseconds(this.getMilliseconds() + extraMilliseconds);
value %= 1e3;
}
this.#microseconds = value;
} else {
super.setTime(NaN);
}
}
getNanoseconds() {
return this.#nanoseconds;
}
setNanoseconds(value) {
if (!isNaN(value)) {
if (value >= 1e3) {
let extraMicroseconds = Math.floor(value / 1e3);
this.setMicroseconds(this.getMicroseconds() + extraMicroseconds);
value %= 1e3;
}
this.#nanoseconds = value;
} else {
super.setTime(NaN);
}
}
setTime(value) {
if (typeof value === 'bigint') {
const nanosecondsTotal = value;
const milliseconds = Number(nanosecondsTotal / BigInt(1e6));
super.setTime(milliseconds);
this.#microseconds = Number((nanosecondsTotal / BigInt(1e3)) % BigInt(1e3));
this.#nanoseconds = Number(nanosecondsTotal % BigInt(1e3));
} else if (!isNaN(value) && typeof value === 'number') {
super.setTime(value);
this.#microseconds = 0;
this.#nanoseconds = 0;
} else {
super.setTime(value);
}
}
toISOString() {
let baseIsoString = super.toISOString();
let floatingPoint = baseIsoString.split('.')[1];
let ms = floatingPoint.substr(0, 3);
return `${baseIsoString.substring(0, baseIsoString.length - floatingPoint.length)}${ms}${this.#microseconds.toString().padStart(3, '0')}${this.#nanoseconds.toString().padStart(3, '0')}Z`;
}
getTime() {
const milliseconds = super.getTime();
if (isNaN(milliseconds)) return NaN;
return BigInt(milliseconds) * BigInt(1e6) + BigInt(this.#microseconds) * BigInt(1e3) + BigInt(this.#nanoseconds);
}
valueOf() {
return super.getTime();
}
static now() {
if (PreciseDate.#hasHrtime()) {
// Node.js environment
const nanoReference = PreciseDate.#hrtime();
const nanoTimeOrigin = PreciseDate.#preciseNow();
return nanoTimeOrigin + (PreciseDate.#hrtime() - nanoReference);
} else {
// Browser environment
return PreciseDate.#preciseNow();
}
}
static #hrtime() {
return process.hrtime.bigint();
}
static #hasHrtime() {
return Boolean(typeof process !== 'undefined' && process?.hrtime?.bigint);
}
static #msToNano(ms) {
const intPart = parseInt(ms);
const floatPart = parseFloat((ms - intPart).toFixed(4));
const nanoIntPart = BigInt(intPart) * BigInt(1e6);
const nanoFloatPart = BigInt(parseInt(floatPart * 1e6));
return nanoIntPart + nanoFloatPart;
}
static #preciseNow() {
return PreciseDate.#msToNano(performance.timeOrigin + performance.now());
}
}
const isNodeJs = typeof module !== 'undefined' && typeof module.exports !== 'undefined';
if (isNodeJs) {
module.exports = PreciseDate;
}