I’d like to move code from the oclock library to mtime - both provide access to a monotonic clock with mtime being quite a bit more abstract. I’m struggling to create in mtime a time span of a given length. The Mtime.Span module provides arithmetic on time stamps but I fail to see how to construct a timestamp of a given length.
The application is in a scheduler where each item is marked with a time in the future now + delay and my problem is to express the delay as a time span. Is this use case not covered by the library?
I just took a look at the documentation. From what I read, it seems the easiest way to create a time span is to convert a number of nanoseconds (as a int64) into a time span using Mtime.Span.of_uint64_ns. So all you have to do is to get the delay that you want expressed as a number of nanoseconds, using all the arithmetic available in the Int64 module, or computing the delay as an int and then converting it to a int64. A full example can then be:
(* 1 second delay in nanoseconds *)
let delay_ns = Int64.(of_int 1_000_000_000) in
(* convert a nanoseconds delay into a time span *)
let delay = Mtime.Span.of_uint64_ns delay_ns in
(* compute target time *)
let now = Mtime_clock.now () in
let target = Mtime.add_span now delay in
(* .... *)
(* Test wether we have passed the target time *)
if Mtime.is_later (Mtime_clock.now ()) ~than:target then
() (* scheduled task can be executed *)