From 4d9563805cee8a14f7c59a5b28227b99a6ffbf75 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 22 Sep 2025 12:55:20 +0200 Subject: [PATCH] time: add Instant::try_from_nanos --- embassy-time/src/instant.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index 4e6670032..de5ebebf8 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs @@ -57,6 +57,17 @@ impl Instant { } } + /// Try to create an Instant from a nanosecond count since system boot. + /// Fails if the number of nanoseconds is too large. + pub const fn try_from_nanos(nanos: u64) -> Option { + let Some(value) = nanos.checked_mul(TICK_HZ / GCD_1G) else { + return None; + }; + Some(Self { + ticks: value / (1_000_000_000 / GCD_1G), + }) + } + /// Try to create an Instant from a microsecond count since system boot. /// Fails if the number of microseconds is too large. pub const fn try_from_micros(micros: u64) -> Option {