Skip to content

Time (std.time)

Core standard library module for timestamp tracking.


Retrieve accurate system epoch timestamps in seconds or milliseconds:

import std.time
// Get current UNIX timestamp in seconds
let secs = time.now().toSeconds()
println($"Current Seconds: {secs}")
// Get current high-resolution UNIX timestamp object
let now = time.now()
println($"Timestamp in Milliseconds: {now.toMillis()}")
// Parse an RFC3339 or RFC2822 date string into a Timestamp
let parsed = time.parse("2026-08-10T08:00:00Z")
println($"Parsed Timestamp Millis: {parsed.toMillis()}")

Since timestamps are returned as Timestamp objects in Flame, you can easily access milliseconds or string representations.

import std.time
let now = time.now()
// Extract components
println($"Millis: {now.toMillis()}")
println($"Seconds: {now.toSeconds()}")
println($"String: {now.toString()}")

High-Resolution Benchmarking (time.perfCounter)

Section titled “High-Resolution Benchmarking (time.perfCounter)”

For microbenchmarks and profiling, Flame provides time.perfCounter() (and alias time.counter()). Equivalent to Python’s time.perf_counter(), it queries the system’s high-resolution monotonic clock with zero allocations and returns elapsed fractional seconds as a Float.

import std.time
let start = time.perfCounter()
// Run benchmark workload...
let mut sum = 0
for i in 0..1000000 {
sum = sum + i
}
let elapsed = time.perfCounter() - start
println($"Workload finished in {elapsed} seconds")

Use time.instant() to capture an immutable starting point in monotonic time, and query .elapsed() to receive a Duration object:

import std.time
let timer = time.instant()
// Execute code...
time.sleep(50)
let duration = timer.elapsed()
println($"Elapsed Millis: {duration.toMillis()} ms")
println($"Elapsed Seconds: {duration.toSeconds()} s")
println($"Formatted: {duration.toString()}")

Method Arguments Returns Description
time.perfCounter None Float Returns high-resolution performance counter in fractional seconds (monotonic, zero-alloc).
time.counter None Float Alias for time.perfCounter().
time.instant None Instant Returns a monotonic instant suitable for measuring elapsed durations.
time.now None Timestamp Returns the current UTC time as a Timestamp object.
time.parse date_string: String Timestamp Parses an ISO-8601 or RFC-3339 string into a Timestamp.
time.fromMillis milliseconds: Int Timestamp Creates a Timestamp from Unix epoch milliseconds.
time.fromSeconds seconds: Int Timestamp Creates a Timestamp from Unix epoch seconds.
time.sleep ms: Int Nil Suspends execution for the specified milliseconds.

The Timestamp object provides the following native methods:

Method Arguments Returns Description
.toMillis() None Int Returns the UNIX epoch milliseconds.
.toSeconds() None Int Returns the UNIX epoch seconds.
.toString() None String Returns the human readable UTC date time string.

Returned by Instant.elapsed(), the Duration object provides:

Method / Property Returns Description
.toMillis() Float Total elapsed milliseconds as a Float.
.toMilliseconds() Float Alias for .toMillis().
.toSeconds() Float Total elapsed seconds as a Float (e.g. 0.2850).
.toString() String Formatted duration string (e.g. 0.2850s).
.millis Int Whole elapsed milliseconds integer.
.nanos Int Whole elapsed nanoseconds integer.
.seconds Float Direct Float seconds value.