clock.bend

bend-kit-scheduler@0.1.0.0 · 20 lines · 0 definitions

1# Monotonic clock adapter for consumers, outside the pure scheduler core.
2import Base
3import bend-kit-time@0.1.2.1/time.bend as Time
4
5# Microseconds avoid exceeding native Nat's 2^48-1 ceiling on normal uptimes.
6# Crossing that ceiling reports Bend's explicit Nat-overflow error, never wraps.
7def micros(r: U32 & U32 & U32) -> Nat:
8  (hi, lo, ns) = r
9  ((U32.to_nat(hi) * (65536n * 65536n : Nat) + U32.to_nat(lo)) *
10    1000000n + U32.to_nat((ns / 1000 : U32)) : Nat)
11
12def now_us() -> IO(Nat):
13  do IO<Nat>:
14    raw : U32 & U32 & U32 <- Time.mono.raw()
15    return micros(raw)
16
17def now() -> IO(Nat):
18  do IO<Nat>:
19    us : Nat <- now_us()
20    return Nat.div(us, 1000n)