abstract duration parsing into utils
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
4d42a29c51
commit
dea5fee6a3
6 changed files with 24 additions and 12 deletions
|
@ -68,6 +68,7 @@ clap.workspace = true
|
|||
conduit-macros.workspace = true
|
||||
const-str.workspace = true
|
||||
ctor.workspace = true
|
||||
cyborgtime.workspace = true
|
||||
either.workspace = true
|
||||
figment.workspace = true
|
||||
http-body-util.workspace = true
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::{err, Result};
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
|
||||
|
@ -10,6 +12,22 @@ pub fn now_millis() -> u64 {
|
|||
.as_millis() as u64
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn parse_timepoint_ago(ago: &str) -> Result<SystemTime> { timepoint_ago(parse_duration(ago)?) }
|
||||
|
||||
#[inline]
|
||||
pub fn timepoint_ago(duration: Duration) -> Result<SystemTime> {
|
||||
SystemTime::now()
|
||||
.checked_sub(duration)
|
||||
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large")))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn parse_duration(duration: &str) -> Result<Duration> {
|
||||
cyborgtime::parse_duration(duration)
|
||||
.map_err(|error| err!("'{duration:?}' is not a valid duration string: {error:?}"))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn rfc2822_from_seconds(epoch: i64) -> String {
|
||||
use chrono::{DateTime, Utc};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue