如何将 performance.now() 输出转换为 UTC 字符串?

How can I convert performance.now() output to a UTC String?

在我们的代码中,我们之前像这样计算事件之间的差异:

var beginTime = new Date();
// Do stuff
var endTime = new Date();
var duration = endTime.getTime() - beginTime.getTime();

console.log("Job began at " + beginTime.toUTCString()
            + " and took " + duration + " milliseconds.");

这会产生一个人类可读的字符串:

Job began at Thu Sep 28 2017 11:17:33 GMT-0500 (Central Daylight Time) and took 7000 milliseconds.

我们决定改用 High Resolution Time,使用更可靠的 performance.now()。但是,我们仍然希望能够包含一个人类可读的 UTC 时间字符串。

最初,我们试过这个:

var beginTime = performance.now();
// Do stuff
var endTime = performance.now();
var duration = endTime - beginTime;

console.log("Job began at " + new Date(beginTime).toUTCString() 
            + " and took " + duration + " seconds.");

我们发现持续时间是准确的,但是 new Date(performance.now()) 导致 UTC 值不准确(在撰写本文时,它提供了将近 50 年前的日期)。

Job began at Wed Dec 31 1969 20:10:46 GMT-0600 (Central Standard Time) and took 7000 milliseconds.

是否有更好的方法将 performance.now() 的输出转换为准确的 UTC 字符串?它不必与 new Date().toUTCString() 的格式完全相同,但它应该是人类可读的。

这是不可能的。 performance.now 的 return 值确实有 page load as their origin (0) value, and cannot be transformed into a date. According to the spec, you should be able to sum it with performance.timeOrigin 来获取 unix 时间戳,但是似乎没有任何浏览器支持这个。

如果您想知道您的测量从挂钟时间开始的时间,我建议您也存储一个常规的 new Date/Date.now() 时间戳。

来自 MDN,

unlike Date.now(), the values returned by Performance.now() always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP). Otherwise, performance.timing.navigationStart + performance.now() will be approximately equal to Date.now()

顾名思义,performance.now() 是在两个任务之间以 5 微秒的精度测量性能,而不是保持 UNIX timeStamp

performance.timing.navigationStart + performance.now() will be approximately equal to Date.now()

在此处了解更多信息。
https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

我会这样做:

// mark the start time
performance.mark("start");

// ... 
//  later...
// ...
// mark the end time
performance.mark("end");

// create a measure called 'm' based on the two marks above
performance.measure("m", "start", "end");

// get the start mark, calculate its real-world timestamp using the time origin
var started = performance.getEntriesByName("start")[0];
var startedDt = new Date(performance.timing.navigationStart + started.startTime);

// get the measure we created above
var duration = performance.getEntriesByName("m")[0];
console.log(`job began on ${startedDt} and took ${duration.duration/1000.0} seconds`);

performance.clearMarks();

首先,标记持续时间测量的开始和结束时间。其次,创建持续时间测量。

稍后,您将获得开始和结束标记,通过将时间原点与开始标记的时间戳相结合来计算开始日期。最后,清除标记。

获取开始标记并不是绝对必要的...您还可以根据度量 m 计算真实世界的开始时间戳,它也有一个 startTime。我使用了开始标记,但两者都有效。

换句话说,你也可以这样做:

// get the measure we created above
var duration = performance.getEntriesByName("m")[0];
var startedDt = new Date(performance.timing.navigationStart + duration.startTime);

console.log(`job began on ${startedDt} and took ${duration.duration/1000.0} seconds`);

可以这样做:

const t0 = performance.now();

// measured job here

const t1     = performance.now(),
      t0Date = new Date(performance.timing.navigationStart + t0).toUTCString();

console.log(`Job began at ${t0Date} and took ${t1 - t0} milliseconds.`);
/* Console formatting only */
.as-console-wrapper { top: 0; }

但是请注意,在 performance.now() MDN page(强调我的)之后:

(…) performance.timing.navigationStart + performance.now() will be approximately equal to Date.now().

对我来说,它在实际时间开始的一秒内。