This commit is contained in:
swrup 2025-12-07 15:03:23 +01:00
parent 7e7bc836d1
commit 5a18683689

View file

@ -1,36 +1,43 @@
type t = Ptime.t type t = int64 option
type span = Ptime.span type span = t
let span_of_int_s = Ptime.Span.of_int_s let never = -1L
let of_uint64_us us = if us = never then None else Some us
let to_uint64_us = function None -> never | Some us -> us
let span_of_int64_us us = let to_s us =
let s = Int64.(to_int (div us 1_000_000_L)) in if us = never then None
span_of_int_s s else
let s = Int64.unsigned_div us 86_400_L in
Some s
let span_to_int_s = Ptime.Span.to_int_s module Span = struct
type t = span
let span_to_int64_us span = let of_uint64_us = of_uint64_us
match span_to_int_s span with let to_uint64_us = to_uint64_us
| None -> None let to_s = to_s
| Some s -> end
let us = Int64.(mul (of_int s) 1_000_000_L) in
Some us
let of_int_s s = Ptime.of_span @@ span_of_int_s s let compare = Int64.unsigned_compare
let of_int64_us us = Ptime.of_span @@ span_of_int64_us us
let to_int_s t = Ptime.Span.to_int_s (Ptime.to_span t)
let to_int64_us span = let now () =
match to_int_s span with let d, ps = Ptime_clock.now () |> Ptime.to_span |> Ptime.Span.to_d_ps in
| None -> None if d < 0 then Fmt.failwith "Ptime_clock.now negative ptime"
| Some s -> else
let us = Int64.(mul (of_int s) 1_000_000_L) in let d = Int64.(mul (of_int d) 86_400_L) in
Some us let us = Int64.(div ps 1_000_000_L) in
Int64.add d us
let now () = Ptime_clock.now () let add_span t span =
let add_span t span = Ptime.add_span t span let us = Int64.add t span in
let sub_span t span = Ptime.sub_span t span if compare t us > 0 then None else Some us
let diff a b = Ptime.diff a b
let sub_span t span =
let us = Int64.sub t span in
if compare t us < 0 then None else Some us
let diff a b = if compare a b < 0 then None else Some (Int64.sub a b)
(* (*
(* microseconds since the UNIX Epoch, or "never" if None *) (* microseconds since the UNIX Epoch, or "never" if None *)
let jsont = let jsont =