Get nanos corresponding to MONOTONIC_CLOCK/CLOCK_BOOTTIME as unsigned integer to be sent over wire as bytes

I ve found https://doc.rust-lang.org/std/time/struct.Instant.html which corresponds to MONOTONIC_CLOCK time in nanoseconds. I would like to convert Instant to bytes to be sent over wire. However i am not able to convert from Instant to u64 value.

The documentation mentions Instant to be used only with Duration.

I am open to other ideas regarding how to serialize Instant.

Thanks;

Answering myself, i could not find a way to get around using Instant and ended up using https://docs.rs/time/0.1.42/time/fn.precise_time_ns.html.

It would be a good idea to expose an API to retrieve nanos behind std::time::Instant

Instant or the value from MONOTONIC_CLOCK represents a time span from some undefined time point. The only guarantee POSIX requires is that this point must not be changed within process, so it doesn't make sense at all to compare this value with the one from other process. On linux this point is implemented as a system boot time, but relying on this observation should not be considered as a good practice.

Anyway, there's SystemTime that can return globally meaningful value when compared with UNIX_EPOCH. Why don't you use this one instead?

3 Likes

SystemTime is not monotonic. I am looking for something monotonic value.

Also i am using this value on the response to compute round trip latency, so it makes sense because it is used from the same process.

For your purposes SystemTime should be effectively monotonic, except for the instant every six months (at YYYY.30.06 24:00 and YYYY.31.12 24:00) when a leap-second could be added to account for the earth's slowing in its orbit.

If a daylight-time/summer-time correction is applied to SystemTime during your measurements, which is a ±3600 s shift that occurs twice a year, such a large discontinuity will be readily apparent when computing round-trip times whose upper bound is at most a few seconds.

1 Like