Some checks failed
Podman Rootless Demo / test-backend (push) Has been skipped
Podman Rootless Demo / test-frontend (push) Has been skipped
Podman Rootless Demo / build-backend (push) Has been skipped
Podman Rootless Demo / build-frontend (push) Failing after 5m32s
Podman Rootless Demo / deploy-prod (push) Has been skipped
52 lines
No EOL
1.4 KiB
Rust
52 lines
No EOL
1.4 KiB
Rust
//! Time abstraction for WASM compatibility
|
|
|
|
use crate::domain::error::DomainError;
|
|
|
|
/// Time provider trait for abstracting time operations
|
|
pub trait TimeProvider {
|
|
/// Get current timestamp in seconds since Unix epoch
|
|
fn now_seconds() -> Result<u64, DomainError>;
|
|
}
|
|
|
|
/// Standard library time provider
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub struct StdTimeProvider;
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
impl TimeProvider for StdTimeProvider {
|
|
fn now_seconds() -> Result<u64, DomainError> {
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.map_err(|e| DomainError::CryptographicError(format!("Time error: {}", e)))
|
|
.map(|d| d.as_secs())
|
|
}
|
|
}
|
|
|
|
/// WASM time provider
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub struct WasmTimeProvider;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
impl TimeProvider for WasmTimeProvider {
|
|
fn now_seconds() -> Result<u64, DomainError> {
|
|
// Use JavaScript Date API via js_sys
|
|
// This will work when compiled to WASM
|
|
let timestamp = js_sys::Date::now() / 1000.0; // Convert from milliseconds to seconds
|
|
Ok(timestamp as u64)
|
|
}
|
|
}
|
|
|
|
/// Get the current timestamp using the appropriate time provider
|
|
pub fn now_seconds() -> Result<u64, DomainError> {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
StdTimeProvider::now_seconds()
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
WasmTimeProvider::now_seconds()
|
|
}
|
|
} |