···2020 else if not (Re.execp tid_regexp tid) then
2121 raise (Invalid_argument (Format.sprintf "invalid tid format: %s" tid))
22222323-let _encode (n : int64) : t =
2424- let rec _encode ~tid n =
2323+let s32_encode (n : int64) : t =
2424+ let rec s32_encode ~tid n =
2525 match n with
2626 | 0L ->
2727 tid
2828 | n ->
2929- _encode
2929+ s32_encode
3030 ~tid:(String.make 1 charset.[Int64.to_int (Int64.rem n 32L)] ^ tid)
3131 (Int64.unsigned_div n 32L)
3232 in
3333- _encode ~tid:"" n
3333+ s32_encode ~tid:"" n
34343535-let _decode (s : t) : int64 =
3636- let rec _decode ~(n : int64) (s : string) =
3535+let s32_decode (s : t) : int64 =
3636+ let rec s32_decode ~(n : int64) (s : string) =
3737 match s with
3838 | s when String.length s > 0 ->
3939 let c = s.[0] in
4040 let cs = String.sub s 1 (String.length s - 1) in
4141- _decode
4141+ s32_decode
4242 ~n:
4343 (Int64.add (Int64.mul n 32L)
4444 (Int64.of_int (String.index charset c)) )
···4646 | _ ->
4747 n
4848 in
4949- _decode ~n:0L s
4949+ s32_decode ~n:0L s
50505151-let of_timestamp_us (timestamp : int64) ~(clockid : int) : t =
5151+let of_timestamp_us ?(clockid = Random.int_in_range ~min:0 ~max:1023)
5252+ (timestamp : int64) : t =
5253 if timestamp < 0L || timestamp >= Int64.shift_left 1L 53 then
5354 raise (Invalid_argument "timestamp must be within range [0, 2^53)") ;
5455 if clockid < 0 || clockid > 1023 then
···5657 let rec pad str len =
5758 if String.length str >= len then str else pad ("2" ^ str) len
5859 in
5959- pad (_encode timestamp) 11 ^ pad (_encode @@ Int64.of_int clockid) 2
6060+ pad (s32_encode timestamp) 11 ^ pad (s32_encode @@ Int64.of_int clockid) 2
60616161-let of_timestamp_ms (timestamp : int64) ~(clockid : int) : t =
6262- of_timestamp_us (Int64.mul timestamp 1000L) ~clockid
6262+let of_timestamp_ms ?(clockid = Random.int_in_range ~min:0 ~max:1023)
6363+ (timestamp : int64) : t =
6464+ of_timestamp_us
6565+ (Int64.add (Int64.mul timestamp 1000L) (Random.int64 1000L))
6666+ ~clockid
63676468let to_timestamp_us (tid : t) : int64 * int =
6569 ensure_valid tid ;
6666- let timestamp = _decode (String.sub tid 0 11) in
6767- let clockid = Int64.to_int @@ _decode (String.sub tid 11 2) in
7070+ let timestamp = s32_decode (String.sub tid 0 11) in
7171+ let clockid = Int64.to_int @@ s32_decode (String.sub tid 11 2) in
6872 (timestamp, clockid)
69737074let to_timestamp_ms (tid : t) : int64 * int =
7175 ensure_valid tid ;
7272- let timestamp = _decode (String.sub tid 0 11) in
7373- let clockid = Int64.to_int @@ _decode (String.sub tid 11 2) in
7676+ let timestamp = s32_decode (String.sub tid 0 11) in
7777+ let clockid = Int64.to_int @@ s32_decode (String.sub tid 11 2) in
7478 (Int64.div timestamp 1000L, clockid)
75797680let now () : t =
7777- Mtime_clock.now_ns () |> Int64.unsigned_div 1_000L
8181+ let calibrator = Lazy.force Time_stamp_counter.calibrator in
8282+ Time_stamp_counter.Calibrator.calibrate calibrator ;
8383+ Time_stamp_counter.now ()
8484+ |> Time_stamp_counter.to_time ~calibrator
8585+ |> Time_float_unix.to_span_since_epoch |> Time_float_unix.Span.to_us
8686+ |> Int64.of_float
7887 |> of_timestamp_us ~clockid:(Random.int_in_range ~min:0 ~max:1023)
79888089let of_string (s : string) : t = ensure_valid s ; s