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.addHrTimes = exports.isTimeInput = exports.isTimeInputHrTime = exports.hrTimeToMicroseconds = exports.hrTimeToMilliseconds = exports.hrTimeToNanoseconds = exports.hrTimeToTimeStamp = exports.hrTimeDuration = exports.timeInputToHrTime = exports.hrTime = exports.getTimeOrigin = exports.millisToHrTime = void 0;
|
19 | const platform_1 = require("../platform");
|
20 | const NANOSECOND_DIGITS = 9;
|
21 | const NANOSECOND_DIGITS_IN_MILLIS = 6;
|
22 | const MILLISECONDS_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS_IN_MILLIS);
|
23 | const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function millisToHrTime(epochMillis) {
|
29 | const epochSeconds = epochMillis / 1000;
|
30 |
|
31 | const seconds = Math.trunc(epochSeconds);
|
32 |
|
33 | const nanos = Math.round((epochMillis % 1000) * MILLISECONDS_TO_NANOSECONDS);
|
34 | return [seconds, nanos];
|
35 | }
|
36 | exports.millisToHrTime = millisToHrTime;
|
37 | function getTimeOrigin() {
|
38 | let timeOrigin = platform_1.otperformance.timeOrigin;
|
39 | if (typeof timeOrigin !== 'number') {
|
40 | const perf = platform_1.otperformance;
|
41 | timeOrigin = perf.timing && perf.timing.fetchStart;
|
42 | }
|
43 | return timeOrigin;
|
44 | }
|
45 | exports.getTimeOrigin = getTimeOrigin;
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function hrTime(performanceNow) {
|
51 | const timeOrigin = millisToHrTime(getTimeOrigin());
|
52 | const now = millisToHrTime(typeof performanceNow === 'number' ? performanceNow : platform_1.otperformance.now());
|
53 | return addHrTimes(timeOrigin, now);
|
54 | }
|
55 | exports.hrTime = hrTime;
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function timeInputToHrTime(time) {
|
62 |
|
63 | if (isTimeInputHrTime(time)) {
|
64 | return time;
|
65 | }
|
66 | else if (typeof time === 'number') {
|
67 |
|
68 | if (time < getTimeOrigin()) {
|
69 | return hrTime(time);
|
70 | }
|
71 | else {
|
72 |
|
73 | return millisToHrTime(time);
|
74 | }
|
75 | }
|
76 | else if (time instanceof Date) {
|
77 | return millisToHrTime(time.getTime());
|
78 | }
|
79 | else {
|
80 | throw TypeError('Invalid input type');
|
81 | }
|
82 | }
|
83 | exports.timeInputToHrTime = timeInputToHrTime;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function hrTimeDuration(startTime, endTime) {
|
90 | let seconds = endTime[0] - startTime[0];
|
91 | let nanos = endTime[1] - startTime[1];
|
92 |
|
93 | if (nanos < 0) {
|
94 | seconds -= 1;
|
95 |
|
96 | nanos += SECOND_TO_NANOSECONDS;
|
97 | }
|
98 | return [seconds, nanos];
|
99 | }
|
100 | exports.hrTimeDuration = hrTimeDuration;
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function hrTimeToTimeStamp(time) {
|
106 | const precision = NANOSECOND_DIGITS;
|
107 | const tmp = `${'0'.repeat(precision)}${time[1]}Z`;
|
108 | const nanoString = tmp.substr(tmp.length - precision - 1);
|
109 | const date = new Date(time[0] * 1000).toISOString();
|
110 | return date.replace('000Z', nanoString);
|
111 | }
|
112 | exports.hrTimeToTimeStamp = hrTimeToTimeStamp;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function hrTimeToNanoseconds(time) {
|
118 | return time[0] * SECOND_TO_NANOSECONDS + time[1];
|
119 | }
|
120 | exports.hrTimeToNanoseconds = hrTimeToNanoseconds;
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function hrTimeToMilliseconds(time) {
|
126 | return time[0] * 1e3 + time[1] / 1e6;
|
127 | }
|
128 | exports.hrTimeToMilliseconds = hrTimeToMilliseconds;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function hrTimeToMicroseconds(time) {
|
134 | return time[0] * 1e6 + time[1] / 1e3;
|
135 | }
|
136 | exports.hrTimeToMicroseconds = hrTimeToMicroseconds;
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function isTimeInputHrTime(value) {
|
142 | return (Array.isArray(value) &&
|
143 | value.length === 2 &&
|
144 | typeof value[0] === 'number' &&
|
145 | typeof value[1] === 'number');
|
146 | }
|
147 | exports.isTimeInputHrTime = isTimeInputHrTime;
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function isTimeInput(value) {
|
153 | return (isTimeInputHrTime(value) ||
|
154 | typeof value === 'number' ||
|
155 | value instanceof Date);
|
156 | }
|
157 | exports.isTimeInput = isTimeInput;
|
158 |
|
159 |
|
160 |
|
161 | function addHrTimes(time1, time2) {
|
162 | const out = [time1[0] + time2[0], time1[1] + time2[1]];
|
163 |
|
164 | if (out[1] >= SECOND_TO_NANOSECONDS) {
|
165 | out[1] -= SECOND_TO_NANOSECONDS;
|
166 | out[0] += 1;
|
167 | }
|
168 | return out;
|
169 | }
|
170 | exports.addHrTimes = addHrTimes;
|
171 |
|
\ | No newline at end of file |