Time Library

Stof time library ("Time").

The time library is a part of Stof, however, is separately organized because it is system-dependent and cannot be broadly implemented for all embedded environments.

Time.now(): ms

Returns the current time (in milliseconds) since the Unix epoch.

See Units and Primitive Types to better understand Stof unit types.

Time.nowNano(): ns

Returns the current time (in nanoseconds) since the Unix epoch. This is helpful for situations that require more granularity than milliseconds.

Time.diff(prev: ms): ms

Returns the current time (in milliseconds) minus a previous timestamp, or the elapsed time since a call to "Time.now()".

The argument may be in alternative time units to milliseconds (will get converted), however, if the value does not have units, it will be assumed to be milliseconds (float or int).

fn example() {
    let ts = Time.now();
    
    // do something cool...
    
    let elapsed = Time.diff(ts);
    pln("This took: ", elapsed); // "This took: 1ms"
}

Time.diffNano(prev: ns): ns

Returns the current time (in nanoseconds) minus a previous timestamp, or the elapsed time since a call to "Time.now()" or "Time.nowNano()".

The argument may be in alternative time units to nanoseconds (will get converted), however, if the value does not have units, it will be assumed to be nanoseconds (float or int).

fn example() {
    let ts = Time.nowNano(); // more accuracy
    
    // do something cool...
    
    let elapsed = Time.diffNano(ts);
    pln("This took: ", elapsed); // "This took: 12345ns"
}

Time.sleep(duration: ms): void

Block the current thread for a duration (assumed to be milliseconds if no units).

#[test]
fn example() {
    let ts = Time.now();
    Time.sleep(2ms);
    assert(Time.diff(ts) > 2ms);
}

Time.sleepNano(duration: ns): void

Block the current thread for a duration (assumed to be nanoseconds if no units). Use this function instead of "sleep" if you need a higher granularity than milliseconds.

#[test]
fn example() {
    let ts = Time.nowNano();
    Time.sleepNano(1000us); // microseconds
    assert(Time.diffNano(ts) > 1000us);
}

Last updated

Was this helpful?