@@ -42,12 +42,7 @@ function normalizeTimestamp(input) {
4242 }
4343
4444 if ( isTimeInputHrTime ( input ) === true ) {
45- // It's unclear why upstream tries to support a `process.hrtime()` value.
46- // Such tuples are relative to an arbitrary point in time, not the Unix
47- // epoch. So there's no way to determine an actual wall clock time and date
48- // from such input. For lack of a better solution, we'll return the
49- // current epoch in this case.
50- return Date . now ( )
45+ return hrtimeToMilliseconds ( input )
5146 }
5247
5348 if ( Object . getPrototypeOf ( input ) === Date . prototype ) {
@@ -57,3 +52,28 @@ function normalizeTimestamp(input) {
5752 // We don't know what they've given us, so just return the current time.
5853 return Date . now ( )
5954}
55+
56+ const { performance } = require ( 'node:perf_hooks' )
57+
58+ /**
59+ * Converts a JS Open Telemetry hrtime tuple to milliseconds since the
60+ * standard epoch.
61+ *
62+ * @param {number[] } hrtime Upstream represents hrtime as `[seconds, nanoseconds]`.
63+ *
64+ * @returns {number } Milliseconds since standard epoch time.
65+ */
66+ function hrtimeToMilliseconds ( hrtime ) {
67+ const seconds = hrtime [ 0 ]
68+ const nanoseconds = hrtime [ 1 ]
69+
70+ // Convert the seconds portion to milliseconds.
71+ const msSeconds = seconds * 1_000
72+ // Convert the nanoseconds portion to milliseconds.
73+ const msNanoseconds = nanoseconds / 1_000_000
74+ // At this point, we have the number of milliseconds since the application
75+ // was started.
76+ const msSinceStart = msSeconds + msNanoseconds
77+
78+ return Math . trunc ( performance . timeOrigin + msSinceStart )
79+ }
0 commit comments