1 use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
3 macro_rules! assert_almost_eq {
4 ($a:expr, $b:expr) => {{
7 let (a, b) = if a > b { (a, b) } else { (b, a) };
8 assert!(a - Duration::from_micros(1) <= b, "{:?} is not almost equal to {:?}", a, b);
14 fn instant_monotonic() {
15 let a = Instant::now();
16 let b = Instant::now();
21 fn instant_elapsed() {
22 let a = Instant::now();
28 let a = Instant::now();
29 let b = Instant::now();
30 println!("a: {:?}", a);
31 println!("b: {:?}", b);
32 let dur = b.duration_since(a);
33 println!("dur: {:?}", dur);
34 assert_almost_eq!(b - dur, a);
35 assert_almost_eq!(a + dur, b);
37 let second = Duration::SECOND;
38 assert_almost_eq!(a - second + second, a);
39 assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
41 // checked_add_duration will not panic on overflow
42 let mut maybe_t = Some(Instant::now());
43 let max_duration = Duration::from_secs(u64::MAX);
44 // in case `Instant` can store `>= now + max_duration`.
46 maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
48 assert_eq!(maybe_t, None);
50 // checked_add_duration calculates the right time and will work for another year
51 let year = Duration::from_secs(60 * 60 * 24 * 365);
52 assert_eq!(a + year, a.checked_add(year).unwrap());
56 fn instant_math_is_associative() {
57 let now = Instant::now();
58 let offset = Duration::from_millis(5);
59 // Changing the order of instant math shouldn't change the results,
60 // especially when the expression reduces to X + identity.
61 assert_eq!((now + offset) - now, (now - now) + offset);
66 fn instant_duration_since_panic() {
67 let a = Instant::now();
68 (a - Duration::SECOND).duration_since(a);
72 fn instant_checked_duration_since_nopanic() {
73 let now = Instant::now();
74 let earlier = now - Duration::SECOND;
75 let later = now + Duration::SECOND;
76 assert_eq!(earlier.checked_duration_since(now), None);
77 assert_eq!(later.checked_duration_since(now), Some(Duration::SECOND));
78 assert_eq!(now.checked_duration_since(now), Some(Duration::ZERO));
82 fn instant_saturating_duration_since_nopanic() {
83 let a = Instant::now();
84 let ret = (a - Duration::SECOND).saturating_duration_since(a);
85 assert_eq!(ret, Duration::ZERO);
89 fn system_time_math() {
90 let a = SystemTime::now();
91 let b = SystemTime::now();
92 match b.duration_since(a) {
93 Ok(Duration::ZERO) => {
94 assert_almost_eq!(a, b);
98 assert_almost_eq!(b - dur, a);
99 assert_almost_eq!(a + dur, b);
102 let dur = dur.duration();
104 assert_almost_eq!(b + dur, a);
105 assert_almost_eq!(a - dur, b);
109 let second = Duration::SECOND;
110 assert_almost_eq!(a.duration_since(a - second).unwrap(), second);
111 assert_almost_eq!(a.duration_since(a + second).unwrap_err().duration(), second);
113 assert_almost_eq!(a - second + second, a);
114 assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
116 let one_second_from_epoch = UNIX_EPOCH + Duration::SECOND;
117 let one_second_from_epoch2 =
118 UNIX_EPOCH + Duration::from_millis(500) + Duration::from_millis(500);
119 assert_eq!(one_second_from_epoch, one_second_from_epoch2);
121 // checked_add_duration will not panic on overflow
122 let mut maybe_t = Some(SystemTime::UNIX_EPOCH);
123 let max_duration = Duration::from_secs(u64::MAX);
124 // in case `SystemTime` can store `>= UNIX_EPOCH + max_duration`.
126 maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
128 assert_eq!(maybe_t, None);
130 // checked_add_duration calculates the right time and will work for another year
131 let year = Duration::from_secs(60 * 60 * 24 * 365);
132 assert_eq!(a + year, a.checked_add(year).unwrap());
136 fn system_time_elapsed() {
137 let a = SystemTime::now();
143 let ts = SystemTime::now();
144 let a = ts.duration_since(UNIX_EPOCH + Duration::SECOND).unwrap();
145 let b = ts.duration_since(UNIX_EPOCH).unwrap();
147 assert_eq!(b - a, Duration::SECOND);
149 let thirty_years = Duration::SECOND * 60 * 60 * 24 * 365 * 30;
151 // Right now for CI this test is run in an emulator, and apparently the
152 // aarch64 emulator's sense of time is that we're still living in the
153 // 70s. This is also true for riscv (also qemu)
155 // Otherwise let's assume that we're all running computers later than
157 if !cfg!(target_arch = "aarch64") && !cfg!(target_arch = "riscv64") {
158 assert!(a > thirty_years);
161 // let's assume that we're all running computers earlier than 2090.
162 // Should give us ~70 years to fix this!
163 let hundred_twenty_years = thirty_years * 4;
164 assert!(a < hundred_twenty_years);