Files
tuliprox/shared/src/utils/time_utils.rs
T

25 lines
632 B
Rust
Raw Normal View History

2025-10-25 20:40:58 +02:00
#[cfg(target_arch = "wasm32")]
pub fn current_time_secs() -> u64 {
(js_sys::Date::now() / 1000.0) as u64
}
2025-04-16 14:23:16 +02:00
2025-10-25 20:40:58 +02:00
#[cfg(not(target_arch = "wasm32"))]
2025-04-16 14:23:16 +02:00
pub fn current_time_secs() -> u64 {
2025-10-25 20:40:58 +02:00
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
2025-04-16 14:23:16 +02:00
.unwrap_or_default()
.as_secs()
2025-07-17 17:20:59 +02:00
}
pub fn unix_ts_to_str(ts: i64) -> Option<String> {
if ts > 0 {
let normalized_ts = if ts > 4102444800 {
ts / 1000
} else {
ts
};
2025-10-25 20:40:58 +02:00
chrono::DateTime::from_timestamp(normalized_ts, 0).map(|dt| dt.format("%d.%m.%Y").to_string())
2025-07-17 17:20:59 +02:00
} else {
None
}
2025-04-16 14:23:16 +02:00
}