spatio_types/time.rs
1//! Time-conversion helpers shared across the workspace.
2
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5/// Convert an `f64` of seconds-since-the-Unix-epoch into a [`SystemTime`].
6///
7/// Returns an error instead of panicking on non-finite, negative, or
8/// out-of-range input. This matters because the value often arrives straight
9/// off the wire or from a Python caller, and [`Duration::from_secs_f64`] panics
10/// on exactly those cases.
11pub fn system_time_from_secs(secs: f64) -> Result<SystemTime, String> {
12 let dur =
13 Duration::try_from_secs_f64(secs).map_err(|e| format!("invalid timestamp {secs}: {e}"))?;
14 UNIX_EPOCH
15 .checked_add(dur)
16 .ok_or_else(|| format!("timestamp out of range: {secs}"))
17}