Skip to content
Systems programming for what’s next.
— Vex

Time & Duration API ​

Time Struct ​

Time stores a UTC Unix timestamp in signed i64 nanoseconds. Use the public constructors to make units explicit; use Instant for elapsed time and deadlines.

Constructors ​

FunctionDescription
now()Current wall-clock time
Time.fromUnixNanos(value)From signed Unix nanoseconds
Time.unix(seconds, nanoseconds)Normalize seconds plus an offset; panic on overflow
Time.checkedUnix(seconds, nanoseconds)The same conversion as Option<Time>

Time.now() and now() use the same target-selected VexArch wall clock. Valid pre-1970 readings remain negative, and native failures or timestamps outside the signed i64 nanosecond range terminate with a runtime diagnostic. They never silently return the epoch or wrap into a different date. Wall time can move backwards after system-clock corrections; use Instant.now() for elapsed-time measurement and deadlines.

Instant.now() also uses the shared VexArch provider, including its checked monotonic conversion and Windows frequency cache. Linux clock reads do not require libc clock_gettime. Blocking sleep also uses the runtime directly; only timezone operations retain their separate native requirements here. No application-facing clock configuration is needed.

Calendar Accessors ​

MethodReturnDescription
.year(): i642024Year
.month(): MonthMonth.JuneMonth enum
.day(): i6415Day of month
.hour(): i6414Hour (0–23)
.minute(): i6430Minute (0–59)
.second(): i640Second (0–59)
.weekday(): WeekdayWeekday.SaturdayDay of week

Unix Representations ​

MethodDescription
.unix(): i64Seconds since epoch
.unixNano(): i64Nanoseconds since epoch
.unixMilli(): i64Milliseconds since epoch
.unixMicro(): i64Microseconds since epoch
.nanosecond(): i64Nanosecond component within the current second (0..999_999_999)

Unix conversion methods use floor semantics for instants before 1970. For example, an instant 500 ms before the epoch has unix() == -1, unixMilli() == -500, and nanosecond() == 500_000_000.

Arithmetic ​

vex
import { Duration, Time, add, sub } from "time";

let start = Time.fromUnixNanos(0);
let finish = start.add(Duration.seconds(30));
let elapsed: Duration = finish.durationSince(start);
let sameFinish = add(start, Duration.seconds(30));
let sameElapsed: Duration = sub(finish, start);

add takes a Duration, not a raw nanosecond integer. durationSince and the free sub return Duration; retrieve its count with asNanos() when needed. checkedAdd and checkedDurationSince return Option on overflow; the corresponding ordinary operations panic. saturatingAdd clamps to the representable endpoint instead.

Fluent API (Day.js Style) ​

Truncate or round time to boundaries:

vex
let t = now();

// Start of day (midnight)
let dayStart = t.startOf("day");

// End of current hour
let hourEnd = t.endOf("hour");

// Start of month
let monthStart = t.startOf("month");

Supported units: "year" / "y", "month" / "M", "day" / "d", "hour" / "h", "minute" / "m", "second" / "s"

Formatting ​

vex
$println(t.toString()); // RFC 3339 UTC representation of t

Duration Struct ​

MethodDescription
Duration.nanoseconds(value)Construct from signed nanoseconds
Duration.microseconds(value) / .milliseconds(value)Construct with explicit units
Duration.seconds(value) / .minutes(value) / .hours(value)Scale with checked overflow
.asNanos()Retrieve the signed nanosecond count

Time Constants ​

vex
import { NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, sleep } from "time";

sleep(500 * MILLISECOND);
let timeout = 30 * SECOND;
let interval = 1 * HOUR;

Blocking sleep ​

sleep(nanoseconds: i64) and sleep(duration: Duration) share VexArch’s target-selected sleeper. Zero and negative durations return immediately without yielding. This blocking API is distinct from scheduler await sleep(...).

On Linux, the runtime uses an absolute monotonic clock_nanosleep syscall without libc. On macOS, interrupted nanosleep calls retry only the time left until the original monotonic deadline. Windows uses a one-shot waitable timer, rounding positive durations upward to 100 ns ticks; it does not clip long durations to a 32-bit millisecond timeout. A kernel that rejects the high-resolution timer flag is retried with the standard timer flag.

Native failure or deadline overflow emits a runtime diagnostic and terminates with exit status 70 instead of pretending that time elapsed. Operating-system scheduling can delay the wake; no real-time scheduling guarantee is implied.

Parsing ​

vex
import { parse, parse_rfc3339 } from "time";

match parse_rfc3339("2024-06-15T14:30:00Z") {
    Ok(value) => { $println(value.year()); }, // 2024
    Err(message) => { $eprintln(message); },
};

let localFields = parse("YYYY-MM-DD HH:mm:ss", "2024-06-15 14:30:00");

parse_rfc3339(input) and parse(layout, input) return Result<Time, string>. The latter requires an explicit layout; it is not an automatic format detector. Its current layout tokens are YYYY, MM, DD, HH, mm, and ss, with other characters matched literally. Missing date/time fields default to the Unix-epoch date and zero clock fields. The layout parser interprets the fields as UTC; a string without an offset does not infer the machine's local zone. Use parse_rfc3339 for fractional seconds and explicit UTC offsets.

Calendar accessors and startOf/endOf in this page use UTC. Named-zone conversion and daylight-saving behavior belong to the location APIs; a fixed 24-hour Duration is not a general replacement for adding a local calendar day.

Enums ​

vex
enum Month { January, February, ..., December }
enum Weekday { Sunday, Monday, ..., Saturday }

Released under the MIT License.