|
24 | 24 | use futures::{prelude::*, sync::oneshot, try_ready};
|
25 | 25 | use std::{error, fmt};
|
26 | 26 | use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
|
27 |
| -use std::ops::{Add, Sub}; |
| 27 | +use std::ops::{Add, Sub, AddAssign, SubAssign}; |
28 | 28 | use std::time::Duration;
|
29 | 29 | use wasm_bindgen::{prelude::*, JsCast};
|
30 | 30 |
|
@@ -105,6 +105,95 @@ impl Sub<Instant> for Instant {
|
105 | 105 | }
|
106 | 106 | }
|
107 | 107 |
|
| 108 | +pub const UNIX_EPOCH: SystemTime = SystemTime { inner: 0.0 }; |
| 109 | + |
| 110 | +#[derive(Debug, Copy, Clone)] |
| 111 | +pub struct SystemTime { |
| 112 | + /// Unit is milliseconds. |
| 113 | + inner: f64, |
| 114 | +} |
| 115 | + |
| 116 | +impl PartialEq for SystemTime { |
| 117 | + fn eq(&self, other: &SystemTime) -> bool { |
| 118 | + // Note that this will most likely only compare equal if we clone an `SystemTime`, |
| 119 | + // but that's ok. |
| 120 | + self.inner == other.inner |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl Eq for SystemTime {} |
| 125 | + |
| 126 | +impl PartialOrd for SystemTime { |
| 127 | + fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> { |
| 128 | + self.inner.partial_cmp(&other.inner) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl Ord for SystemTime { |
| 133 | + fn cmp(&self, other: &Self) -> Ordering { |
| 134 | + self.inner.partial_cmp(&other.inner).unwrap() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl SystemTime { |
| 139 | + pub const UNIX_EPOCH: SystemTime = SystemTime { inner: 0.0 }; |
| 140 | + |
| 141 | + pub fn now() -> SystemTime { |
| 142 | + let val = js_sys::Date::now(); |
| 143 | + SystemTime { inner: val } |
| 144 | + } |
| 145 | + |
| 146 | + pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, ()> { |
| 147 | + let dur_ms = self.inner - earlier.inner; |
| 148 | + if dur_ms < 0.0 { |
| 149 | + return Err(()) |
| 150 | + } |
| 151 | + Ok(Duration::from_millis(dur_ms as u64)) |
| 152 | + } |
| 153 | + |
| 154 | + pub fn elapsed(&self) -> Result<Duration, ()> { |
| 155 | + self.duration_since(SystemTime::now()) |
| 156 | + } |
| 157 | + |
| 158 | + pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> { |
| 159 | + Some(*self + duration) |
| 160 | + } |
| 161 | + |
| 162 | + pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> { |
| 163 | + Some(*self - duration) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl Add<Duration> for SystemTime { |
| 168 | + type Output = SystemTime; |
| 169 | + |
| 170 | + fn add(self, other: Duration) -> SystemTime { |
| 171 | + let new_val = self.inner + other.as_millis() as f64; |
| 172 | + SystemTime { inner: new_val as f64 } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl Sub<Duration> for SystemTime { |
| 177 | + type Output = SystemTime; |
| 178 | + |
| 179 | + fn sub(self, other: Duration) -> SystemTime { |
| 180 | + let new_val = self.inner - other.as_millis() as f64; |
| 181 | + SystemTime { inner: new_val as f64 } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +impl AddAssign<Duration> for SystemTime { |
| 186 | + fn add_assign(&mut self, rhs: Duration) { |
| 187 | + *self = *self + rhs; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +impl SubAssign<Duration> for SystemTime { |
| 192 | + fn sub_assign(&mut self, rhs: Duration) { |
| 193 | + *self = *self - rhs; |
| 194 | + } |
| 195 | +} |
| 196 | + |
108 | 197 | #[derive(Debug)]
|
109 | 198 | pub struct Error;
|
110 | 199 |
|
|
0 commit comments