Duration

First-class duration literals with suffixes, arithmetic, and component accessors. No import required.

Summary

Write a number immediately followed by a suffix: 5s, 200ms, 100ns, 2m30s. Supported suffixes: ns, us, ms, s, m, h, d. Internally stored as an int64_t nanosecond count with no float drift. Duration arithmetic: adding/subtracting durations gives a duration; dividing two durations gives a float ratio; multiplying by a scalar gives a duration. Comparing against a non-duration is a type error. Component accessors (.s,.ms, etc.) read the value in that unit. The repr picks the largest readable unit, so 90s prints as 1m30s.

Canonical

XS has a first-class Duration type. Any number written with a time suffix is a duration; no pragma or import is required:

let timeout = 5s
let frame   = 200ms
let warmup  = 2m30s
let tick    = 100ns
let micro   = 50us

println(timeout)         -- 5s
println(warmup + 1s)     -- 2m31s
println((1500ms).s)      -- 1.5

Supported suffixes: ns, us, ms, s, m, h, d. The suffix has to sit immediately after the number (no whitespace) so a bare identifier on the next line stays an identifier. Compound forms like 2m30s or 1h15m are also supported.

Internally, every duration is an int64_t count of nanoseconds, so the range is around plus or minus 292 years and there's no float drift on arithmetic. Arithmetic mirrors what you'd expect:

operationresult
dur + dur, dur - durDuration
dur * scalar, scalar * dur, dur / scalarDuration
dur / durfloat (ratio)
dur < dur, dur == durbool

Comparisons against non-durations are a type error rather than a silent coercion.

The repr picks the largest readable unit and trims trailing zeros, so 1500ns prints as 1.5us, 2500ms prints as 2.5s, and 90s prints as 1m30s. Component accessors (.ns, .us, .ms, .s, .m, .h, .d) read the duration in that unit, returning an int for .ns and a float for the rest.

let dt = 750ms
println(dt.ns)            -- 750000000
println(dt.s)             -- 0.75

The transpilers carry durations as raw nanosecond counts (numbers in the JS backend, int64_t in the C and WASM backends); a richer Duration runtime in those targets is a follow-up.