Ptime precision question

Hi there! I’ve recently used @dbuenzli’s Ptime library and noticed that it declares picosecond precision, but ignores anything but seconds precision.
E.g., I tryed this:

let Ok (rfc,_,_) = Ptime.of_rfc3339 "2018-05-17T12:34:34.123-00:00";;
Ptime.to_rfc3339 rfc;;
- : string = "2018-05-17T12:34:34-00:00"

As can be seen, milliseconds are ignored here. Also, there are no functions to convert Ptime.t to any integer type with precision more than a second.
So, is there any possibility in Ptime to work with time units, more precise than a second?

Thanks in advance

Please read the documentation about the frac_s argument of Ptime.to_rfc3339.

A Ptime.t value is not a duration it is a point on a timeline. You can convert it to a duration w.r.t. to the POSIX epoch via Ptime.to_span which you can then convert to a pico-second precision duration.

1 Like

Thank you for explaining. My fault, I had to read the documentation more carefully :slightly_smiling_face:

One more thing. Why this is done this way ? Why not use as much fractional digits as needed to represent the time stamp as accurately as possible ?

The reason is that if you use a fixed number of fractional digits and no time zone offset your time stamps can be ordered by binary sorting them (e.g. via sort(1)). This means that by default to_rfc3339 automatically outputs sortable stamps.

One could dispute the default of frac_s (0) but OTOH it seems to me that most of the rfc3339 timestamps you see in the wild lack any fractional part.