1use std::time;
6
7use crate::{ProtocolError, Timestamp};
8
9impl TryFrom<Timestamp> for std::time::SystemTime {
10 type Error = ProtocolError;
11
12 fn try_from(value: Timestamp) -> Result<Self, Self::Error> {
13 if value.seconds <= 0 || value.nanos < 0 {
14 return Err(ProtocolError::InvalidMessage);
15 }
16
17 std::time::SystemTime::UNIX_EPOCH
18 .checked_add(time::Duration::from_secs(value.seconds as u64))
19 .and_then(|ts| ts.checked_add(time::Duration::from_nanos(value.nanos as u64)))
20 .ok_or(ProtocolError::InvalidMessage)
21 }
22}
23
24impl From<time::SystemTime> for Timestamp {
25 fn from(value: time::SystemTime) -> Self {
26 let d = value.duration_since(time::UNIX_EPOCH).unwrap();
27
28 Self {
29 seconds: d.as_secs() as i64,
30 nanos: d.subsec_nanos() as i64,
31 }
32 }
33}