aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-09-22 12:55:20 +0200
committerDario Nieuwenhuis <[email protected]>2025-09-22 12:55:20 +0200
commit4d9563805cee8a14f7c59a5b28227b99a6ffbf75 (patch)
treee4a8bb1b8658e70f1d8c2df18b527090ae2d0a0a
parent57f1517f70bafb0709014f7a44e2d1c8a4841739 (diff)
time: add Instant::try_from_nanos
-rw-r--r--embassy-time/src/instant.rs11
1 files changed, 11 insertions, 0 deletions
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 {
57 } 57 }
58 } 58 }
59 59
60 /// Try to create an Instant from a nanosecond count since system boot.
61 /// Fails if the number of nanoseconds is too large.
62 pub const fn try_from_nanos(nanos: u64) -> Option<Self> {
63 let Some(value) = nanos.checked_mul(TICK_HZ / GCD_1G) else {
64 return None;
65 };
66 Some(Self {
67 ticks: value / (1_000_000_000 / GCD_1G),
68 })
69 }
70
60 /// Try to create an Instant from a microsecond count since system boot. 71 /// Try to create an Instant from a microsecond count since system boot.
61 /// Fails if the number of microseconds is too large. 72 /// Fails if the number of microseconds is too large.
62 pub const fn try_from_micros(micros: u64) -> Option<Self> { 73 pub const fn try_from_micros(micros: u64) -> Option<Self> {