time +linux +x86_64

The time module provides basic timekeeping primitives and access to the system's clocks. See time::chrono for incorporating human timescales and datetime for working with the ISO 8601 Gregorian calendar.




type clock;
type duration;
type instant;
type interval;


type ambiguous;
type error;
type nonexistent;


const HOUR: duration;
const MICROSECOND: duration;
const MILLISECOND: duration;
const MINUTE: duration;
const NANOSECOND: duration;
const SECOND: duration;


fn add(instant, duration) instant;
fn compare(instant, instant) i8;
fn diff(instant, instant) duration;
fn duration_to_timespec(duration, *rt::timespec) void;
fn from_unix(i64) instant;
fn instant_to_timespec(instant, *rt::timespec) void;
fn mult(instant, f64) instant;
fn now(clock) instant;
fn sleep(duration) void;
fn timespec_to_instant(rt::timespec) instant;
fn unix(instant) i64;


type clock[link]

type clock = enum {
	// The current wall-clock time. This may jump forwards or backwards in
// time to account for leap seconds, NTP adjustments, etc.
REALTIME = 0, // The current monotonic time. This clock measures from some undefined
// epoch and is not affected by leap seconds, NTP adjustments, and
// changes to the system time: it always increases by one second per
// second.
MONOTONIC = 1, PROCESS_CPU = 2, // Measures CPU time consumed by the calling process.
// Time since the system was booted. Increases monotonically and, unlike
// [[MONOTONIC]], continues to tick while the system is suspended.
BOOT = 7, REALTIME_ALARM = 8, // This clock is like [[REALTIME]], but will wake the system if it is suspended.
BOOT_ALARM = 9, // This clock is like [[BOOT]], but will wake the system if it is suspended.
TAI = 11, // A system-wide clock derived from wall-clock time but ignoring leap seconds.

An enumeration of clocks available on this system. Different clocks represent times from different epochs, and have different characteristics with regards to leap seconds, NTP adjustments, and so on. All systems provide the REALTIME and MONOTONIC clocks at least; use of other clocks is not guaranteed to be portable.

type duration[link]

type duration = i64;

The elapsed time between two instants, in nanoseconds. The largest representable duration is about 290 years.

type instant[link]

type instant = struct {
	sec: i64,
	nsec: i64,

Represents a specific instant in time as seconds (+nanoseconds) since an arbitrary epoch. Instants may only be meaningfully compared with other instants sourced from the same clock, or handled by the same time::chrono::timescale

type interval[link]

type interval = (instant, instant);

Represents a unique interval of time between two instants.


type ambiguous[link]

type ambiguous = ![]instant;

The conversion of an instant has multiple possible results.

type error[link]

type error = !(ambiguous | nonexistent);

All error types which are concerned with the handling of instants.

type nonexistent[link]

type nonexistent = !void;

The conversion of an instant has no possible result.


def HOUR[link]

def HOUR: duration;

duration representing an hour.


def MICROSECOND: duration;

duration representing a single microsecond.


def MILLISECOND: duration;

duration representing a single millisecond.

def MINUTE[link]

def MINUTE: duration;

duration representing a minute.

def NANOSECOND[link]

def NANOSECOND: duration;

duration representing a single nanosecond.

def SECOND[link]

def SECOND: duration;

duration representing a second.


fn add[link]

fn add(a: instant, d: duration) instant;

Adds a duration to an instant, returning an instant further in the future (given a positive duration), or further in the past (given a negative duration).

fn compare[link]

fn compare(a: instant, b: instant) i8;

Returns -1 if a precedes b, 0 if a and b are simultaneous, or +1 if b precedes a.

fn diff[link]

fn diff(a: instant, b: instant) duration;

Returns the duration from instant "a" to instant "b".

fn duration_to_timespec[link]

fn duration_to_timespec(d: duration, ts: *rt::timespec) void;

Converts a duration to an rt::timespec. This function is non-portable.

fn from_unix[link]

fn from_unix(u: i64) instant;

Converts a given Unix timestamp to an instant.

fn instant_to_timespec[link]

fn instant_to_timespec(t: instant, ts: *rt::timespec) void;

Converts an instant to an rt::timespec. This function is non-portable.

fn mult[link]

fn mult(a: instant, f: f64) instant;

Scales the given instant's scalar value by a factor 'f'. Make sure to know what epoch you're dealing with.

fn now[link]

fn now(clock: clock) instant;

Returns the current time for a given clock.

fn sleep[link]

fn sleep(d: duration) void;

Yields the process to the kernel and returns after the requested duration.

fn timespec_to_instant[link]

fn timespec_to_instant(ts: rt::timespec) instant;

Converts a rt::timespec to an rt::instant. This function is non-portable.

fn unix[link]

fn unix(a: instant) i64;

Converts a given instant to a Unix timestamp. Note, nanosecond information is lost during conversion.