1*13776c11Slogin use core::{fmt, ops}; 2*13776c11Slogin 3*13776c11Slogin use self::timekeep::ktime_get_real_ns; 4*13776c11Slogin 5bacd691cSlogin pub mod sleep; 601876902SGou Ngai pub mod timekeep; 7bacd691cSlogin pub mod timer; 8004e86ffSlogin 9*13776c11Slogin /* Time structures. (Partitially taken from smoltcp) 10*13776c11Slogin 11*13776c11Slogin The `time` module contains structures used to represent both 12*13776c11Slogin absolute and relative time. 13*13776c11Slogin 14*13776c11Slogin - [Instant] is used to represent absolute time. 15*13776c11Slogin - [Duration] is used to represent relative time. 16*13776c11Slogin 17*13776c11Slogin [Instant]: struct.Instant.html 18*13776c11Slogin [Duration]: struct.Duration.html 19*13776c11Slogin */ 20*13776c11Slogin 21004e86ffSlogin /// 表示时间的结构体 22004e86ffSlogin #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] 23004e86ffSlogin pub struct TimeSpec { 24004e86ffSlogin pub tv_sec: i64, 25004e86ffSlogin pub tv_nsec: i64, 26004e86ffSlogin } 2720e3152eSlogin 2820e3152eSlogin impl TimeSpec { 29*13776c11Slogin #[allow(dead_code)] 3020e3152eSlogin pub fn new(sec: i64, nsec: i64) -> TimeSpec { 3120e3152eSlogin return TimeSpec { 3220e3152eSlogin tv_sec: sec, 3320e3152eSlogin tv_nsec: nsec, 3420e3152eSlogin }; 3520e3152eSlogin } 3620e3152eSlogin } 37*13776c11Slogin 38*13776c11Slogin /// A representation of an absolute time value. 39*13776c11Slogin /// 40*13776c11Slogin /// The `Instant` type is a wrapper around a `i64` value that 41*13776c11Slogin /// represents a number of microseconds, monotonically increasing 42*13776c11Slogin /// since an arbitrary moment in time, such as system startup. 43*13776c11Slogin /// 44*13776c11Slogin /// * A value of `0` is inherently arbitrary. 45*13776c11Slogin /// * A value less than `0` indicates a time before the starting 46*13776c11Slogin /// point. 47*13776c11Slogin #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 48*13776c11Slogin #[cfg_attr(feature = "defmt", derive(defmt::Format))] 49*13776c11Slogin pub struct Instant { 50*13776c11Slogin micros: i64, 51*13776c11Slogin } 52*13776c11Slogin 53*13776c11Slogin #[allow(dead_code)] 54*13776c11Slogin impl Instant { 55*13776c11Slogin pub const ZERO: Instant = Instant::from_micros_const(0); 56*13776c11Slogin 57*13776c11Slogin /// Create a new `Instant` from a number of microseconds. 58*13776c11Slogin pub fn from_micros<T: Into<i64>>(micros: T) -> Instant { 59*13776c11Slogin Instant { 60*13776c11Slogin micros: micros.into(), 61*13776c11Slogin } 62*13776c11Slogin } 63*13776c11Slogin 64*13776c11Slogin pub const fn from_micros_const(micros: i64) -> Instant { 65*13776c11Slogin Instant { micros } 66*13776c11Slogin } 67*13776c11Slogin 68*13776c11Slogin /// Create a new `Instant` from a number of milliseconds. 69*13776c11Slogin pub fn from_millis<T: Into<i64>>(millis: T) -> Instant { 70*13776c11Slogin Instant { 71*13776c11Slogin micros: millis.into() * 1000, 72*13776c11Slogin } 73*13776c11Slogin } 74*13776c11Slogin 75*13776c11Slogin /// Create a new `Instant` from a number of milliseconds. 76*13776c11Slogin pub const fn from_millis_const(millis: i64) -> Instant { 77*13776c11Slogin Instant { 78*13776c11Slogin micros: millis * 1000, 79*13776c11Slogin } 80*13776c11Slogin } 81*13776c11Slogin 82*13776c11Slogin /// Create a new `Instant` from a number of seconds. 83*13776c11Slogin pub fn from_secs<T: Into<i64>>(secs: T) -> Instant { 84*13776c11Slogin Instant { 85*13776c11Slogin micros: secs.into() * 1000000, 86*13776c11Slogin } 87*13776c11Slogin } 88*13776c11Slogin 89*13776c11Slogin /// Create a new `Instant` from the current time 90*13776c11Slogin pub fn now() -> Instant { 91*13776c11Slogin Self::from_micros(ktime_get_real_ns() / 1000) 92*13776c11Slogin } 93*13776c11Slogin 94*13776c11Slogin /// The fractional number of milliseconds that have passed 95*13776c11Slogin /// since the beginning of time. 96*13776c11Slogin pub const fn millis(&self) -> i64 { 97*13776c11Slogin self.micros % 1000000 / 1000 98*13776c11Slogin } 99*13776c11Slogin 100*13776c11Slogin /// The fractional number of microseconds that have passed 101*13776c11Slogin /// since the beginning of time. 102*13776c11Slogin pub const fn micros(&self) -> i64 { 103*13776c11Slogin self.micros % 1000000 104*13776c11Slogin } 105*13776c11Slogin 106*13776c11Slogin /// The number of whole seconds that have passed since the 107*13776c11Slogin /// beginning of time. 108*13776c11Slogin pub const fn secs(&self) -> i64 { 109*13776c11Slogin self.micros / 1000000 110*13776c11Slogin } 111*13776c11Slogin 112*13776c11Slogin /// The total number of milliseconds that have passed since 113*13776c11Slogin /// the beginning of time. 114*13776c11Slogin pub const fn total_millis(&self) -> i64 { 115*13776c11Slogin self.micros / 1000 116*13776c11Slogin } 117*13776c11Slogin /// The total number of milliseconds that have passed since 118*13776c11Slogin /// the beginning of time. 119*13776c11Slogin pub const fn total_micros(&self) -> i64 { 120*13776c11Slogin self.micros 121*13776c11Slogin } 122*13776c11Slogin } 123*13776c11Slogin 124*13776c11Slogin impl fmt::Display for Instant { 125*13776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 126*13776c11Slogin write!(f, "{}.{:0>3}s", self.secs(), self.millis()) 127*13776c11Slogin } 128*13776c11Slogin } 129*13776c11Slogin 130*13776c11Slogin impl ops::Add<Duration> for Instant { 131*13776c11Slogin type Output = Instant; 132*13776c11Slogin 133*13776c11Slogin fn add(self, rhs: Duration) -> Instant { 134*13776c11Slogin Instant::from_micros(self.micros + rhs.total_micros() as i64) 135*13776c11Slogin } 136*13776c11Slogin } 137*13776c11Slogin 138*13776c11Slogin impl ops::AddAssign<Duration> for Instant { 139*13776c11Slogin fn add_assign(&mut self, rhs: Duration) { 140*13776c11Slogin self.micros += rhs.total_micros() as i64; 141*13776c11Slogin } 142*13776c11Slogin } 143*13776c11Slogin 144*13776c11Slogin impl ops::Sub<Duration> for Instant { 145*13776c11Slogin type Output = Instant; 146*13776c11Slogin 147*13776c11Slogin fn sub(self, rhs: Duration) -> Instant { 148*13776c11Slogin Instant::from_micros(self.micros - rhs.total_micros() as i64) 149*13776c11Slogin } 150*13776c11Slogin } 151*13776c11Slogin 152*13776c11Slogin impl ops::SubAssign<Duration> for Instant { 153*13776c11Slogin fn sub_assign(&mut self, rhs: Duration) { 154*13776c11Slogin self.micros -= rhs.total_micros() as i64; 155*13776c11Slogin } 156*13776c11Slogin } 157*13776c11Slogin 158*13776c11Slogin impl ops::Sub<Instant> for Instant { 159*13776c11Slogin type Output = Duration; 160*13776c11Slogin 161*13776c11Slogin fn sub(self, rhs: Instant) -> Duration { 162*13776c11Slogin Duration::from_micros((self.micros - rhs.micros).unsigned_abs()) 163*13776c11Slogin } 164*13776c11Slogin } 165*13776c11Slogin 166*13776c11Slogin /// A relative amount of time. 167*13776c11Slogin #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 168*13776c11Slogin #[cfg_attr(feature = "defmt", derive(defmt::Format))] 169*13776c11Slogin pub struct Duration { 170*13776c11Slogin micros: u64, 171*13776c11Slogin } 172*13776c11Slogin 173*13776c11Slogin impl Duration { 174*13776c11Slogin pub const ZERO: Duration = Duration::from_micros(0); 175*13776c11Slogin /// Create a new `Duration` from a number of microseconds. 176*13776c11Slogin pub const fn from_micros(micros: u64) -> Duration { 177*13776c11Slogin Duration { micros } 178*13776c11Slogin } 179*13776c11Slogin 180*13776c11Slogin /// Create a new `Duration` from a number of milliseconds. 181*13776c11Slogin pub const fn from_millis(millis: u64) -> Duration { 182*13776c11Slogin Duration { 183*13776c11Slogin micros: millis * 1000, 184*13776c11Slogin } 185*13776c11Slogin } 186*13776c11Slogin 187*13776c11Slogin /// Create a new `Instant` from a number of seconds. 188*13776c11Slogin pub const fn from_secs(secs: u64) -> Duration { 189*13776c11Slogin Duration { 190*13776c11Slogin micros: secs * 1000000, 191*13776c11Slogin } 192*13776c11Slogin } 193*13776c11Slogin 194*13776c11Slogin /// The fractional number of milliseconds in this `Duration`. 195*13776c11Slogin pub const fn millis(&self) -> u64 { 196*13776c11Slogin self.micros / 1000 % 1000 197*13776c11Slogin } 198*13776c11Slogin 199*13776c11Slogin /// The fractional number of milliseconds in this `Duration`. 200*13776c11Slogin pub const fn micros(&self) -> u64 { 201*13776c11Slogin self.micros % 1000000 202*13776c11Slogin } 203*13776c11Slogin 204*13776c11Slogin /// The number of whole seconds in this `Duration`. 205*13776c11Slogin pub const fn secs(&self) -> u64 { 206*13776c11Slogin self.micros / 1000000 207*13776c11Slogin } 208*13776c11Slogin 209*13776c11Slogin /// The total number of milliseconds in this `Duration`. 210*13776c11Slogin pub const fn total_millis(&self) -> u64 { 211*13776c11Slogin self.micros / 1000 212*13776c11Slogin } 213*13776c11Slogin 214*13776c11Slogin /// The total number of microseconds in this `Duration`. 215*13776c11Slogin pub const fn total_micros(&self) -> u64 { 216*13776c11Slogin self.micros 217*13776c11Slogin } 218*13776c11Slogin } 219*13776c11Slogin 220*13776c11Slogin impl fmt::Display for Duration { 221*13776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 222*13776c11Slogin write!(f, "{}.{:03}s", self.secs(), self.millis()) 223*13776c11Slogin } 224*13776c11Slogin } 225*13776c11Slogin 226*13776c11Slogin impl ops::Add<Duration> for Duration { 227*13776c11Slogin type Output = Duration; 228*13776c11Slogin 229*13776c11Slogin fn add(self, rhs: Duration) -> Duration { 230*13776c11Slogin Duration::from_micros(self.micros + rhs.total_micros()) 231*13776c11Slogin } 232*13776c11Slogin } 233*13776c11Slogin 234*13776c11Slogin impl ops::AddAssign<Duration> for Duration { 235*13776c11Slogin fn add_assign(&mut self, rhs: Duration) { 236*13776c11Slogin self.micros += rhs.total_micros(); 237*13776c11Slogin } 238*13776c11Slogin } 239*13776c11Slogin 240*13776c11Slogin impl ops::Sub<Duration> for Duration { 241*13776c11Slogin type Output = Duration; 242*13776c11Slogin 243*13776c11Slogin fn sub(self, rhs: Duration) -> Duration { 244*13776c11Slogin Duration::from_micros( 245*13776c11Slogin self.micros 246*13776c11Slogin .checked_sub(rhs.total_micros()) 247*13776c11Slogin .expect("overflow when subtracting durations"), 248*13776c11Slogin ) 249*13776c11Slogin } 250*13776c11Slogin } 251*13776c11Slogin 252*13776c11Slogin impl ops::SubAssign<Duration> for Duration { 253*13776c11Slogin fn sub_assign(&mut self, rhs: Duration) { 254*13776c11Slogin self.micros = self 255*13776c11Slogin .micros 256*13776c11Slogin .checked_sub(rhs.total_micros()) 257*13776c11Slogin .expect("overflow when subtracting durations"); 258*13776c11Slogin } 259*13776c11Slogin } 260*13776c11Slogin 261*13776c11Slogin impl ops::Mul<u32> for Duration { 262*13776c11Slogin type Output = Duration; 263*13776c11Slogin 264*13776c11Slogin fn mul(self, rhs: u32) -> Duration { 265*13776c11Slogin Duration::from_micros(self.micros * rhs as u64) 266*13776c11Slogin } 267*13776c11Slogin } 268*13776c11Slogin 269*13776c11Slogin impl ops::MulAssign<u32> for Duration { 270*13776c11Slogin fn mul_assign(&mut self, rhs: u32) { 271*13776c11Slogin self.micros *= rhs as u64; 272*13776c11Slogin } 273*13776c11Slogin } 274*13776c11Slogin 275*13776c11Slogin impl ops::Div<u32> for Duration { 276*13776c11Slogin type Output = Duration; 277*13776c11Slogin 278*13776c11Slogin fn div(self, rhs: u32) -> Duration { 279*13776c11Slogin Duration::from_micros(self.micros / rhs as u64) 280*13776c11Slogin } 281*13776c11Slogin } 282*13776c11Slogin 283*13776c11Slogin impl ops::DivAssign<u32> for Duration { 284*13776c11Slogin fn div_assign(&mut self, rhs: u32) { 285*13776c11Slogin self.micros /= rhs as u64; 286*13776c11Slogin } 287*13776c11Slogin } 288*13776c11Slogin 289*13776c11Slogin impl ops::Shl<u32> for Duration { 290*13776c11Slogin type Output = Duration; 291*13776c11Slogin 292*13776c11Slogin fn shl(self, rhs: u32) -> Duration { 293*13776c11Slogin Duration::from_micros(self.micros << rhs) 294*13776c11Slogin } 295*13776c11Slogin } 296*13776c11Slogin 297*13776c11Slogin impl ops::ShlAssign<u32> for Duration { 298*13776c11Slogin fn shl_assign(&mut self, rhs: u32) { 299*13776c11Slogin self.micros <<= rhs; 300*13776c11Slogin } 301*13776c11Slogin } 302*13776c11Slogin 303*13776c11Slogin impl ops::Shr<u32> for Duration { 304*13776c11Slogin type Output = Duration; 305*13776c11Slogin 306*13776c11Slogin fn shr(self, rhs: u32) -> Duration { 307*13776c11Slogin Duration::from_micros(self.micros >> rhs) 308*13776c11Slogin } 309*13776c11Slogin } 310*13776c11Slogin 311*13776c11Slogin impl ops::ShrAssign<u32> for Duration { 312*13776c11Slogin fn shr_assign(&mut self, rhs: u32) { 313*13776c11Slogin self.micros >>= rhs; 314*13776c11Slogin } 315*13776c11Slogin } 316*13776c11Slogin 317*13776c11Slogin impl From<::core::time::Duration> for Duration { 318*13776c11Slogin fn from(other: ::core::time::Duration) -> Duration { 319*13776c11Slogin Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64) 320*13776c11Slogin } 321*13776c11Slogin } 322*13776c11Slogin 323*13776c11Slogin impl From<Duration> for ::core::time::Duration { 324*13776c11Slogin fn from(val: Duration) -> Self { 325*13776c11Slogin ::core::time::Duration::from_micros(val.total_micros()) 326*13776c11Slogin } 327*13776c11Slogin } 328*13776c11Slogin 329*13776c11Slogin /// 支持与smoltcp的时间转换 330*13776c11Slogin impl From<smoltcp::time::Instant> for Instant { 331*13776c11Slogin fn from(val: smoltcp::time::Instant) -> Self { 332*13776c11Slogin Instant::from_micros(val.micros()) 333*13776c11Slogin } 334*13776c11Slogin } 335*13776c11Slogin 336*13776c11Slogin impl Into<smoltcp::time::Instant> for Instant { 337*13776c11Slogin fn into(self) -> smoltcp::time::Instant { 338*13776c11Slogin smoltcp::time::Instant::from_millis(self.millis()) 339*13776c11Slogin } 340*13776c11Slogin } 341*13776c11Slogin 342*13776c11Slogin /// 支持与smoltcp的时间转换 343*13776c11Slogin impl From<smoltcp::time::Duration> for Duration { 344*13776c11Slogin fn from(val: smoltcp::time::Duration) -> Self { 345*13776c11Slogin Duration::from_micros(val.micros()) 346*13776c11Slogin } 347*13776c11Slogin } 348*13776c11Slogin 349*13776c11Slogin impl Into<smoltcp::time::Duration> for Duration { 350*13776c11Slogin fn into(self) -> smoltcp::time::Duration { 351*13776c11Slogin smoltcp::time::Duration::from_millis(self.millis()) 352*13776c11Slogin } 353*13776c11Slogin } 354