Skip to content

Commit de5dd10

Browse files
authored
Merge pull request #4695 from meowcakes/time-as-nanos
Add `as_nanos` and `from_nanos` where missing in embassy-time
2 parents a25c5c4 + 4d95638 commit de5dd10

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

embassy-time/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
<!-- next-header -->
99
## Unreleased - ReleaseDate
1010

11+
- Add as_nanos and from_nanos where missing
12+
1113
## 0.5.0 - 2025-08-26
1214

1315
- Allow inlining on time driver boundary

embassy-time/src/duration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ impl Duration {
3737
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
3838
}
3939

40+
/// Convert the `Duration` to nanoseconds, rounding down.
41+
pub const fn as_nanos(&self) -> u64 {
42+
self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G)
43+
}
44+
4045
/// Creates a duration from the specified number of clock ticks
4146
pub const fn from_ticks(ticks: u64) -> Duration {
4247
Duration { ticks }

embassy-time/src/instant.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::fmt;
22
use core::ops::{Add, AddAssign, Sub, SubAssign};
33

4-
use super::{Duration, GCD_1K, GCD_1M, TICK_HZ};
4+
use super::{Duration, GCD_1G, GCD_1K, GCD_1M, TICK_HZ};
55

66
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
77
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -29,6 +29,13 @@ impl Instant {
2929
Self { ticks }
3030
}
3131

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+
3239
/// Create an Instant from a microsecond count since system boot.
3340
pub const fn from_micros(micros: u64) -> Self {
3441
Self {
@@ -50,6 +57,17 @@ impl Instant {
5057
}
5158
}
5259

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+
5371
/// Try to create an Instant from a microsecond count since system boot.
5472
/// Fails if the number of microseconds is too large.
5573
pub const fn try_from_micros(micros: u64) -> Option<Self> {
@@ -101,6 +119,11 @@ impl Instant {
101119
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
102120
}
103121

122+
/// Nanoseconds since system boot.
123+
pub const fn as_nanos(&self) -> u64 {
124+
self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G)
125+
}
126+
104127
/// Duration between this Instant and another Instant
105128
/// Panics on over/underflow.
106129
pub fn duration_since(&self, earlier: Instant) -> Duration {

0 commit comments

Comments
 (0)