use std::time::SystemTime;
#[allow(unreachable_code)]
fn time_nanos() -> u128 {
#[cfg(target_arch = "wasm32")]
{
return u128::from(crate::api::time());
}
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(d) => d.as_nanos(),
Err(_) => 0,
}
}
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub fn now_secs() -> u64 {
(time_nanos() / 1_000_000_000) as u64
}
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub fn now_millis() -> u64 {
(time_nanos() / 1_000_000) as u64
}
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub fn now_micros() -> u64 {
(time_nanos() / 1_000) as u64
}
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub fn now_nanos() -> u64 {
time_nanos() as u64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_now_secs_sanity() {
let now = now_secs();
let current_year_secs = 1_700_000_000; assert!(now > current_year_secs);
}
}