Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • πŸš€Welcome
  • ℹ️Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • πŸƒRun
      • πŸ§ͺTest
      • πŸ“šCLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • πŸͺ§Formats
Powered by GitBook
On this page
  • Time.now(): ms
  • Time.nowNano(): ns
  • Time.diff(prev: ms): ms
  • Time.diffNano(prev: ns): ns
  • Time.sleep(duration: ms): void
  • Time.sleepNano(duration: ns): void

Was this helpful?

  1. Reference
  2. CLI
  3. CLI Libraries

Time Library

Stof time library ("Time").

PreviousFilesystem LibraryNextHTTP Library

Last updated 3 months ago

Was this helpful?

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 .

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);
}
πŸ“š
Unix epoch