posix-clock
v3.0.0
Published
POSIX clock_gettime(), clock_getres() and clock_nanosleep() for NodeJS
Downloads
45
Maintainers
Readme
POSIX clock_*()
for NodeJS
Installation
npm install posix-clock
WARNING: if you want to use a module with NodeJS before v12, then use module version 2:
npm install posix-clock@2
Examples
clock_getres()
var clock = require('posix-clock');
var clockResolution = clock.getres(clock.MONOTONIC);
console.log(
'Resolution of CLOCK_MONOTONIC: '
+ clockResolution.sec + ' sec and '
+ clockResolution.nsec + ' nanosec.'
, clockResolution
);
Resolution of CLOCK_MONOTONIC: 0 sec and 1 nanosec. { sec: 0, nsec: 1 }
clock_gettime()
var clock = require('posix-clock');
var clockTime = clock.gettime(clock.MONOTONIC);
console.log(
'Time from CLOCK_MONOTONIC: '
+ clockTime.sec + ' sec and '
+ clockTime.nsec + ' nanosec.'
, clockTime
);
Time from CLOCK_MONOTONIC: 15224 sec and 557776233 nanosec. { sec: 15224, nsec: 557776233 }
clock_nanosleep()
var clock = require('posix-clock');
// sleep until 13 Feb 2009 23:31:30 UTC (Unix Timestamp = 1234567890)
clock.nanosleep(
clock.REALTIME,
clock.TIMER_ABSTIME,
{
sec: 1234567890,
nsec: 0
}
);
// sleep at least 10 seconds and 123 nanoseconds
clock.nanosleep(
clock.REALTIME,
0,
{
sec: 10,
nsec: 123
}
);
API
Methods
gettime(clockId)
- the function retrieve the time from the specified clock clockId. See man 2 clock_gettime for more details.getres(clockId)
- the function return the resolution (precision) of the specified clock clockId. The resolution of clocks depends on the implementation and cannot be configured by a particular process. See man 2 clock_getres for more details.nanosleep(clockId, flags, sleepTime)
- high resolution sleep with specifiable clock. If the flagTIMER_ABSTIME
is not set in theflags
argument, thenanosleep()
function shall cause the current thread to be suspended from execution until either the time interval specified by thesleepTime
argument has elapsed, or a signal is delivered to the calling thread and its action is to invoke a signal-catching function, or the process is terminated. The clock used to measure the time shall be the clock specified by clockId. See man 2 clock_nanosleep for more details. On non-linux OS onlynanosleep(REALTIME, 0, {...})
is supported.
Clocks
REALTIME
- system-wide clock that measures real (i.e., wall-clock) time. This clock is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.MONOTONIC
- clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
Linux-specific
PROCESS_CPUTIME_ID
- since Linux 2.6.12. High-resolution per-process timer from the CPU.THREAD_CPUTIME_ID
- since Linux 2.6.12. Thread-specific CPU-time clock.REALTIME_COARSE
- since Linux 2.6.32; Linux-specific. A faster but less precise version ofREALTIME
. Use when you need very fast, but not fine-grained timestamps.MONOTONIC_COARSE
- since Linux 2.6.32; Linux-specific. A faster but less precise version ofMONOTONIC
. Use when you need very fast, but not fine-grained timestamps.MONOTONIC_RAW
- since Linux 2.6.28; Linux-specific. Similar toMONOTONIC
, but provides access to a raw hardware-based time that is not subject to NTP adjustments or the incremental adjustments performed by adjtime(3).BOOTTIME
- since Linux 2.6.39; Linux-specific Identical toMONOTONIC
, except it also includes any time that the system is suspended. This allows applications to get a suspend-aware monotonic clock without having to deal with the complications ofREALTIME
, which may have discontinuities if the time is changed using settimeofday(2).
FreeBSD-specific
REALTIME_FAST
- analog ofREALTIME
but do not perform a full time counter query, so their accuracy is one timer tickREALTIME_PRECISE
- analog ofREALTIME
but get the most exact value as possible, at the expense of execution timeMONOTONIC_FAST
- analog ofMONOTONIC
but do not perform a full time counter query, so their accuracy is one timer tickMONOTONIC_PRECISE
- analog ofMONOTONIC
but get the most exact value as possible, at the expense of execution timeUPTIME
- which starts at zero when the kernel boots and increments monotonically in SI seconds while the machine is runningUPTIME_FAST
- analog ofUPTIME
but do not perform a full time counter query, so their accuracy is one timer tickUPTIME_PRECISE
- analog ofUPTIME
but get the most exact value as possible, at the expense of execution timeSECOND
- returns the current second without performing a full time counter query, using in-kernel cached value of current second.PROF
- for time that increments when the CPU is running in user or kernel mode