app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

time.rs (797B)


      1 //! Time values shared by identity and profile records.
      2 
      3 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
      4 pub struct UnixTimestamp(i64);
      5 
      6 impl UnixTimestamp {
      7     pub const UNIX_EPOCH: Self = Self(0);
      8 
      9     #[must_use]
     10     pub const fn from_seconds(seconds: i64) -> Option<Self> {
     11         if seconds < 0 {
     12             None
     13         } else {
     14             Some(Self(seconds))
     15         }
     16     }
     17 
     18     #[must_use]
     19     pub const fn as_seconds(self) -> i64 {
     20         self.0
     21     }
     22 }
     23 
     24 #[cfg(test)]
     25 mod tests {
     26     use super::UnixTimestamp;
     27 
     28     #[test]
     29     fn timestamp_rejects_negative_seconds() {
     30         assert_eq!(UnixTimestamp::from_seconds(-1), None);
     31         assert_eq!(
     32             UnixTimestamp::from_seconds(0).map(UnixTimestamp::as_seconds),
     33             Some(0)
     34         );
     35     }
     36 }