Add {from_as}_micros to Instant

This commit is contained in:
Daniel Franklin 2022-02-11 19:01:43 -07:00
parent 4c5f5f7169
commit 04ac488319
2 changed files with 13 additions and 2 deletions

View File

@ -196,8 +196,7 @@ macro_rules! unwrap {
} }
#[cfg(feature = "defmt-timestamp-uptime")] #[cfg(feature = "defmt-timestamp-uptime")]
// defmt offers a disply hint for microseconds so we convert from millis defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_micros() }
defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_millis() * 1_000 }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct NoneError; pub struct NoneError;

View File

@ -28,6 +28,13 @@ impl Instant {
Self { ticks } Self { ticks }
} }
/// Create an Instant from a microsecond count since system boot.
pub const fn from_micros(micros: u64) -> Self {
Self {
ticks: micros * TICKS_PER_SECOND / 1_000_000,
}
}
/// Create an Instant from a millisecond count since system boot. /// Create an Instant from a millisecond count since system boot.
pub const fn from_millis(millis: u64) -> Self { pub const fn from_millis(millis: u64) -> Self {
Self { Self {
@ -57,6 +64,11 @@ impl Instant {
self.ticks * 1000 / TICKS_PER_SECOND self.ticks * 1000 / TICKS_PER_SECOND
} }
/// Microseconds since system boot.
pub const fn as_micros(&self) -> u64 {
self.ticks * 1_000_000 / TICKS_PER_SECOND
}
/// Duration between this Instant and another Instant /// Duration between this Instant and another Instant
/// Panics on over/underflow. /// Panics on over/underflow.
pub fn duration_since(&self, earlier: Instant) -> Duration { pub fn duration_since(&self, earlier: Instant) -> Duration {