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
use std::time::{ Duration, SystemTime, SystemTimeError, UNIX_EPOCH, }; pub mod duration_ext; pub trait SystemTimeExt { fn duration_since_epoch(&self) -> Result<Duration, SystemTimeError>; #[inline] fn seconds_since_epoch(&self) -> Result<u64, SystemTimeError> { self.duration_since_epoch().map(|d| d.as_secs()) } } impl SystemTimeExt for SystemTime { #[inline] fn duration_since_epoch(&self) -> Result<Duration, SystemTimeError> { self.duration_since(UNIX_EPOCH) } } pub trait DurationExt { fn as_seconds_since_epoch(&self) -> SystemTime; } impl DurationExt for Duration { #[inline] fn as_seconds_since_epoch(&self) -> SystemTime { UNIX_EPOCH + *self } }