diff --git a/src/timestamp.ml b/src/timestamp.ml index a63d74d1..b567ec8a 100644 --- a/src/timestamp.ml +++ b/src/timestamp.ml @@ -1,36 +1,43 @@ -type t = Ptime.t -type span = Ptime.span +type t = int64 option +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 s = Int64.(to_int (div us 1_000_000_L)) in - span_of_int_s s +let to_s us = + if us = never then None + 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 = - match span_to_int_s span with - | None -> None - | Some s -> - let us = Int64.(mul (of_int s) 1_000_000_L) in - Some us + let of_uint64_us = of_uint64_us + let to_uint64_us = to_uint64_us + let to_s = to_s +end -let of_int_s s = Ptime.of_span @@ span_of_int_s s -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 compare = Int64.unsigned_compare -let to_int64_us span = - match to_int_s span with - | None -> None - | Some s -> - let us = Int64.(mul (of_int s) 1_000_000_L) in - Some us +let now () = + let d, ps = Ptime_clock.now () |> Ptime.to_span |> Ptime.Span.to_d_ps in + if d < 0 then Fmt.failwith "Ptime_clock.now negative ptime" + else + let d = Int64.(mul (of_int d) 86_400_L) in + let us = Int64.(div ps 1_000_000_L) in + Int64.add d us -let now () = Ptime_clock.now () -let add_span t span = Ptime.add_span t span -let sub_span t span = Ptime.sub_span t span -let diff a b = Ptime.diff a b +let add_span t span = + let us = Int64.add t span in + if compare t us > 0 then None else Some us + +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 *) let jsont =