1 # Monotonic clock adapter for consumers, outside the pure scheduler core. 2 import Base 3 import 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. 7 def 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 12 def now_us() -> IO(Nat): 13 do IO: 14 raw : U32 & U32 & U32 <- Time.mono.raw() 15 return micros(raw) 16 17 def now() -> IO(Nat): 18 do IO: 19 us : Nat <- now_us() 20 return Nat.div(us, 1000n)