add time.ml, similar to libgnunetutil's

This commit is contained in:
swrup 2026-02-05 15:31:49 +01:00
parent 5eba5f2265
commit cec0e6afa9
17 changed files with 336 additions and 321 deletions

View file

@ -18,13 +18,34 @@ open Offline_impl
module Arg = struct
include Arg
(* in seconds *)
let timestamp =
Arg.Conv.make ~docv:"timestamp argument" ~parser:Timestamp.of_string
~pp:Timestamp.pp ()
let parser s =
match int_of_string_opt s with
| None -> Error "not an int"
| Some n -> Ok (Time.Timestamp.of_s (Int64.of_int n))
in
let pp fmt t =
match Time.Timestamp.to_s t with
| None -> Fmt.pf fmt "never"
| Some s -> Fmt.pf fmt "%Ld" s
in
Arg.Conv.make ~docv:"timestamp argument" ~parser ~pp ()
(* in seconds *)
let relative_time =
Arg.Conv.make ~docv:"relative time argument"
~parser:Timestamp.Span.of_string ~pp:Timestamp.Span.pp ()
let parser s =
match int_of_string_opt s with
| None -> Error "not an int"
| Some s -> Ok (Time.Relative.of_s (Int64.of_int s))
in
let pp fmt d =
let t = Time.Timestamp.of_absolute Time.Absolute.(add zero d) in
match Time.Timestamp.to_s t with
| None -> Fmt.pf fmt "never"
| Some s -> Fmt.pf fmt "%Ld" s
in
Arg.Conv.make ~docv:"relative time argument" ~parser ~pp ()
let b32 =
let pp fmt v = Fmt.pf fmt "%s" (B32.encode v) in