Skip to content

Commit

Permalink
Merge pull request #42 from wngr/systemtime
Browse files Browse the repository at this point in the history
Add `Systemtime` (second attempt)
  • Loading branch information
sebcrozet authored Oct 18, 2021
2 parents b79ce7e + 7b058d4 commit d90a340
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ cfg_if::cfg_if! {
pub use wasm::Instant;
#[cfg(feature = "now")]
pub use crate::wasm::now;
pub use wasm::SystemTime;
} else {
mod native;
pub use native::Instant;
#[cfg(feature = "now")]
pub use native::now;
pub use native::SystemTime;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/native.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub type Instant = std::time::Instant;
pub type SystemTime = std::time::SystemTime;

/// The current time, in milliseconds.
#[cfg(feature = "now")]
Expand Down
78 changes: 78 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;

Expand Down Expand Up @@ -160,3 +161,80 @@ pub fn now() -> f64 {
#[cfg(target_os = "emscripten")]
return unsafe { js::_emscripten_get_now() };
}

/// Returns the number of millisecods elapsed since January 1, 1970 00:00:00 UTC.
#[cfg(any(feature = "wasm-bindgen", feature = "stdweb"))]
fn get_time() -> f64 {
#[cfg(feature = "wasm-bindgen")]
return js_sys::Date::now();
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
{
let v = js! { return Date.now(); };
return v.try_into().unwrap();
}
}

#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct SystemTime(f64);

impl SystemTime {
pub const UNIX_EPOCH: SystemTime = SystemTime(0.0);

pub fn now() -> SystemTime {
cfg_if::cfg_if! {
if #[cfg(any(feature = "wasm-bindgen", feature = "stdweb"))] {
SystemTime(get_time())
} else {
SystemTime(now())
}
}
}

pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, ()> {
let dur_ms = self.0 - earlier.0;
if dur_ms < 0.0 {
return Err(());
}
Ok(Duration::from_millis(dur_ms as u64))
}

pub fn elapsed(&self) -> Result<Duration, ()> {
self.duration_since(SystemTime::now())
}

pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
Some(*self + duration)
}

pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> {
Some(*self - duration)
}
}

impl Add<Duration> for SystemTime {
type Output = SystemTime;

fn add(self, other: Duration) -> SystemTime {
SystemTime(self.0 + other.as_millis() as f64)
}
}

impl Sub<Duration> for SystemTime {
type Output = SystemTime;

fn sub(self, other: Duration) -> SystemTime {
SystemTime(self.0 - other.as_millis() as f64)
}
}

impl AddAssign<Duration> for SystemTime {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}

impl SubAssign<Duration> for SystemTime {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
10 changes: 9 additions & 1 deletion tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate wasm_bindgen_test;

use instant::Instant;
use instant::{Instant, SystemTime};
use std::time::Duration;
use wasm_bindgen_test::*;

Expand Down Expand Up @@ -47,3 +47,11 @@ fn test_checked_sub() {
.checked_sub(Duration::new(u64::MAX, ONE_BILLION - 1))
.is_none());
}

#[wasm_bindgen_test]
fn test_system_time() {
assert!(SystemTime::UNIX_EPOCH
.duration_since(SystemTime::now())
.is_err());
}

0 comments on commit d90a340

Please sign in to comment.