Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Type-safe timestamps

11 views
Skip to first unread message

Adam Perry

unread,
Aug 22, 2024, 6:58:25 PM8/22/24
to rust-...@fuchsia.dev
TLDR: Use zx::MonotonicTime::get() instead of zx::Time::get_monotonic(). Use from_nanos()/into_nanos() if you need to convert between timelines.

Hi all! To prepare for the upcoming boot time RFC I've been working to make it harder for Rust programs to confuse timestamps from the monotonic & boot clocks.

I just landed a change which makes timestamps generic over the source timeline after migrating all of our code to use type aliases for the different kinds of timestamps. As of now, you'll need to reference zx::MonotonicTime for the most common use cases, and zx::SyntheticTime if you're reading from a custom kernel clock. Comparing or doing math on timestamps from different timelines will now produce a compiler error.

I've already migrated all the cases where a zx::Time was compared across timelines, but if you need to do that yourself going forward, you can use the raw nanoseconds representation from zx::Time::into_nanos() for the comparison or arithmetic. You can put the resulting value back into the "right timeline" with zx::Time::<TimelineMarkerType>::from_nanos().

I've also added a shorter function name for retrieving the current time so that "monotonic" isn't repeated unnecessarily: zx::MonotonicTime::get(). The new function is already available and migration will be complete with this change.

Stay tuned for a follow-up regarding UTC and the boot time clock and in the meantime happy hacking!

Adam Perry
Reply all
Reply to author
Forward
0 new messages