Skip to content

FR: add real-time clock version of uv_hrtime() #1674

@bnoordhuis

Description

@bnoordhuis

uv_hrtime() always uses a monotonic clock but that is not sufficient for all applications.

Example: new Date() in node.js currently needs platform-specific code because the spec mandates the use of a real-time clock (i.e., human time.)

My proposal:

struct uv_timespec {
  int64_t sec;  /* Seconds since UNIX epoch.  Negative for dates < 1970. */
  uint32_t nsec;
};

enum uv_clock_id {
  UV_CLOCK_MONOTONIC,
  UV_CLOCK_MONOTONIC_COARSE,  // XXX define as "at least 1 ms precision"?
  UV_CLOCK_REALTIME,
  UV_CLOCK_REALTIME_COARSE
 };

int uv_clock_gettime(enum uv_clock_id id, struct uv_timespec* ts);

Look familiar? uv_hrtime() can be reimplemented in terms of uv_clock_gettime().

Other interesting clocks can be added later if we error with UV_EINVAL for invalid clock ids so that downstream users can detect their (lack of) support at run-time.

The Windows implementation would probably use GetSystemTimePreciseAsFileTime() or GetSystemTimeAsFileTime() for the real-time clock.

Like gettimeofday() and clock_gettime(CLOCK_REALTIME), they return the time in UTC, but in "100-nanosecond intervals since January 1, 1601 (UTC)" according to MSDN.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions