add time.ml, similar to libgnunetutil's

This commit is contained in:
swrup 2026-02-05 15:31:49 +01:00 committed by Swrup
parent b507d540ad
commit 5392bafddf
17 changed files with 336 additions and 321 deletions

View file

@ -150,6 +150,7 @@ module Parse_duration = struct
let duration = many1 duration_element <* end_of_input
(* TODO put this in Time.Relative *)
let dunit_to_seconds u =
let rec f = function
| `Year -> 365 * f `Day
@ -167,15 +168,14 @@ module Parse_duration = struct
fail "ptime_span_of_int64 error: `%Ld` is not a valid ptime span" i
| Some ts -> ts
let to_ptime_span t =
let to_time_span t =
let acc =
List.fold_left
(fun acc { number; dunit } -> acc + (number * dunit_to_seconds dunit))
0 t
in
let acc = Int64.of_int acc in
let ptime = ptime_span_of_int64 acc in
ptime
Time.Relative.of_s acc
let parse s : duration_element list =
match parse_string ~consume:All duration s with
@ -218,7 +218,7 @@ let yes_no = function
let uri s = Uri.of_string s
let amount s = s |> Amount.of_string |> unwrap_res
let duration s = Parse_duration.(s |> parse |> to_ptime_span)
let duration s = Parse_duration.(s |> parse |> to_time_span)
let ed25519 s =
s