Skip to content

clock_gettime function only returns seconds. #1795

@MarkCallow

Description

@MarkCallow

clock_gettime has two problems.

  • It does not use a monotonic timer when CLOCK_MONOTONIC is set.
  • It does not set the nanosecond part of the returned timespec.

Something like the following change should be made

-    var now = Date.now();
-    {{{ makeSetValue('tp', '___timespec_struct_layout.tv_sec', 'Math.floor(now/1000)', 'i32') }}}; // seconds

-    {{{ makeSetValue('tp', '___timespec_struct_layout.tv_nsec', '0', 'i32') }}}; // nanoseconds - not supported
----
+   var now, msec, fmsec;
+   if (clk_id == CLOCK_REALTIME) {
+     now = Date.now();
+   } else {
+     now = performance.now();
+   }
+   msec = Math.floor(now);
+   fmsec = now - msec;
+   {{{ makeSetValue('tp', '___timespec_struct_layout.tv_sec', 'msec/1000', 'i32') }}}; // seconds

+   {{{ makeSetValue('tp', '___timespec_struct_layout.tv_nsec', 'fmsec*1000', 'i32') }}}; // nanoseconds

I'm not sure how widespread support is for the high resolution timer (performance.now()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions