1 use crate::cmp::Ordering;
3 use crate::sys::{cvt, syscall};
4 use crate::time::Duration;
5 use crate::convert::TryInto;
7 use core::hash::{Hash, Hasher};
9 const NSEC_PER_SEC: u64 = 1_000_000_000;
11 #[derive(Copy, Clone)]
17 fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
19 Ok(if self.t.tv_nsec >= other.t.tv_nsec {
20 Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
21 (self.t.tv_nsec - other.t.tv_nsec) as u32)
23 Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
24 self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) -
25 other.t.tv_nsec as u32)
28 match other.sub_timespec(self) {
35 fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
38 .try_into() // <- target type would be `i64`
40 .and_then(|secs| self.t.tv_sec.checked_add(secs))?;
42 // Nano calculations can't overflow because nanos are <1B which fit
44 let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
45 if nsec >= NSEC_PER_SEC as u32 {
46 nsec -= NSEC_PER_SEC as u32;
47 secs = secs.checked_add(1)?;
50 t: syscall::TimeSpec {
57 fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
60 .try_into() // <- target type would be `i64`
62 .and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
64 // Similar to above, nanos can't overflow.
65 let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
67 nsec += NSEC_PER_SEC as i32;
68 secs = secs.checked_sub(1)?;
71 t: syscall::TimeSpec {
79 impl PartialEq for Timespec {
80 fn eq(&self, other: &Timespec) -> bool {
81 self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
85 impl Eq for Timespec {}
87 impl PartialOrd for Timespec {
88 fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
93 impl Ord for Timespec {
94 fn cmp(&self, other: &Timespec) -> Ordering {
95 let me = (self.t.tv_sec, self.t.tv_nsec);
96 let other = (other.t.tv_sec, other.t.tv_nsec);
101 impl Hash for Timespec {
102 fn hash<H : Hasher>(&self, state: &mut H) {
103 self.t.tv_sec.hash(state);
104 self.t.tv_nsec.hash(state);
108 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
113 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
114 pub struct SystemTime {
118 pub const UNIX_EPOCH: SystemTime = SystemTime {
120 t: syscall::TimeSpec {
128 pub fn now() -> Instant {
129 Instant { t: now(syscall::CLOCK_MONOTONIC) }
132 pub const fn zero() -> Instant {
133 Instant { t: Timespec { t: syscall::TimeSpec { tv_sec: 0, tv_nsec: 0 } } }
136 pub fn actually_monotonic() -> bool {
140 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
141 self.t.sub_timespec(&other.t).ok()
144 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
145 Some(Instant { t: self.t.checked_add_duration(other)? })
148 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
149 Some(Instant { t: self.t.checked_sub_duration(other)? })
153 impl fmt::Debug for Instant {
154 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
155 f.debug_struct("Instant")
156 .field("tv_sec", &self.t.t.tv_sec)
157 .field("tv_nsec", &self.t.t.tv_nsec)
163 pub fn now() -> SystemTime {
164 SystemTime { t: now(syscall::CLOCK_REALTIME) }
167 pub fn sub_time(&self, other: &SystemTime)
168 -> Result<Duration, Duration> {
169 self.t.sub_timespec(&other.t)
172 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
173 Some(SystemTime { t: self.t.checked_add_duration(other)? })
176 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
177 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
181 impl From<syscall::TimeSpec> for SystemTime {
182 fn from(t: syscall::TimeSpec) -> SystemTime {
183 SystemTime { t: Timespec { t } }
187 impl fmt::Debug for SystemTime {
188 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189 f.debug_struct("SystemTime")
190 .field("tv_sec", &self.t.t.tv_sec)
191 .field("tv_nsec", &self.t.t.tv_nsec)
196 pub type clock_t = usize;
198 fn now(clock: clock_t) -> Timespec {
199 let mut t = Timespec {
200 t: syscall::TimeSpec {
205 cvt(syscall::clock_gettime(clock, &mut t.t)).unwrap();