use coarsetime::{Clock, Updater};
use ctor::ctor;
use std::sync::LazyLock;
static COARSE_CLOCK_UPDATER: LazyLock<Updater> = LazyLock::new(|| {
let interval = std::env::var("PINGAP_COARSE_CLOCK_INTERVAL")
.unwrap_or("10".to_string())
.parse::<u64>()
.unwrap_or(10)
.clamp(1, 500);
Updater::new(interval)
.start()
.expect("Failed to start coarse clock updater")
});
fn init_time_cache() {
LazyLock::force(&COARSE_CLOCK_UPDATER);
}
const SUPER_TIMESTAMP: u64 = 1651852800;
#[inline]
pub fn now_sec() -> u64 {
Clock::recent_since_epoch().as_secs()
}
#[inline]
pub fn get_super_ts() -> u32 {
let super_ts_secs = SUPER_TIMESTAMP;
now_sec().saturating_sub(super_ts_secs) as u32
}
static HOST_NAME: LazyLock<String> = LazyLock::new(|| {
hostname::get()
.ok()
.as_deref()
.and_then(std::ffi::OsStr::to_str)
.unwrap_or("")
.to_string()
});
pub fn get_hostname() -> &'static str {
HOST_NAME.as_str()
}
#[inline]
pub fn now_ms() -> u64 {
Clock::recent_since_epoch().as_millis()
}
#[inline]
pub fn real_now_ms() -> u64 {
Clock::now_since_epoch().as_millis()
}
#[ctor]
fn init() {
init_time_cache();
}
#[cfg(test)]
mod tests {
use super::{
get_hostname, get_super_ts, init_time_cache, now_ms, real_now_ms,
};
use pretty_assertions::assert_eq;
#[test]
fn test_super_ts() {
init_time_cache();
assert_eq!(true, get_super_ts() > 104017048);
}
#[test]
fn test_now_ms() {
init_time_cache();
assert_eq!(true, now_ms() > 1755870295813);
}
#[test]
fn test_real_now_ms() {
init_time_cache();
assert_eq!(true, real_now_ms() > 1755870295813);
}
#[test]
fn test_get_hostname() {
assert_eq!(false, get_hostname().is_empty());
}
}