From a0a204f586301ee4e014c3673c2ecd3ad90c504c Mon Sep 17 00:00:00 2001 From: Rogan Morrow Date: Mon, 22 Sep 2025 13:36:22 +1000 Subject: [PATCH 1/3] add as_nanos and from_nanos where missing --- embassy-time/src/duration.rs | 5 +++++ embassy-time/src/instant.rs | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 5b140eeff..b3ea0468d 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs @@ -37,6 +37,11 @@ impl Duration { self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) } + /// Convert the `Duration` to nanoseconds, rounding down. + pub const fn as_nanos(&self) -> u64 { + self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G) + } + /// Creates a duration from the specified number of clock ticks pub const fn from_ticks(ticks: u64) -> Duration { Duration { ticks } diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index 6571bea62..a311b365c 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs @@ -1,7 +1,7 @@ use core::fmt; use core::ops::{Add, AddAssign, Sub, SubAssign}; -use super::{Duration, GCD_1K, GCD_1M, TICK_HZ}; +use super::{Duration, GCD_1K, GCD_1M, GCD_1G, TICK_HZ}; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -29,6 +29,13 @@ impl Instant { Self { ticks } } + /// Create an Instant from a nanosecond count since system boot. + pub const fn from_nanos(nanos: u64) -> Self { + Self { + ticks: nanos * (TICK_HZ / GCD_1G) / (1_000_000_000 / GCD_1G), + } + } + /// Create an Instant from a microsecond count since system boot. pub const fn from_micros(micros: u64) -> Self { Self { @@ -101,6 +108,11 @@ impl Instant { self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) } + /// Nanoseconds since system boot. + pub const fn as_nanos(&self) -> u64 { + self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G) + } + /// Duration between this Instant and another Instant /// Panics on over/underflow. pub fn duration_since(&self, earlier: Instant) -> Duration { From 57f1517f70bafb0709014f7a44e2d1c8a4841739 Mon Sep 17 00:00:00 2001 From: Rogan Morrow Date: Mon, 22 Sep 2025 13:43:57 +1000 Subject: [PATCH 2/3] fix rustfmt and add changelog --- embassy-time/CHANGELOG.md | 2 ++ embassy-time/src/instant.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/embassy-time/CHANGELOG.md b/embassy-time/CHANGELOG.md index 572571215..4a50da8ef 100644 --- a/embassy-time/CHANGELOG.md +++ b/embassy-time/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased - ReleaseDate +- Add as_nanos and from_nanos where missing + ## 0.5.0 - 2025-08-26 - Allow inlining on time driver boundary diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index a311b365c..4e6670032 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs @@ -1,7 +1,7 @@ use core::fmt; use core::ops::{Add, AddAssign, Sub, SubAssign}; -use super::{Duration, GCD_1K, GCD_1M, GCD_1G, TICK_HZ}; +use super::{Duration, GCD_1G, GCD_1K, GCD_1M, TICK_HZ}; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] From 4d9563805cee8a14f7c59a5b28227b99a6ffbf75 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 22 Sep 2025 12:55:20 +0200 Subject: [PATCH 3/3] 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 {