libstd/time

Clocks, timestamps, sleeping and measuring durations, over clock_gettime/clock_nanosleep.

#include <libstd/time>

Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/systime and wrapper/windows/systime (see Linux and Windows).

Example

#include <libstd/stdio>
#include <libstd/time>

t0 = new Timespec()
monotonic(t0)
sleep_ms(150 as long)
print("elapsed: ", since_ms(t0), " ms\n")       // >= 150
print("now: ", timestamp(), " s since the epoch\n")

Constants

NameLinuxWindowsMeaning
CLOCK_REALTIME00
CLOCK_MONOTONIC11
CLOCK_MONOTONIC_RAW44
CLOCK_BOOTTIME77
CLOCK_PROCESS_CPUTIME_ID22
CLOCK_THREAD_CPUTIME_ID33
TIMER_RELATIVE00
TIMER_ABSTIME11

Classes

class
Timeval

Seconds and microseconds.

    tv_sec long
    tv_usec long
class
Timespec

Seconds and nanoseconds, as written by the clock syscalls.

    tv_sec long
    tv_nsec long

Functions

def now(tp: Timespec) -> None

Wall-clock time (CLOCK_REALTIME) into tp.

def monotonic(tp: Timespec) -> None

Monotonic time (CLOCK_MONOTONIC) into tp: for measuring durations.

def timestamp() -> long

Seconds since the Unix epoch.

def timestamp_ms() -> long

Milliseconds since the Unix epoch.

def elapsed_ms(start: Timespec, end: Timespec) -> long

Milliseconds between two instants (typically from monotonic).

def since_ms(start: Timespec) -> long

Milliseconds elapsed from start until now.

def sleep_ms(ms: long) -> None

Sleeps for the given milliseconds (a signal can end it early, silently).

def sleep_seconds(sec: long) -> None

Sleeps for the given seconds.

def cpu_time_ms() -> long

CPU time consumed by the process so far, all threads, in milliseconds.

def thread_cpu_time_ms() -> long

CPU time consumed by the calling thread, in milliseconds.

def clock_resolution_ns(which_clock: int) -> long

Resolution of a clock in nanoseconds.

def sys_clock_settime(which_clock: int, tp: Timespec) -> None

Sets a clock (needs privileges).

def sys_clock_gettime(which_clock: int, tp: Timespec) -> None

Reads a clock into a Timespec.

def sys_clock_getres(which_clock: int, tp: Timespec) -> None

Reads the resolution of a clock into a Timespec.

def sys_clock_nanosleep(which_clock: int, flags: int, request: Timespec, remain: Timespec) -> int

Sleeps for request (relative or absolute, see flags); the remaining time on interruption is written to remain.