cap_primitives/fs/
system_time_spec.rs

1use crate::time::SystemTime;
2
3/// A value for specifying a time.
4#[derive(Debug)]
5#[allow(clippy::module_name_repetitions)]
6pub enum SystemTimeSpec {
7    /// A value which always represents the current time, in symbolic form, so
8    /// that even as time elapses, it continues to represent the current time.
9    SymbolicNow,
10
11    /// An absolute time value.
12    Absolute(SystemTime),
13}
14
15impl SystemTimeSpec {
16    /// Constructs a new instance of `Self` from the given
17    /// [`fs_set_times::SystemTimeSpec`].
18    // TODO: Make this a `const fn` once `SystemTime::from_std` is a `const fn`.
19    #[inline]
20    pub fn from_std(std: fs_set_times::SystemTimeSpec) -> Self {
21        match std {
22            fs_set_times::SystemTimeSpec::SymbolicNow => Self::SymbolicNow,
23            fs_set_times::SystemTimeSpec::Absolute(time) => {
24                Self::Absolute(SystemTime::from_std(time))
25            }
26        }
27    }
28
29    /// Constructs a new instance of [`fs_set_times::SystemTimeSpec`] from the
30    /// given `Self`.
31    #[inline]
32    pub const fn into_std(self) -> fs_set_times::SystemTimeSpec {
33        match self {
34            Self::SymbolicNow => fs_set_times::SystemTimeSpec::SymbolicNow,
35            Self::Absolute(time) => fs_set_times::SystemTimeSpec::Absolute(time.into_std()),
36        }
37    }
38}
39
40impl From<SystemTime> for SystemTimeSpec {
41    #[inline]
42    fn from(time: SystemTime) -> Self {
43        Self::Absolute(time)
44    }
45}