Skip to content

Commit 074d153

Browse files
committed
Add TryFrom<MlsTime> for SystemTime
1 parent 84dfb5b commit 074d153

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

mls-rs-core/src/time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ impl From<MlsTime> for Duration {
6060
}
6161
}
6262

63+
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
64+
#[derive(Debug, thiserror::Error)]
65+
#[error("Overflow while adding {0:?}")]
66+
/// Overflow in time conversion.
67+
pub struct TimeOverflow(Duration);
68+
69+
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
70+
impl TryFrom<MlsTime> for std::time::SystemTime {
71+
type Error = TimeOverflow;
72+
73+
fn try_from(value: MlsTime) -> Result<std::time::SystemTime, Self::Error> {
74+
let duration = Duration::from(value);
75+
std::time::SystemTime::UNIX_EPOCH
76+
.checked_add(duration)
77+
.ok_or(TimeOverflow(duration))
78+
}
79+
}
80+
6381
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
6482
impl TryFrom<std::time::SystemTime> for MlsTime {
6583
type Error = std::time::SystemTimeError;

0 commit comments

Comments
 (0)