Skip to content

Commit 4d95638

Browse files
committed
time: add Instant::try_from_nanos
1 parent 57f1517 commit 4d95638

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

embassy-time/src/instant.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ impl Instant {
5757
}
5858
}
5959

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+
6071
/// Try to create an Instant from a microsecond count since system boot.
6172
/// Fails if the number of microseconds is too large.
6273
pub const fn try_from_micros(micros: u64) -> Option<Self> {

0 commit comments

Comments
 (0)