Skip to content

Commit

Permalink
Add SystemTime
Browse files Browse the repository at this point in the history
  • Loading branch information
expenses authored and wngr committed Sep 30, 2021
1 parent 092475e commit 8796d1c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,19 @@ cfg_if::cfg_if! {
}
}

#[cfg(any(
not(any(target_arch = "wasm32", target_arch = "asmjs")),
not(any(feature = "stdweb", feature = "wasm-bindgen"))
))]
#[cfg(feature = "now")]
pub use crate::native::SystemTime;

#[cfg(all(
any(target_arch = "wasm32", target_arch = "asmjs"),
any(feature = "stdweb", feature = "wasm-bindgen")
))]
#[cfg(feature = "now")]
pub use crate::wasm::SystemTime;


pub use std::time::Duration;
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
63 changes: 63 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,65 @@ pub fn now() -> f64 {
#[cfg(target_os = "emscripten")]
return unsafe { js::_emscripten_get_now() };
}

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

impl Eq for SystemTime {}

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

pub fn now() -> SystemTime {
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;
}
}
11 changes: 10 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,12 @@ 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 8796d1c

Please sign in to comment.