wowlab_common/time.rs
1//! Cross-platform time helpers via `web-time` (native and wasm).
2
3use std::time::Duration;
4
5use wowlab_types::constants::MS_PER_SECOND_U64;
6
7#[rustfmt::skip]
8// #t(rust_foreign_reexports) web-time is the workspace's cross-platform Instant abstraction
9pub use web_time::Instant;
10
11/// Current unix timestamp in whole seconds.
12#[must_use]
13pub fn unix_timestamp_secs() -> u64 {
14 unix_timestamp_millis() / MS_PER_SECOND_U64
15}
16
17/// Current unix timestamp in milliseconds.
18#[must_use]
19pub fn unix_timestamp_millis() -> u64 {
20 let now = {
21 // #t(block: rust_ambient_syscall) this zero-argument helper is the system-clock adapter boundary
22 web_time::SystemTime::now()
23 };
24
25 u64::try_from(
26 now.duration_since(web_time::UNIX_EPOCH)
27 .unwrap_or(Duration::ZERO)
28 .as_millis(),
29 )
30 .unwrap_or(u64::MAX)
31}