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

Add duration_since_epoch method to Spot

parent 3b7d2cc8
No related branches found
No related tags found
No related merge requests found
[package]
name = "web_instant"
version = "0.2.1"
version = "0.3.0"
edition = "2021"
description = "Cross platform impl of Instant"
authors = [
......
hard_tabs = true
#group_imports = "StdExternalCrate"
use_field_init_shorthand = true
use_try_shorthand = true
\ No newline at end of file
use std::fmt::{Debug, Formatter};
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::{Duration, Instant};
use std::time::{Duration, Instant, SystemTime};
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Copy, Clone, PartialEq, PartialOrd)]
......@@ -54,6 +54,18 @@ impl Spot {
pub fn checked_sub(&self, duration: Duration) -> Option<Spot> {
self.inner.checked_sub(duration).map(|inner| Spot { inner })
}
/// Converts this `Spot` into a duration that represents the time that has elapsed between the
/// UNIX Epoch and this `Spot`
pub fn duration_since_epoch(&self) -> Duration {
let duration_since_epoch = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Failed to get time since epoch");
let duration_since_instant = self.elapsed();
duration_since_epoch - duration_since_instant
}
}
impl Add<Duration> for Spot {
......@@ -64,7 +76,8 @@ impl Add<Duration> for Spot {
/// This function may panic if the resulting point in time cannot be represented by the
/// underlying data structure. See [`Spot::checked_add`] for a version without panic.
fn add(self, other: Duration) -> Spot {
self.checked_add(other).expect("overflow when adding duration to instant")
self.checked_add(other)
.expect("overflow when adding duration to instant")
}
}
......@@ -78,7 +91,8 @@ impl Sub<Duration> for Spot {
type Output = Spot;
fn sub(self, other: Duration) -> Spot {
self.checked_sub(other).expect("overflow when subtracting duration from instant")
self.checked_sub(other)
.expect("overflow when subtracting duration from instant")
}
}
......
use std::fmt::{Debug, Formatter};
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
use std::time::{Duration, Instant};
#[derive(Copy, Clone, PartialEq, PartialOrd)]
pub struct Spot {
......@@ -27,7 +27,10 @@ impl Spot {
/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is later than this one.
pub fn duration_since(&self, earlier: Spot) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
match self.checked_duration_since(earlier) {
Some(duration) => duration,
None => std::process::abort(),
}
}
/// Returns the amount of time elapsed from another instant to this one,
/// or None if that instant is later than this one.
......@@ -45,14 +48,19 @@ impl Spot {
/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is later than this one.
pub fn saturating_duration_since(&self, earlier: Spot) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
match self.checked_duration_since(earlier) {
Some(duration) => duration,
None => std::process::abort(),
}
}
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
pub fn checked_add(&self, duration: Duration) -> Option<Spot> {
let duration_millis = duration.as_secs_f64() / 1000.0;
Some(Spot { inner: self.inner + duration_millis })
Some(Spot {
inner: self.inner + duration_millis,
})
}
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
......@@ -62,9 +70,17 @@ impl Spot {
if duration_millis > self.inner {
None
} else {
Some(Spot { inner: self.inner - duration_millis })
Some(Spot {
inner: self.inner - duration_millis,
})
}
}
/// Converts this `Spot` into a duration that represents the time that has elapsed between the
/// UNIX Epoch and this `Spot`
pub fn duration_since_epoch(&self) -> Duration {
self.duration_since(Spot { inner: 0.0 })
}
}
impl Add<Duration> for Spot {
......@@ -75,7 +91,13 @@ impl Add<Duration> for Spot {
/// This function may panic if the resulting point in time cannot be represented by the
/// underlying data structure. See [`Spot::checked_add`] for a version without panic.
fn add(self, other: Duration) -> Spot {
self.checked_add(other).expect("overflow when adding duration to instant")
match self.checked_add(other) {
Some(duration) => duration,
None => {
eprint!("overflow when adding duration to instant");
std::process::abort();
}
}
}
}
......@@ -89,7 +111,13 @@ impl Sub<Duration> for Spot {
type Output = Spot;
fn sub(self, other: Duration) -> Spot {
self.checked_sub(other).expect("overflow when subtracting duration from instant")
match self.checked_sub(other) {
Some(duration) => duration,
None => {
eprintln!("overflow when subtracting duration from instant");
std::process::abort();
}
}
}
}
......@@ -99,7 +127,6 @@ impl SubAssign<Duration> for Spot {
}
}
impl Sub<Spot> for Spot {
type Output = Duration;
......
//! # Web Instant
//!
//! Provides `Spot`, a version of `Instant` that works both in environments that support `Instant` and `wasm` family targets. On desktop, the internal representation uses `std::time::Instant`. On the web, the internal representation is an `f64`,
//! and uses `js_sys::Date` methods to cover functinality where required
//!
//! All the methods on `Instant` are implemented for `Spot`, so you can just replace uses of `std::time::Instant` with `web_instant::Spot`
//!
//!
//! Provides `Spot`, a version of `Instant` that works both in environments that support `Instant` and
//! `wasm` family targets. On desktop, the internal representation uses `std::time::Instant`. On the
//! web, the internal representation is an `f64`, and uses `js_sys::Date` methods to cover
//! functionality where required
//!
//! All the methods on `Instant` are implemented for `Spot`, so you can just replace uses of
//! `std::time::Instant` with `web_instant::Spot`
//!
//! ## Install
//!
//!
//! `cargo add web_instant`
//!
//!
//! ## Usage
//!
//!
//! ```rust
//! use web_instant::Spot;
//! use std::time::Duration;
//!
//!
//! fn my_cross_platform_timer(last_time: Spot) {
//! let time_diff: Duration = Spot::now() - last_time;
//! println!("It has been {} seconds", time_diff.as_secs());
......
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