#[cfg(not(target_arch = "wasm32"))]
pub use std::time::Instant;
#[cfg(target_arch = "wasm32")]
pub use wasm::Instant;
#[cfg(target_arch = "wasm32")]
mod wasm {
use std::ops::Add;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(u64);
impl Instant {
pub fn now() -> Self {
Instant(0)
}
pub fn elapsed(&self) -> Duration {
Duration::ZERO
}
pub fn duration_since(&self, _earlier: Instant) -> Duration {
Duration::ZERO
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, _rhs: Duration) -> Instant {
Instant(u64::MAX)
}
}
}
pub fn unix_seconds() -> u64 {
#[cfg(not(target_arch = "wasm32"))]
{
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
#[cfg(target_arch = "wasm32")]
{
0
}
}
#[cfg(test)]
mod tests {
#[test]
fn deadline_pattern_works() {
let deadline = super::Instant::now() + std::time::Duration::from_secs(8);
assert!(super::Instant::now() < deadline);
}
}