Add timepoint_from_epoch to time utils.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -22,22 +22,37 @@ pub fn now() -> Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse_timepoint_ago(ago: &str) -> Result<SystemTime> {
|
#[must_use]
|
||||||
timepoint_ago(parse_duration(ago)?)
|
pub fn duration_since_epoch(timepoint: SystemTime) -> Duration {
|
||||||
|
timepoint
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or(Duration::ZERO)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn timepoint_ago(duration: Duration) -> Result<SystemTime> {
|
pub fn timepoint_from_epoch(duration: Duration) -> Result<SystemTime> {
|
||||||
SystemTime::now()
|
UNIX_EPOCH
|
||||||
.checked_sub(duration)
|
.checked_add(duration)
|
||||||
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large")))
|
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} from epoch is too large")))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn timepoint_from_now(duration: Duration) -> Result<SystemTime> {
|
pub fn timepoint_from_now(duration: Duration) -> Result<SystemTime> {
|
||||||
SystemTime::now()
|
SystemTime::now()
|
||||||
.checked_add(duration)
|
.checked_add(duration)
|
||||||
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large")))
|
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} from now is too large")))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn timepoint_ago(duration: Duration) -> Result<SystemTime> {
|
||||||
|
SystemTime::now()
|
||||||
|
.checked_sub(duration)
|
||||||
|
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} ago is too large")))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn parse_timepoint_ago(ago: &str) -> Result<SystemTime> {
|
||||||
|
timepoint_ago(parse_duration(ago)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
Reference in New Issue
Block a user