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        let instant_nsec = mul_div_u64(now as u64, NANOS_PER_SEC, freq);
39        Self { t: Duration::from_nanos(instant_nsec) }
40    }
41
42    pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
43        // On windows there's a threshold below which we consider two timestamps
44        // equivalent due to measurement error. For more details + doc link,
45        // check the docs on epsilon.
46        let epsilon = perf_counter::epsilon();
47        if other.t > self.t && other.t - self.t <= epsilon {
48            Some(Duration::new(0, 0))
49        } else {
50            self.t.checked_sub(other.t)
51        }
52    }
53
54    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
55        Some(Instant { t: self.t.checked_add(*other)? })
56    }
57
58    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
59        Some(Instant { t: self.t.checked_sub(*other)? })
60    }
61}
62
63impl SystemTime {
64    pub const MAX: SystemTime = SystemTime::from_intervals(i64::MAX);
65    pub const MIN: SystemTime = SystemTime::from_intervals(0);
66
67    pub fn now() -> SystemTime {
68        unsafe {
69            let mut t: SystemTime = mem::zeroed();
70            c::GetSystemTimePreciseAsFileTime(&mut t.t);
71            t
72        }
73    }
74
75    const fn from_intervals(intervals: i64) -> SystemTime {
76        SystemTime {
77            t: c::FILETIME {
78                dwLowDateTime: intervals as u32,
79                dwHighDateTime: (intervals >> 32) as u32,
80            },
81        }
82    }
83
84    fn intervals(&self) -> i64 {
85        (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
86    }
87
88    pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
89        let me = self.intervals();
90        let other = other.intervals();
91        if me >= other {
92            Ok(intervals2dur((me - other) as u64))
93        } else {
94            Err(intervals2dur((other - me) as u64))
95        }
96    }
97
98    pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
99        let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
100        Some(SystemTime::from_intervals(intervals))
101    }
102
103    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
104        // Windows does not support times before 1601, hence why we don't
105        // support negatives. In order to tackle this, we try to convert the
106        // resulting value into an u64, which should obviously fail in the case
107        // that the value is below zero.
108        let intervals: u64 =
109            self.intervals().checked_sub(checked_dur2intervals(other)?)?.try_into().ok()?;
110        Some(SystemTime::from_intervals(intervals as i64))
111    }
112}
113
114impl PartialEq for SystemTime {
115    fn eq(&self, other: &SystemTime) -> bool {
116        self.intervals() == other.intervals()
117    }
118}
119
120impl Eq for SystemTime {}
121
122impl PartialOrd for SystemTime {
123    fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
124        Some(self.cmp(other))
125    }
126}
127
128impl Ord for SystemTime {
129    fn cmp(&self, other: &SystemTime) -> Ordering {
130        self.intervals().cmp(&other.intervals())
131    }
132}
133
134impl fmt::Debug for SystemTime {
135    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136        f.debug_struct("SystemTime").field("intervals", &self.intervals()).finish()
137    }
138}
139
140impl From<c::FILETIME> for SystemTime {
141    fn from(t: c::FILETIME) -> SystemTime {
142        SystemTime { t }
143    }
144}
145
146impl IntoInner<c::FILETIME> for SystemTime {
147    fn into_inner(self) -> c::FILETIME {
148        self.t
149    }
150}
151
152impl Hash for SystemTime {
153    fn hash<H: Hasher>(&self, state: &mut H) {
154        self.intervals().hash(state)
155    }
156}