Skip to content
Snippets Groups Projects
Verified Commit 07ca0b51 authored by Louis's avatar Louis :fire:
Browse files

Add basic conversion methods

parents
No related branches found
No related tags found
No related merge requests found
/target
/Cargo.lock
[package]
name = "web_instant"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
use std::ops::Sub;
use std::time::{Duration, Instant};
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Spot {
inner: Instant,
}
impl Spot {
pub fn now() -> Self {
Spot {
inner: Instant::now(),
}
}
pub fn as_secs(&self) -> u64 {
self.as_duration().as_secs()
}
pub fn as_duration(&self) -> Duration {
self.inner.elapsed()
}
pub fn elapsed(&self) -> Duration {
self.as_duration()
}
}
impl Sub<Spot> for Spot {
type Output = Duration;
fn sub(self, rhs: Spot) -> Self::Output {
self.inner - rhs.inner
}
}
use std::ops::Sub;
use std::time::Duration;
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Spot {
/// Millisecond offset from the Unix Epoch - equivalent to Date.now()
inner: f64,
}
impl Spot {
pub fn now() -> Self {
Spot {
inner: js_sys::Date::now(),
}
}
pub fn as_secs(&self) -> u64 {
self.as_duration().as_secs()
}
pub fn as_duration(&self) -> Duration {
Self::now() - *self
}
pub fn elapsed(&self) -> Duration {
self.as_duration()
}
}
impl Sub<Spot> for Spot {
type Output = Duration;
fn sub(self, rhs: Spot) -> Self::Output {
let diff = (self.inner - rhs.inner).max(0.0);
let secs = (diff as u64) / 1_000;
let nanos = (((diff as u64) % 1_000) as u32) * 1_000_000;
Duration::new(secs, nanos)
}
}
#[cfg(not(target_arch = "wasm32"))]
mod instant_desktop;
#[cfg(target_arch = "wasm32")]
mod instant_web;
#[cfg(not(target_arch = "wasm32"))]
pub use instant_desktop::Spot;
#[cfg(target_arch = "wasm32")]
pub use instant_web::Spot;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment