aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src/instant.rs
diff options
context:
space:
mode:
authorRogan Morrow <[email protected]>2025-09-22 13:36:22 +1000
committerRogan Morrow <[email protected]>2025-09-22 13:36:22 +1000
commita0a204f586301ee4e014c3673c2ecd3ad90c504c (patch)
tree520ba0ab08f0512c958d9342e4388d37f26b68c6 /embassy-time/src/instant.rs
parent539837a7485381f83ef078595a4e248a0ea11436 (diff)
add as_nanos and from_nanos where missing
Diffstat (limited to 'embassy-time/src/instant.rs')
-rw-r--r--embassy-time/src/instant.rs14
1 files changed, 13 insertions, 1 deletions
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 @@
1use core::fmt; 1use core::fmt;
2use core::ops::{Add, AddAssign, Sub, SubAssign}; 2use core::ops::{Add, AddAssign, Sub, SubAssign};
3 3
4use super::{Duration, GCD_1K, GCD_1M, TICK_HZ}; 4use super::{Duration, GCD_1K, GCD_1M, GCD_1G, TICK_HZ};
5 5
6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))] 7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -29,6 +29,13 @@ impl Instant {
29 Self { ticks } 29 Self { ticks }
30 } 30 }
31 31
32 /// Create an Instant from a nanosecond count since system boot.
33 pub const fn from_nanos(nanos: u64) -> Self {
34 Self {
35 ticks: nanos * (TICK_HZ / GCD_1G) / (1_000_000_000 / GCD_1G),
36 }
37 }
38
32 /// Create an Instant from a microsecond count since system boot. 39 /// Create an Instant from a microsecond count since system boot.
33 pub const fn from_micros(micros: u64) -> Self { 40 pub const fn from_micros(micros: u64) -> Self {
34 Self { 41 Self {
@@ -101,6 +108,11 @@ impl Instant {
101 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) 108 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
102 } 109 }
103 110
111 /// Nanoseconds since system boot.
112 pub const fn as_nanos(&self) -> u64 {
113 self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G)
114 }
115
104 /// Duration between this Instant and another Instant 116 /// Duration between this Instant and another Instant
105 /// Panics on over/underflow. 117 /// Panics on over/underflow.
106 pub fn duration_since(&self, earlier: Instant) -> Duration { 118 pub fn duration_since(&self, earlier: Instant) -> Duration {