Skip to main content

std\sys\time/
windows.rs

1use crate::cmp::Ordering;
2use crate::hash::{Hash, Hasher};
3use crate::sys::helpers::mul_div_u64;
4use crate::sys::pal::time::{
5    INTERVALS_PER_SEC, checked_dur2intervals, intervals2dur, perf_counter,
6};
7use crate::sys::{IntoInner, c};
8use crate::time::Duration;
9use crate::{fmt, mem};
10
11const NANOS_PER_SEC: u64 = 1_000_000_000;
12
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
14pub struct Instant {
15    // This duration is relative to an arbitrary microsecond epoch
16    // from the winapi QueryPerformanceCounter function.
17    t: Duration,
18}
19
20#[derive(Copy, Clone)]
21pub struct SystemTime {
22    t: c::FILETIME,
23}
24
25pub const UNIX_EPOCH: SystemTime =
26    SystemTime::from_intervals(11_644_473_600 * INTERVALS_PER_SEC as i64);
27
28impl Instant {
29    pub fn now() -> Instant {
30        // High precision timing on windows operates in "Performance Counter"
31        // units, as returned by the WINAPI QueryPerformanceCounter function.
32        // These relate to seconds by a factor of QueryPerformanceFrequency.
33        // In order to keep unit conversions out of normal interval math, we
34        // measure in QPC units and immediately convert to nanoseconds.
35
36        let freq = perf_counter::frequency() as u64;
37        let now = perf_counter::now();
38
39        // We convert now to `u64` to be able to use `Duration`.
40        let instant_nsec = mul_div_u64(now as u64, NANOS_PER_SEC, freq);
41        // We can add an arbitrary offset to shift the epoch of this clock. We do that to avoid
42        // being too close to 0 which would lead to underflow when computing times in the past. Also
43        // see <https://github.com/rust-lang/rust/issues/156142>.
44        let instant_nsec = instant_nsec + (u64::MAX / 4);
45
46        Self { t: Duration::from_nanos(instant_nsec) }
47    }
48
49    pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
50        // On windows there's a threshold below which we consider two timestamps
51        // equivalent due to measurement error. For more details + doc link,
52        // check the docs on epsilon.
53        let epsilon = perf_counter::epsilon();
54        if other.t > self.t && other.t - self.t <= epsilon {
55            Some(Duration::new(0, 0))
56        } else {
57            self.t.checked_sub(other.t)
58        }
59    }
60
61    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
62        Some(Instant { t: self.t.checked_add(*other)? })
63    }
64
65    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
66        Some(Instant { t: self.t.checked_sub(*other)? })
67    }
68}
69
70impl SystemTime {
71    pub const MAX: SystemTime = SystemTime::from_intervals(i64::MAX);
72    pub const MIN: SystemTime = SystemTime::from_intervals(0);
73
74    pub fn now() -> SystemTime {
75        unsafe {
76            let mut t: SystemTime = mem::zeroed();
77            c::GetSystemTimePreciseAsFileTime(&mut t.t);
78            t
79        }
80    }
81
82    const fn from_intervals(intervals: i64) -> SystemTime {
83        SystemTime {
84            t: c::FILETIME {
85                dwLowDateTime: intervals as u32,
86                dwHighDateTime: (intervals >> 32) as u32,
87            },
88        }
89    }
90
91    fn intervals(&self) -> i64 {
92        (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
93    }
94
95    pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
96        let me = self.intervals();
97        let other = other.intervals();
98        if me >= other {
99            Ok(intervals2dur((me - other) as u64))
100        } else {
101            Err(intervals2dur((other - me) as u64))
102        }
103    }
104
105    pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
106        let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
107        Some(SystemTime::from_intervals(intervals))
108    }
109
110    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
111        // Windows does not support times before 1601, hence why we don't
112        // support negatives. In order to tackle this, we try to convert the
113        // resulting value into an u64, which should obviously fail in the case
114        // that the value is below zero.
115        let intervals: u64 =
116            self.intervals().checked_sub(checked_dur2intervals(other)?)?.try_into().ok()?;
117        Some(SystemTime::from_intervals(intervals as i64))
118    }
119}
120
121impl PartialEq for SystemTime {
122    fn eq(&self, other: &SystemTime) -> bool {
123        self.intervals() == other.intervals()
124    }
125}
126
127impl Eq for SystemTime {}
128
129impl PartialOrd for SystemTime {
130    fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
131        Some(self.cmp(other))
132    }
133}
134
135impl Ord for SystemTime {
136    fn cmp(&self, other: &SystemTime) -> Ordering {
137        self.intervals().cmp(&other.intervals())
138    }
139}
140
141impl fmt::Debug for SystemTime {
142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143        f.debug_struct("SystemTime").field("intervals", &self.intervals()).finish()
144    }
145}
146
147impl From<c::FILETIME> for SystemTime {
148    fn from(t: c::FILETIME) -> SystemTime {
149        SystemTime { t }
150    }
151}
152
153impl IntoInner<c::FILETIME> for SystemTime {
154    fn into_inner(self) -> c::FILETIME {
155        self.t
156    }
157}
158
159impl Hash for SystemTime {
160    fn hash<H: Hasher>(&self, state: &mut H) {
161        self.intervals().hash(state)
162    }
163}