-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Description
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.