Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SystemTime #9

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ pub use crate::native::now;
#[cfg(feature = "now")]
pub use crate::wasm::now;

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

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


pub use std::time::Duration;

#[cfg(any(
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
59 changes: 59 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,62 @@ pub fn now() -> f64 {
.expect("performance should be available")
.now()
}

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

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

@sebcrozet sebcrozet Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this is a valid definition. It is my understanding that doing SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) should return the duration since the unix epoch. However, the now function may return results from the JS function performance.now(), which is not relative to the UNIX epoch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about wngr@bd46ccb ? If @expenses agrees, I could create a new PR based on her commits.


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 All @@ -19,3 +19,12 @@ fn test_duration() {
let one_sec = Duration::from_secs(1);
assert!(now.elapsed() < one_sec);
}

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