use chrono::NaiveDateTime;
use std::io;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn unix_now_seconds() -> f64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0)
}
pub fn resolve_file_start_unix(path: &Path) -> Result<f64, io::Error> {
if let Some(ts) = parse_gqrx_start_unix(path) {
return Ok(ts);
}
let meta = std::fs::metadata(path)?;
if let Ok(created) = meta.created() {
return Ok(created
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0));
}
if let Ok(modified) = meta.modified() {
return Ok(modified
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0));
}
Err(io::Error::other(
"Could not determine absolute start time for input file",
))
}
pub fn parse_gqrx_start_unix(path: &Path) -> Option<f64> {
let name = path.file_name()?.to_str()?;
if !name.starts_with("gqrx_") {
return None;
}
let mut parts = name.split('_');
if parts.next()? != "gqrx" {
return None;
}
let date = parts.next()?;
let time = parts.next()?;
if date.len() != 8 || time.len() != 6 {
return None;
}
let dt = NaiveDateTime::parse_from_str(&format!("{date}{time}"), "%Y%m%d%H%M%S").ok()?;
Some(dt.and_utc().timestamp() as f64)
}