2017-10-22 20:01:00 -07:00
|
|
|
use time::Duration;
|
2017-12-31 16:40:34 +00:00
|
|
|
use sys::{TimeSysCall, TimeClock};
|
2017-10-22 20:01:00 -07:00
|
|
|
|
2017-12-19 00:35:43 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
2017-12-31 16:40:34 +00:00
|
|
|
pub struct Instant(Duration);
|
2017-10-22 20:01:00 -07:00
|
|
|
|
2017-12-31 16:40:34 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
|
|
|
|
pub struct SystemTime(Duration);
|
2017-10-22 20:01:00 -07:00
|
|
|
|
2017-12-31 16:40:34 +00:00
|
|
|
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
|
2017-10-22 20:01:00 -07:00
|
|
|
|
|
|
|
|
impl Instant {
|
|
|
|
|
pub fn now() -> Instant {
|
2017-12-31 16:40:34 +00:00
|
|
|
Instant(TimeSysCall::perform(TimeClock::Monotonic))
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-19 10:29:23 -08:00
|
|
|
pub const fn zero() -> Instant {
|
|
|
|
|
Instant(Duration::from_secs(0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn actually_monotonic() -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:40:34 +00:00
|
|
|
pub fn sub_instant(&self, other: &Instant) -> Duration {
|
|
|
|
|
self.0 - other.0
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-04 02:29:57 +01:00
|
|
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
2018-12-10 23:55:53 +01:00
|
|
|
Some(Instant(self.0.checked_add(*other)?))
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 23:55:53 +01:00
|
|
|
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
|
|
|
|
|
Some(Instant(self.0.checked_sub(*other)?))
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SystemTime {
|
|
|
|
|
pub fn now() -> SystemTime {
|
2017-12-31 16:40:34 +00:00
|
|
|
SystemTime(TimeSysCall::perform(TimeClock::System))
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:40:34 +00:00
|
|
|
pub fn sub_time(&self, other: &SystemTime)
|
2017-10-22 20:01:00 -07:00
|
|
|
-> Result<Duration, Duration> {
|
2017-12-31 16:40:34 +00:00
|
|
|
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-30 22:24:33 -07:00
|
|
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
2018-12-10 23:55:53 +01:00
|
|
|
Some(SystemTime(self.0.checked_add(*other)?))
|
2018-10-30 22:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 23:55:53 +01:00
|
|
|
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
|
|
|
|
Some(SystemTime(self.0.checked_sub(*other)?))
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
}
|