LibWeb: Bring performance.now() closer to spec

This commit is contained in:
stelar7 2024-10-05 18:01:19 +02:00 committed by Alexander Kalenik
commit f6991a2955
Notes: github-actions[bot] 2024-10-31 13:11:10 +00:00
5 changed files with 62 additions and 5 deletions

View file

@ -14,9 +14,32 @@ namespace Web::HighResolutionTime {
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global)
{
// FIXME: Implement this.
(void)global;
return 0;
// To get time origin timestamp, given a global object global, run the following steps, which return a duration:
// FIXME: 1. Let timeOrigin be global's relevant settings object's time origin.
auto time_origin = 0;
// Each group of environment settings objects that could possibly communicate in any way
// has an estimated monotonic time of the Unix epoch, a moment on the monotonic clock,
// whose value is initialized by the following steps:
// !. Let wall time be the wall clock's unsafe current time.
struct timeval tv;
gettimeofday(&tv, nullptr);
auto wall_time = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
// 2. Let monotonic time be the monotonic clock's unsafe current time.
auto monotonic_time = unsafe_shared_current_time();
// 3. Let epoch time be monotonic time - (wall time - Unix epoch)
auto epoch_time = monotonic_time - (wall_time - 0);
// 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
auto estimated_monotonic_time = coarsen_time(epoch_time);
// 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
return estimated_monotonic_time - time_origin;
}
// https://w3c.github.io/hr-time/#dfn-coarsen-time