source: frontend/node_modules/w3c-hr-time/lib/calculate-clock-offset.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.5 KB
Line 
1"use strict";
2
3// This files implements the calculation of the offset between the global monotonic clock and UNIX time. This value is
4// known as |t1| in the calculation of "time origin timestamp" in the spec. This value needs to be calculated once and
5// can be used in all subsequent Performance instances.
6//
7// However, if the clock is not fast enough, the export is undefined to signify that we should use Date.now() to get the
8// time origin timestamp with millisecond accuracy, per spec.
9
10const { getGlobalMonotonicClockMS } = require("./global-monotonic-clock");
11const clockIsAccurate = require("./clock-is-accurate");
12
13// This function assumes the clock is accurate.
14function calculateClockOffset() {
15 const start = Date.now();
16 let cur = start;
17 // Limit the iterations, just in case we're running in an environment where Date.now() has been mocked and is
18 // constant.
19 for (let i = 0; i < 1e6 && cur === start; i++) {
20 cur = Date.now();
21 }
22
23 // At this point |cur| "just" became equal to the next millisecond -- the unseen digits after |cur| are approximately
24 // all 0, and |cur| is the closest to the actual value of the UNIX time. Now, get the current global monotonic clock
25 // value and do the remaining calculations.
26
27 return cur - getGlobalMonotonicClockMS();
28}
29
30if (clockIsAccurate) {
31 // Warm up the function.
32 calculateClockOffset();
33 calculateClockOffset();
34 calculateClockOffset();
35
36 module.exports = calculateClockOffset;
37} else {
38 module.exports = undefined;
39}
Note: See TracBrowser for help on using the repository browser.