Docs/Standard library

time

Sleep the calling thread using nanosecond durations and integer time units.

Sleep in nanoseconds#

Import time. Sleep(nanoseconds int) blocks the calling thread for the requested duration, subject to native timer resolution and scheduling. Zero and negative durations return immediately. Durations use ordinary signed 64-bit integer arithmetic; ensure multiplication fits in int.

Ocean currently has no const declaration. The unit names below are exported int bindings used as constants by convention; do not assign to them.

Sleep in nanoseconds reference
UnitNanoseconds (int)
Nanosecond1
Microsecond1,000
Millisecond1,000,000
Second1,000,000,000
Ocean
package main;
import "time";

func main() {
    time.Sleep(10 * time.Millisecond);
    time.Sleep(500 * time.Microsecond);
}

Native waiting and cancellation#

Linux x86-64/AArch64/RISC-V64 use nanosleep system calls; macOS x86-64/AArch64 use nanosleep. Interrupted POSIX sleeps resume with the remaining duration. Windows x86-64/AArch64 use relative waitable timers, rounding requests upward to 100ns timer units. Hardware timer resolution and scheduling can extend waits; the implementation does not busy-wait or change system timer resolution. Native failures panic.

Long waits are split into native waits of at most 50ms, allowing cooperative cancellation at compiler checkpoints in threaded applications. time does not import threads or sync and does not add a pthread dependency on its own. Positive sleeps panic on unsupported platforms, including Wasm. Nonpositive sleeps always return immediately. Linux timing and signal interruption are tested; real macOS/Windows execution still needs host validation.

Measure monotonic time#

Monotonic() int returns monotonic nanoseconds with an unspecified origin. Use only differences between readings in the same process. Linux uses CLOCK_MONOTONIC, macOS uses clock_gettime(CLOCK_MONOTONIC), and Windows converts GetTickCount64 milliseconds to nanoseconds; the unit does not imply nanosecond precision. The Wasm implementation imports env.ocean_time_monotonic_ns() -> i64, supplied by the JavaScript host as a BigInt timestamp.