1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.TEST_ONLY = exports.timestampFromMillis = exports.getTimestampWithProcessHRTime = void 0;
|
19 | const MILLIS_PER_SECOND = 1e3;
|
20 | const NANOS_PER_MILLI = 1e3 * 1e3;
|
21 | const NANOS_PER_SECOND = 1e3 * 1e3 * 1e3;
|
22 | let hrtime = process.hrtime;
|
23 | let hrtimeOrigin = [0, 0];
|
24 | let hrtimeRefSeconds = 0;
|
25 | let hrtimeRefNanos = 0;
|
26 | function setHrtimeReference() {
|
27 | resetHrtimeFunctionCache();
|
28 | hrtimeOrigin = hrtime();
|
29 | const refTime = Date.now();
|
30 | hrtimeRefSeconds = Math.floor(refTime / MILLIS_PER_SECOND);
|
31 | hrtimeRefNanos = (refTime % MILLIS_PER_SECOND) * NANOS_PER_MILLI;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function resetHrtimeFunctionCache() {
|
38 | hrtime = process.hrtime;
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function getTimestampWithProcessHRTime() {
|
46 | const [offsetSecs, offsetNanos] = hrtime(hrtimeOrigin);
|
47 |
|
48 | const seconds = hrtimeRefSeconds + offsetSecs;
|
49 | const nanos = hrtimeRefNanos + offsetNanos;
|
50 | if (nanos >= NANOS_PER_SECOND) {
|
51 | return { seconds: seconds + 1, nanos: nanos % NANOS_PER_SECOND };
|
52 | }
|
53 | return { seconds, nanos };
|
54 | }
|
55 | exports.getTimestampWithProcessHRTime = getTimestampWithProcessHRTime;
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function timestampFromMillis(epochMilli) {
|
63 | return {
|
64 | seconds: Math.floor(epochMilli / MILLIS_PER_SECOND),
|
65 | nanos: (epochMilli % MILLIS_PER_SECOND) * NANOS_PER_MILLI,
|
66 | };
|
67 | }
|
68 | exports.timestampFromMillis = timestampFromMillis;
|
69 | setHrtimeReference();
|
70 | exports.TEST_ONLY = {
|
71 | setHrtimeReference,
|
72 | resetHrtimeFunctionCache,
|
73 | };
|
74 |
|
\ | No newline at end of file |