pub(crate) use self::sys::*;
#[cfg(not(all(
feature = "js",
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"
)))]
mod sys {
pub(crate) fn system_time() -> std::time::SystemTime {
if cfg!(all(
not(feature = "js"),
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"
)) {
panic!(
"getting the current time in wasm32-unknown-unknown \
is not possible with just the standard library, \
enable Jiff's `js` feature if you are \
targeting a browser environment",
);
} else {
std::time::SystemTime::now()
}
}
#[cfg(any(
feature = "tz-system",
feature = "tzdb-zoneinfo",
feature = "tzdb-concatenated"
))]
pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
if cfg!(all(
not(feature = "js"),
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"
)) {
None
} else {
Some(std::time::Instant::now())
}
}
}
#[cfg(all(
feature = "js",
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"
))]
mod sys {
pub(crate) fn system_time() -> std::time::SystemTime {
use std::time::{Duration, SystemTime};
let millis = js_sys::Date::new_0().get_time();
let is_positive = millis.is_sign_positive();
let millis = millis.abs() as u64;
let duration = Duration::from_millis(millis);
let result = if is_positive {
SystemTime::UNIX_EPOCH.checked_add(duration)
} else {
SystemTime::UNIX_EPOCH.checked_sub(duration)
};
let Some(timestamp) = result else {
panic!(
"failed to get current time from Javascript date: \
arithmetic on Unix epoch overflowed"
)
};
timestamp
}
#[cfg(any(
feature = "tz-system",
feature = "tzdb-zoneinfo",
feature = "tzdb-concatenated"
))]
pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
None
}
}