UNPKG

5.51 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.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;
19const platform_1 = require("../platform");
20const NANOSECOND_DIGITS = 9;
21const NANOSECOND_DIGITS_IN_MILLIS = 6;
22const MILLISECONDS_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS_IN_MILLIS);
23const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
24/**
25 * Converts a number of milliseconds from epoch to HrTime([seconds, remainder in nanoseconds]).
26 * @param epochMillis
27 */
28function millisToHrTime(epochMillis) {
29 const epochSeconds = epochMillis / 1000;
30 // Decimals only.
31 const seconds = Math.trunc(epochSeconds);
32 // Round sub-nanosecond accuracy to nanosecond.
33 const nanos = Math.round((epochMillis % 1000) * MILLISECONDS_TO_NANOSECONDS);
34 return [seconds, nanos];
35}
36exports.millisToHrTime = millisToHrTime;
37function 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}
45exports.getTimeOrigin = getTimeOrigin;
46/**
47 * Returns an hrtime calculated via performance component.
48 * @param performanceNow
49 */
50function 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}
55exports.hrTime = hrTime;
56/**
57 *
58 * Converts a TimeInput to an HrTime, defaults to _hrtime().
59 * @param time
60 */
61function timeInputToHrTime(time) {
62 // process.hrtime
63 if (isTimeInputHrTime(time)) {
64 return time;
65 }
66 else if (typeof time === 'number') {
67 // Must be a performance.now() if it's smaller than process start time.
68 if (time < getTimeOrigin()) {
69 return hrTime(time);
70 }
71 else {
72 // epoch milliseconds or performance.timeOrigin
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}
83exports.timeInputToHrTime = timeInputToHrTime;
84/**
85 * Returns a duration of two hrTime.
86 * @param startTime
87 * @param endTime
88 */
89function hrTimeDuration(startTime, endTime) {
90 let seconds = endTime[0] - startTime[0];
91 let nanos = endTime[1] - startTime[1];
92 // overflow
93 if (nanos < 0) {
94 seconds -= 1;
95 // negate
96 nanos += SECOND_TO_NANOSECONDS;
97 }
98 return [seconds, nanos];
99}
100exports.hrTimeDuration = hrTimeDuration;
101/**
102 * Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123456Z"
103 * @param time
104 */
105function 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}
112exports.hrTimeToTimeStamp = hrTimeToTimeStamp;
113/**
114 * Convert hrTime to nanoseconds.
115 * @param time
116 */
117function hrTimeToNanoseconds(time) {
118 return time[0] * SECOND_TO_NANOSECONDS + time[1];
119}
120exports.hrTimeToNanoseconds = hrTimeToNanoseconds;
121/**
122 * Convert hrTime to milliseconds.
123 * @param time
124 */
125function hrTimeToMilliseconds(time) {
126 return time[0] * 1e3 + time[1] / 1e6;
127}
128exports.hrTimeToMilliseconds = hrTimeToMilliseconds;
129/**
130 * Convert hrTime to microseconds.
131 * @param time
132 */
133function hrTimeToMicroseconds(time) {
134 return time[0] * 1e6 + time[1] / 1e3;
135}
136exports.hrTimeToMicroseconds = hrTimeToMicroseconds;
137/**
138 * check if time is HrTime
139 * @param value
140 */
141function isTimeInputHrTime(value) {
142 return (Array.isArray(value) &&
143 value.length === 2 &&
144 typeof value[0] === 'number' &&
145 typeof value[1] === 'number');
146}
147exports.isTimeInputHrTime = isTimeInputHrTime;
148/**
149 * check if input value is a correct types.TimeInput
150 * @param value
151 */
152function isTimeInput(value) {
153 return (isTimeInputHrTime(value) ||
154 typeof value === 'number' ||
155 value instanceof Date);
156}
157exports.isTimeInput = isTimeInput;
158/**
159 * Given 2 HrTime formatted times, return their sum as an HrTime.
160 */
161function addHrTimes(time1, time2) {
162 const out = [time1[0] + time2[0], time1[1] + time2[1]];
163 // Nanoseconds
164 if (out[1] >= SECOND_TO_NANOSECONDS) {
165 out[1] -= SECOND_TO_NANOSECONDS;
166 out[0] += 1;
167 }
168 return out;
169}
170exports.addHrTimes = addHrTimes;
171//# sourceMappingURL=time.js.map
\No newline at end of file