OxL Reference

data Duration

Exact time interval in nanoseconds (§17.1). Literals: 250ms, 1h30m.

Example:

oxl✓ checked in CI
fn main() io {
  let timeout: Duration = 1h30m
  print("{timeout.minutes()} {(250ms).millis()}")
}
Output
90 250

Examples#

Recipes for durations: literals, arithmetic, parsing text and measuring.

Durations#

The literals 1h30m, 45s, 500ms have type Duration; durations can be added, multiplied by a number and compared. now() and sleep need the time effect.

oxl✓ checked in CI
fn main() io, time {
  let a = 1h30m  // compound literals: 1h30m is 90 minutes
  let b = 45s + 500ms
  print("{a.minutes()} min, {b.millis()} ms, {b.secondsF()} s")
  print("{(a * 2).hours()} h, {(a / 3).minutes()} min, {a > b}")
  print("{Duration.ofSeconds(125).minutes()} {Duration.ofMillis(2500).secondsF()} {(0s - 5s).isNegative()}")
  match Duration.parse("2h15m") {
    Duration d => print("{d.minutes()}")
    ParseError => print("bad duration")
  }
  let start = now()  // now() reads the clock: it needs the time effect
  sleep(20ms)
  print("{now() - start >= 20ms}")
}
Output
90 min, 45500 ms, 45.5 s
3 h, 30 min, true
2 2.5 true
135
true
@builtin("Duration") pub(api) data Duration

Thread safety. an immutable value.

Since. 1.0.0-beta

Defined in: std/core/types.oxl:96

See also

OxL 1.0.0-beta · Documentation version 1.0.0-beta© OxwaldLink · generated from the Standard, the std sources and the guides