Skip to content

Commit fd76bd4

Browse files
committed
Add SystemTime
1 parent 1347e8b commit fd76bd4

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
name = "wasm-timer"
33
edition = "2018"
44
description = "Abstraction over std::time::Instant and tokio_timer that works on WASM"
5-
version = "0.1.1"
5+
version = "0.1.2"
66
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
77
license = "MIT"
88
repository = "https://github.com/tomaka/wasm-timer"
99

1010
[target.'cfg(any(target_arch = "wasm32"))'.dependencies]
1111
futures = "0.1"
12+
js-sys = "0.3.14"
1213
send_wrapper = "0.2"
1314
wasm-bindgen = "0.2.37"
1415
web-sys = { version = "0.3.14", features = ["Performance", "Window"] }

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
#[cfg(not(target_arch = "wasm32"))]
22-
pub use std::time::Instant;
22+
pub use std::time::{Instant, SystemTime, UNIX_EPOCH};
2323
#[cfg(not(target_arch = "wasm32"))]
2424
pub use tokio_timer::*;
2525
#[cfg(target_arch = "wasm32")]

src/wasm.rs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use futures::{prelude::*, sync::oneshot, try_ready};
2525
use std::{error, fmt};
2626
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
27-
use std::ops::{Add, Sub};
27+
use std::ops::{Add, Sub, AddAssign, SubAssign};
2828
use std::time::Duration;
2929
use wasm_bindgen::{prelude::*, JsCast};
3030

@@ -105,6 +105,95 @@ impl Sub<Instant> for Instant {
105105
}
106106
}
107107

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+
108197
#[derive(Debug)]
109198
pub struct Error;
110199

0 commit comments

Comments
 (0)