use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub struct Timestamp(std::time::SystemTime);
impl Timestamp {
pub fn from_path(path: impl AsRef<Path>) -> std::io::Result<Self> {
let metadata = fs_err::metadata(path.as_ref())?;
Ok(Self::from_metadata(&metadata))
}
pub fn from_metadata(metadata: &std::fs::Metadata) -> Self {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
let ctime = u64::try_from(metadata.ctime()).expect("ctime to be representable as u64");
let ctime_nsec = u32::try_from(metadata.ctime_nsec())
.expect("ctime_nsec to be representable as u32");
let duration = std::time::Duration::new(ctime, ctime_nsec);
Self(std::time::UNIX_EPOCH + duration)
}
#[cfg(not(unix))]
{
let modified = metadata.modified().expect("modified time to be available");
Self(modified)
}
}
pub fn now() -> Self {
Self(std::time::SystemTime::now())
}
}
impl From<std::time::SystemTime> for Timestamp {
fn from(system_time: std::time::SystemTime) -> Self {
Self(system_time)
}
}