From 89b5c4ee1c6cfd662a60d704b1ffec736d7a3600 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 11 Oct 2024 20:32:32 +0000 Subject: [PATCH] add timepoint_from_now to complement timepoint_ago in utils Signed-off-by: Jason Volk --- src/core/utils/mod.rs | 2 +- src/core/utils/time.rs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 4dbecf91..13221341 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -34,7 +34,7 @@ pub use self::{ stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt}, string::{str_from_bytes, string_from_bytes}, sys::available_parallelism, - time::now_millis as millis_since_unix_epoch, + time::{now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now}, }; #[inline] diff --git a/src/core/utils/time.rs b/src/core/utils/time.rs index 04f47ac3..f96a27d0 100644 --- a/src/core/utils/time.rs +++ b/src/core/utils/time.rs @@ -22,6 +22,13 @@ pub fn timepoint_ago(duration: Duration) -> Result { .ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large"))) } +#[inline] +pub fn timepoint_from_now(duration: Duration) -> Result { + SystemTime::now() + .checked_add(duration) + .ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large"))) +} + #[inline] pub fn parse_duration(duration: &str) -> Result { cyborgtime::parse_duration(duration)