1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::time::{Duration, SystemTime};

pub(crate) fn since_epoch(systemtime: SystemTime) -> Result<Duration, std::io::Error> {
    systemtime
        .duration_since(SystemTime::UNIX_EPOCH)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
}

#[derive(
    Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
/// Represents file or directory timestamps.
pub struct Timestamps {
    pub(crate) modified: u64,
}

impl Timestamps {
    #[cfg(feature = "package")]
    /// Extracts timestamps from the metadata.
    pub(crate) fn from_metadata(
        metadata: &std::fs::Metadata,
    ) -> Result<Timestamps, std::io::Error> {
        let modified = metadata.modified()?;

        let modified = since_epoch(modified).unwrap();

        Ok(Timestamps {
            modified: modified.as_nanos() as u64,
        })
    }

    /// timestamp of the last access time
    pub fn accessed(&self) -> u64 {
        0
    }

    /// timestamp of the latest modification time
    pub fn modified(&self) -> u64 {
        self.modified
    }

    /// timestamp of the creation time
    pub fn created(&self) -> u64 {
        0
    }
}